class Puppet::InfoService::ClassInformationService

Public Class Methods

new() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
 8 def initialize
 9   @file_to_result = {}
10   @parser = Puppet::Pops::Parser::EvaluatingParser.new()
11 end

Public Instance Methods

classes_per_environment(env_file_hash) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
13 def classes_per_environment(env_file_hash)
14   # In this version of puppet there is only one way to parse manifests, as feature switches per environment
15   # are added or removed, this logic needs to change to compute the result per environment with the correct
16   # feature flags in effect.
17 
18   unless env_file_hash.is_a?(Hash)
19     raise ArgumentError, _('Given argument must be a Hash')
20   end
21 
22   result = {}
23 
24   # for each environment
25   #   for each file
26   #     if file already processed, use last result or error
27   #
28   env_file_hash.each do |env, files|
29     env_result = result[env] = {}
30     files.each do |f|
31       env_result[f] = result_of(f)
32     end
33   end
34   result
35 end

Private Instance Methods

extract_default(structure, p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
82 def extract_default(structure, p)
83   value_expr = p.value
84   return structure if value_expr.nil?
85   default_value = value_as_literal(value_expr)
86   structure[:default_literal] = default_value unless default_value.nil?
87   structure[:default_source] = extract_value_source(value_expr)
88   structure
89 end
extract_param(p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
72 def extract_param(p)
73   extract_default(extract_type({:name => p.name}, p), p)
74 end
extract_type(structure, p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
76 def extract_type(structure, p)
77   return structure if p.type_expr.nil?
78   structure[:type] = typeexpr_to_string(p.type_expr)
79   structure
80 end
extract_value_source(value_expr) click to toggle source

Extracts the source for the expression

    # File lib/puppet/info_service/class_information_service.rb
108 def extract_value_source(value_expr)
109   value_expr.locator.extract_tree_text(value_expr)
110 end
literal_evaluator() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
43 def literal_evaluator
44   @@literal_evaluator ||= Puppet::Pops::Evaluator::JsonStrictLiteralEvaluator.new
45 end
parse_file(f) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
55 def parse_file(f)
56   return {:error => _("The file %{f} does not exist") % { f: f }} unless Puppet::FileSystem.exist?(f)
57 
58   begin
59     parse_result = @parser.parse_file(f)
60     {:classes =>
61       parse_result.definitions.select {|d| d.is_a?(Puppet::Pops::Model::HostClassDefinition)}.map do |d|
62         {:name   => d.name,
63          :params => d.parameters.map {|p| extract_param(p) }
64         }
65       end
66     }
67   rescue StandardError => e
68     {:error => e.message }
69   end
70 end
result_of(f) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
47 def result_of(f)
48   entry =  @file_to_result[f]
49   if entry.nil?
50     @file_to_result[f] = entry = parse_file(f)
51   end
52   entry
53 end
type_parser() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
39 def type_parser
40   Puppet::Pops::Types::TypeParser.singleton
41 end
typeexpr_to_string(type_expr) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
91 def typeexpr_to_string(type_expr)
92   begin
93     type_parser.interpret_any(type_expr, nil).to_s
94   rescue Puppet::ParseError
95     # type is to complex - contains expressions that are not literal
96     nil
97   end
98 end
value_as_literal(value_expr) click to toggle source
    # File lib/puppet/info_service/class_information_service.rb
100 def value_as_literal(value_expr)
101   catch(:not_literal) do
102     return literal_evaluator.literal(value_expr)
103   end
104   nil
105 end