[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 5986e8a3747ebb0fe48169f88fecb481be76d16c
Author: Daniel Pittman <daniel at puppetlabs.com>
Date:   Wed May 4 10:36:50 2011 -0700

    (#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 <jacob at puppetlabs.com>

diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb
index 31e58ac..8c1e03e 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
 
 
diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb
index 133e6b2..25797ea 100755
--- a/spec/unit/application/face_base_spec.rb
+++ b/spec/unit/application/face_base_spec.rb
@@ -246,14 +246,14 @@ describe Puppet::Application::FaceBase do
       end
 
       [[1, 2], ["one"], [{ 1 => 1 }]].each do |input|
-        it "should render #{input.class} using the 'pp' library" do
-          app.render(input).should == input.pretty_inspect
+        it "should render #{input.class} using JSON" do
+          app.render(input).should == input.to_pson.chomp
         end
       end
 
-      it "should render a non-trivially-keyed Hash with the 'pp' library" do
+      it "should render a non-trivially-keyed Hash with using JSON" do
         hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 }
-        app.render(hash).should == hash.pretty_inspect
+        app.render(hash).should == hash.to_pson.chomp
       end
 
       it "should render a {String,Numeric}-keyed Hash into a table" do
@@ -266,7 +266,7 @@ describe Puppet::Application::FaceBase do
         app.render(hash).should == <<EOT
 5      5
 6.0    6
-four   #{object.pretty_inspect.chomp}
+four   #{object.to_pson.chomp}
 one    1
 three  {}
 two    []
@@ -274,6 +274,7 @@ 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 }
@@ -289,7 +290,7 @@ EOT
       end
 
       it "should invoke the action rendering hook while rendering" do
-        app.action.set_rendering_method_for(:for_humans, proc { |value| "bi-winning!" })
+        app.action.set_rendering_method_for(:console, proc { |value| "bi-winning!" })
         app.render("bi-polar?").should == "bi-winning!"
       end
 
@@ -328,4 +329,15 @@ EOT
       }.to have_printed(/you invoked the 's' rendering hook/)
     end
   end
+
+  describe "#render_as=" do
+    # This is for compatibility with the format name in 2.7.0rc1, but isn't a
+    # long term desirable name.  We can't just randomly drop it since it was
+    # released, though, so it will live for a couple of major versions.
+    # --daniel 2011-05-04
+    it "should treat :for_humans as a synonym for :console" do
+      app.render_as = :for_humans
+      app.render_as.name.should == :console
+    end
+  end
 end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list