From a4e735e133898a376747491d5bbc1ca5692ac122 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:30:52 -0700 Subject: (#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 --- lib/puppet/network/formats.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib') diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 4ca3240d4..082c83ee3 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 -- cgit From 5986e8a3747ebb0fe48169f88fecb481be76d16c Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:36:50 -0700 Subject: (#7353) Unify rendering in the face_bace application. We now move over to using only network FormatHandler rendering code for output, ditching all the support we had in the application. We include a compatibility shim to ensure that the :for_humans format that was supported for a while is now an alias for the :console format we are using moving forward. Reviewed-By: Jacob Helwig --- lib/puppet/application/face_base.rb | 63 +++++++++++-------------------------- 1 file changed, 19 insertions(+), 44 deletions(-) (limited to 'lib') diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb index 31e58aca4..8c1e03e3f 100644 --- a/lib/puppet/application/face_base.rb +++ b/lib/puppet/application/face_base.rb @@ -29,56 +29,31 @@ class Puppet::Application::FaceBase < Puppet::Application attr_accessor :face, :action, :type, :arguments, :render_as def render_as=(format) - if format == :for_humans or format == :json - @render_as = format - elsif network_format = Puppet::Network::FormatHandler.format(format) - method = network_format.render_method - if method == "to_pson" then - @render_as = :json - else - @render_as = method.to_sym - end - else - raise ArgumentError, "I don't know how to render '#{format}'" - end + @render_as = case format.to_sym + when :for_humans then + # We have an old alias name for :console, which went out in + # 2.7.0rc1, so we are going to carry it forward for a + # while. --daniel 2011-05-04 + Puppet::Network::FormatHandler.format(:console) + when :json then + Puppet::Network::FormatHandler.format(:pson) + else + Puppet::Network::FormatHandler.format(format) + end + @render_as or raise ArgumentError, "I don't know how to render '#{format}'" end def render(result) # Invoke the rendering hook supplied by the user, if appropriate. - if hook = action.when_rendering(render_as) then + if hook = action.when_rendering(render_as.name) + result = hook.call(result) + elsif render_as.name == :console and hook = action.when_rendering(:for_humans) + # We have an old alias name for :console, which went out in 2.7.0rc1, so + # we are going to carry it forward for a while. --daniel 2011-05-04 result = hook.call(result) end - if render_as == :for_humans then - render_for_humans(result) - elsif render_as == :json - PSON::pretty_generate(result, :allow_nan => true, :max_nesting => false) - else - result.send(render_as) - end - end - - def render_for_humans(result) - # String to String - return result if result.is_a? String - return result if result.is_a? Numeric - - # Simple hash to table - if result.is_a? Hash and result.keys.all? { |x| x.is_a? String or x.is_a? Numeric } - output = '' - column_a = result.map do |k,v| k.to_s.length end.max + 2 - column_b = 79 - column_a - result.sort_by { |k,v| k.to_s } .each do |key, value| - output << key.to_s.ljust(column_a) - output << PP.pp(value, '', column_b). - chomp.gsub(/\n */) { |x| x + (' ' * column_a) } - output << "\n" - end - return output - end - - # ...or pretty-print the inspect outcome. - return result.pretty_inspect + render_as.render(result) end def preinit @@ -204,7 +179,7 @@ class Puppet::Application::FaceBase < Puppet::Application @arguments << options # If we don't have a rendering format, set one early. - self.render_as ||= (@action.render_as || :for_humans) + self.render_as ||= (@action.render_as || :console) end -- cgit From efd118114bab228b5f8f4a437ead8be2cc895d8a Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:38:36 -0700 Subject: (#7353) Use :console rendering format in our own code. Now that we have unified things, stop using the compatibility name in favour of the new :console name for the output format. No functional effect beyond avoiding a deprecated output mode. Reviewed-By: Jacob Helwig --- lib/puppet/face/catalog/select.rb | 2 +- lib/puppet/face/plugin.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/puppet/face/catalog/select.rb b/lib/puppet/face/catalog/select.rb index a8ecd82fa..a68a8e0f3 100644 --- a/lib/puppet/face/catalog/select.rb +++ b/lib/puppet/face/catalog/select.rb @@ -15,7 +15,7 @@ Puppet::Face.define(:catalog, '0.0.1') do end end - when_rendering :for_humans do |value| + when_rendering :console do |value| if value.nil? then "no matching resources found" else diff --git a/lib/puppet/face/plugin.rb b/lib/puppet/face/plugin.rb index 4b45ed3a1..8a2559405 100644 --- a/lib/puppet/face/plugin.rb +++ b/lib/puppet/face/plugin.rb @@ -20,7 +20,7 @@ Puppet::Face.define(:plugin, '0.0.1') do Puppet[:pluginsignore]).evaluate end - when_rendering :for_humans do |value| + when_rendering :console do |value| if value.empty? then "No plugins downloaded." else -- cgit From dda32647b4c11ecb352e469088f2438835ff99d7 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:40:55 -0700 Subject: (#7353) Note the :for_humans compatibility issue. Where we need special support for :for_humans as an alias for :console, call it out in comments. This makes it clear to someone who wonders why what the actual underlying purpose of the whole thing is. Reviewed-By: Jacob Helwig --- lib/puppet/interface/action_builder.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb index 0bf4f1408..16305530a 100644 --- a/lib/puppet/interface/action_builder.rb +++ b/lib/puppet/interface/action_builder.rb @@ -38,6 +38,8 @@ class Puppet::Interface::ActionBuilder def render_as(value = nil) value.nil? and raise ArgumentError, "You must give a rendering format to render_as" + # :for_humans is a compatibility alias for :console, but since we shipped + # it in 2.7.0rc1 we need to support it ongoing. --daniel 2011-05-04 formats = Puppet::Network::FormatHandler.formats << :for_humans unless formats.include? value raise ArgumentError, "#{value.inspect} is not a valid rendering format: #{formats.sort.join(", ")}" -- cgit