summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-03-23 13:43:38 -0500
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commitb41d53596cdcb6d7220b93926f6eedf171c09c46 (patch)
treef7616b6197520fdb5f96fe37aa955518d7b26978
parent7504f1ede94900ab42cd6e9c4ff9c8a5cb9e65ee (diff)
downloadpuppet-b41d53596cdcb6d7220b93926f6eedf171c09c46.tar.gz
puppet-b41d53596cdcb6d7220b93926f6eedf171c09c46.tar.xz
puppet-b41d53596cdcb6d7220b93926f6eedf171c09c46.zip
Adding filename extension support to formats.
This is toward fixing #1943 - we need the ability to easily convert between file extensions and file formats. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/network/format.rb3
-rw-r--r--lib/puppet/network/format_handler.rb7
-rw-r--r--lib/puppet/network/formats.rb2
-rwxr-xr-xspec/unit/network/format.rb7
-rwxr-xr-xspec/unit/network/format_handler.rb9
-rwxr-xr-xspec/unit/network/formats.rb4
6 files changed, 30 insertions, 2 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index d78124221..7c3cc7bd8 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -7,7 +7,7 @@ class Puppet::Network::Format
include Puppet::Provider::Confiner
attr_reader :name, :mime
- attr_accessor :intern_method, :render_method, :intern_multiple_method, :render_multiple_method, :weight, :required_methods
+ attr_accessor :intern_method, :render_method, :intern_multiple_method, :render_multiple_method, :weight, :required_methods, :extension
def init_attribute(name, default)
if value = @options[name]
@@ -36,6 +36,7 @@ class Puppet::Network::Format
init_attribute(:mime, "text/%s" % name)
init_attribute(:weight, 5)
init_attribute(:required_methods, method_list.keys)
+ init_attribute(:extension, name.to_s)
method_list.each do |method, value|
init_attribute(method, value)
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
index ea8cf35de..a637f8b89 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -51,6 +51,13 @@ module Puppet::Network::FormatHandler
@formats[name.to_s.downcase.intern]
end
+ def self.format_by_extension(ext)
+ @formats.each do |name, format|
+ return format if format.extension == ext
+ end
+ return nil
+ end
+
# Provide a list of all formats.
def self.formats
@formats.keys
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index a98dcbcc5..62569d4b1 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -131,7 +131,7 @@ Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal") do
end
end
-Puppet::Network::FormatHandler.create(:s, :mime => "text/plain")
+Puppet::Network::FormatHandler.create(:s, :mime => "text/plain", :extension => "txt")
# A very low-weight format so it'll never get chosen automatically.
Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw", :weight => 1) do
diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb
index e5a6b599c..73530b9d7 100755
--- a/spec/unit/network/format.rb
+++ b/spec/unit/network/format.rb
@@ -123,6 +123,13 @@ describe Puppet::Network::Format do
Puppet::Network::Format.new(:foo, :weight => 1).weight.should == 1
end
+ it "should default to its extension being equal to its name" do
+ Puppet::Network::Format.new(:foo).extension.should == "foo"
+ end
+
+ it "should support overriding the extension" do
+ Puppet::Network::Format.new(:foo, :extension => "bar").extension.should == "bar"
+ end
[:intern_method, :intern_multiple_method, :render_multiple_method, :render_method].each do |method|
it "should allow assignment of the #{method}" do
Puppet::Network::Format.new(:foo, method => :foo).send(method).should == :foo
diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb
index 110effe09..f379e5cce 100755
--- a/spec/unit/network/format_handler.rb
+++ b/spec/unit/network/format_handler.rb
@@ -204,6 +204,15 @@ describe Puppet::Network::FormatHandler do
Puppet::Network::FormatHandler.format(:by_name).should equal(format)
end
+ it "should be able to retrieve a format by extension" do
+ format = Puppet::Network::FormatHandler.create(:by_extension, :extension => "foo")
+ Puppet::Network::FormatHandler.format_by_extension("foo").should equal(format)
+ end
+
+ it "should return nil if asked to return a format by an unknown extension" do
+ Puppet::Network::FormatHandler.format_by_extension("yayness").should be_nil
+ end
+
it "should be able to retrieve formats by name irrespective of case and class" do
format = Puppet::Network::FormatHandler.create(:by_name)
Puppet::Network::FormatHandler.format(:By_Name).should equal(format)
diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb
index a241306a2..7b0c0adba 100755
--- a/spec/unit/network/formats.rb
+++ b/spec/unit/network/formats.rb
@@ -257,6 +257,10 @@ describe "Puppet Network Format" do
it "should have its mimetype set to text/plain" do
@text.mime.should == "text/plain"
end
+
+ it "should use 'txt' as its extension" do
+ @text.extension.should == "txt"
+ end
end
describe Puppet::Network::FormatHandler.format(:raw) do