[Pkg-puppet-devel] [facter] 48/61: maint: Refactor Key Value Output Parsers

Stig Sandbeck Mathisen ssm at debian.org
Mon Nov 4 15:02:03 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 26a7691eb7399d791e8a12b4efa1f55f14826ec9
Author: Rob Reynolds <ferventcoder at gmail.com>
Date:   Mon Aug 5 16:48:40 2013 -0500

    maint: Refactor Key Value Output Parsers
    
    This extracts parsing the output of a command to a common method shared between
    powershellparser, textparser and scriptparser and cleans up the methods to take
    advantage of the same parsing technique.
---
 lib/facter/util/parser.rb |   47 +++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 31 deletions(-)

diff --git a/lib/facter/util/parser.rb b/lib/facter/util/parser.rb
index 36f1358..4596c8f 100644
--- a/lib/facter/util/parser.rb
+++ b/lib/facter/util/parser.rb
@@ -66,6 +66,19 @@ module Facter::Util::Parser
     end
   end
 
+  module KeyValuePairOutputFormat
+    def self.parse(output)
+      result = {}
+      re = /^(.+?)=(.+)$/
+      output.each_line do |line|
+        if match_data = re.match(line.chomp)
+          result[match_data[1]] = match_data[2]
+        end
+      end
+      result
+    end
+  end
+
   class YamlParser < Base
     def parse_results
       YAML.load(content)
@@ -78,14 +91,7 @@ module Facter::Util::Parser
 
   class TextParser < Base
     def parse_results
-      re = /^(.+?)=(.+)$/
-      result = {}
-      content.each_line do |line|
-        if match_data = re.match(line.chomp)
-          result[match_data[1]] = match_data[2]
-        end
-      end
-      result
+      KeyValuePairOutputFormat.parse content
     end
   end
 
@@ -111,16 +117,7 @@ module Facter::Util::Parser
 
   class ScriptParser < Base
     def results
-      output = Facter::Util::Resolution.exec(filename)
-
-      result = {}
-      re = /^(.+)=(.+)$/
-      output.each_line do |line|
-        if match_data = re.match(line.chomp)
-          result[match_data[1]] = match_data[2]
-        end
-      end
-      result
+      KeyValuePairOutputFormat.parse Facter::Util::Resolution.exec(filename)
     end
   end
 
@@ -137,19 +134,7 @@ module Facter::Util::Parser
     # Returns a hash of facts from powershell output
     def results
       shell_command = "powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File \"#{filename}\""
-      output = Facter::Util::Resolution.exec(shell_command)
-
-      result = {}
-      output.split("\n").each do |line|
-        if line =~ /^(.+)=(.+)$/
-          result[$1] = $2
-        end
-      end
-
-      result
-    rescue Exception => e
-      Facter.warn("Failed to handle #{filename} as powershell facts: #{e.class}: #{e}")
-      Facter.debug(e.backtrace.join("\n\t"))
+      KeyValuePairOutputFormat.parse Facter::Util::Resolution.exec(shell_command)
     end
   end
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list