[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5
Daniel Pittman
daniel at puppetlabs.com
Tue May 10 08:19:30 UTC 2011
The following commit has been merged in the experimental branch:
commit a4e735e133898a376747491d5bbc1ca5692ac122
Author: Daniel Pittman <daniel at puppetlabs.com>
Date: Wed May 4 10:30:52 2011 -0700
(#7353) Add 'console' format to FormatHandler
This adds a console rendering format to the Network FormatHandler subsystem;
it provides the same human-friendly textual rendering as the Faces application
did, except it uses JSON rather than PP as the fall-back rendering mode.
This paves the path for unification of all formatting into the same subsystem,
rather than the half-measures we used to have.
Reviewed-By: Jacob Helwig <jacob at puppetlabs.com>
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index 4ca3240..082c83e 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -160,3 +160,39 @@ end
# This is really only ever going to be used for Catalogs.
Puppet::Network::FormatHandler.create_serialized_formats(:dot, :required_methods => [:render_method])
+
+
+Puppet::Network::FormatHandler.create(:console,
+ :mime => 'text/x-console-text',
+ :weight => 0) do
+ def json
+ @json ||= Puppet::Network::FormatHandler.format(:pson)
+ end
+
+ def render(datum)
+ # String to String
+ return datum if datum.is_a? String
+ return datum if datum.is_a? Numeric
+
+ # Simple hash to table
+ if datum.is_a? Hash and datum.keys.all? { |x| x.is_a? String or x.is_a? Numeric }
+ output = ''
+ column_a = datum.map do |k,v| k.to_s.length end.max + 2
+ column_b = 79 - column_a
+ datum.sort_by { |k,v| k.to_s } .each do |key, value|
+ output << key.to_s.ljust(column_a)
+ output << json.render(value).
+ chomp.gsub(/\n */) { |x| x + (' ' * column_a) }
+ output << "\n"
+ end
+ return output
+ end
+
+ # ...or pretty-print the inspect outcome.
+ return json.render(datum)
+ end
+
+ def render_multiple(data)
+ data.collect(&:render).join("\n")
+ end
+end
diff --git a/spec/unit/network/formats_spec.rb b/spec/unit/network/formats_spec.rb
index 72d3551..62c2dbb 100755
--- a/spec/unit/network/formats_spec.rb
+++ b/spec/unit/network/formats_spec.rb
@@ -330,4 +330,66 @@ describe "Puppet Network Format" do
end
end
end
+
+ describe ":console format" do
+ subject { Puppet::Network::FormatHandler.format(:console) }
+ it { should be_an_instance_of Puppet::Network::Format }
+ let :json do Puppet::Network::FormatHandler.format(:pson) end
+
+ [:intern, :intern_multiple].each do |method|
+ it "should not implement #{method}" do
+ expect { subject.send(method, String, 'blah') }.to raise_error NotImplementedError
+ end
+ end
+
+ ["hello", 1, 1.0].each do |input|
+ it "should just return a #{input.inspect}" do
+ subject.render(input).should == input
+ end
+ end
+
+ [[1, 2], ["one"], [{ 1 => 1 }]].each do |input|
+ it "should render #{input.inspect} as JSON" do
+ subject.render(input).should == json.render(input).chomp
+ end
+ end
+
+ it "should render a non-trivially-keyed Hash as JSON" do
+ hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 }
+ subject.render(hash).should == json.render(hash).chomp
+ end
+
+ it "should render a {String,Numeric}-keyed Hash into a table" do
+ object = Object.new
+ hash = { "one" => 1, "two" => [], "three" => {}, "four" => object,
+ 5 => 5, 6.0 => 6 }
+
+ # Gotta love ASCII-betical sort order. Hope your objects are better
+ # structured for display than my test one is. --daniel 2011-04-18
+ subject.render(hash).should == <<EOT
+5 5
+6.0 6
+four #{json.render(object).chomp}
+one 1
+three {}
+two []
+EOT
+ end
+
+ it "should render a hash nicely with a multi-line value" do
+ pending "Moving to PSON rather than PP makes this unsupportable."
+ hash = {
+ "number" => { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 },
+ "text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 }
+ }
+ subject.render(hash).should == <<EOT
+number {"1"=>"1111111111111111111111111111111111111111",
+ "2"=>"2222222222222222222222222222222222222222",
+ "3"=>"3333333333333333333333333333333333333333"}
+text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
+ "c"=>"cccccccccccccccccccccccccccccccccccccccc"}
+EOT
+ end
+ end
end
--
Puppet packaging for Debian
More information about the Pkg-puppet-devel
mailing list