class Puppet::Pops::Evaluator::EppEvaluator

Handler of Epp call/evaluation from the epp and inline_epp functions

Public Class Methods

epp(scope, file, env_name, template_args = nil) click to toggle source
   # File lib/puppet/pops/evaluator/epp_evaluator.rb
26 def self.epp(scope, file, env_name, template_args = nil)
27   unless file.is_a?(String)
28     #TRANSLATORS 'epp()' is a method name and should not be translated
29     raise ArgumentError, _("epp(): the first argument must be a String with the filename, got a %{class_name}") % { class_name: file.class }
30   end
31 
32   unless Puppet::FileSystem.exist?(file)
33     unless file =~ /\.epp$/
34       file = file + ".epp"
35     end
36   end
37 
38   scope.debug "Retrieving epp template #{file}"
39   template_file = Puppet::Parser::Files.find_template(file, env_name)
40   if template_file.nil? ||  !Puppet::FileSystem.exist?(template_file)
41     raise Puppet::ParseError, _("Could not find template '%{file}'") % { file: file }
42   end
43 
44   # Parse and validate the source
45   parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new
46   begin
47     result = parser.parse_file(template_file)
48   rescue Puppet::ParseError => e
49     #TRANSLATORS 'epp()' is a method name and 'EPP' refers to 'Embedded Puppet (EPP) template' and should not be translated
50     raise ArgumentError, _("epp(): Invalid EPP: %{detail}") % { detail: e.message }
51   end
52 
53   # Evaluate (and check template_args)
54   evaluate(parser, 'epp', scope, true, result, template_args)
55 end
inline_epp(scope, epp_source, template_args = nil) click to toggle source
   # File lib/puppet/pops/evaluator/epp_evaluator.rb
 6 def self.inline_epp(scope, epp_source, template_args = nil)
 7   unless epp_source.is_a?(String)
 8     #TRANSLATORS 'inline_epp()' is a method name and 'epp' refers to 'Embedded Puppet (EPP) template' and should not be translated
 9     raise ArgumentError, _("inline_epp(): the first argument must be a String with the epp source text, got a %{class_name}") %
10         { class_name: epp_source.class }
11   end
12 
13   # Parse and validate the source
14   parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new
15   begin
16     result = parser.parse_string(epp_source, 'inlined-epp-text')
17   rescue Puppet::ParseError => e
18     #TRANSLATORS 'inline_epp()' is a method name and 'EPP' refers to 'Embedded Puppet (EPP) template' and should not be translated
19     raise ArgumentError, _("inline_epp(): Invalid EPP: %{detail}") % { detail: e.message }
20   end
21 
22   # Evaluate (and check template_args)
23   evaluate(parser, 'inline_epp', scope, false, result, template_args)
24 end

Private Class Methods

evaluate(parser, func_name, scope, use_global_scope_only, parse_result, template_args) click to toggle source
    # File lib/puppet/pops/evaluator/epp_evaluator.rb
 57 def self.evaluate(parser, func_name, scope, use_global_scope_only, parse_result, template_args)
 58   template_args, template_args_set = handle_template_args(func_name, template_args)
 59 
 60   body = parse_result.body
 61   unless body.is_a?(Puppet::Pops::Model::LambdaExpression)
 62     #TRANSLATORS 'LambdaExpression' is a class name and should not be translated
 63     raise ArgumentError, _("%{function_name}(): the parser did not produce a LambdaExpression, got '%{class_name}'") %
 64         { function_name: func_name, class_name: body.class }
 65   end
 66   unless body.body.is_a?(Puppet::Pops::Model::EppExpression)
 67     #TRANSLATORS 'EppExpression' is a class name and should not be translated
 68     raise ArgumentError, _("%{function_name}(): the parser did not produce an EppExpression, got '%{class_name}'") %
 69         { function_name: func_name, class_name: body.body.class }
 70   end
 71   unless parse_result.definitions.empty?
 72     #TRANSLATORS 'EPP' refers to 'Embedded Puppet (EPP) template'
 73     raise ArgumentError, _("%{function_name}(): The EPP template contains illegal expressions (definitions)") %
 74         { function_name: func_name }
 75   end
 76 
 77   parameters_specified = body.body.parameters_specified
 78   if parameters_specified || template_args_set
 79     enforce_parameters = parameters_specified
 80   else
 81     enforce_parameters = true
 82   end
 83 
 84   # filter out all qualified names and set them in qualified_variables
 85   # only pass unqualified (filtered) variable names to the the template
 86   filtered_args = {}
 87   template_args.each_pair do |k, v|
 88     if k =~ /::/
 89       k = k[2..-1] if k.start_with?('::')
 90       scope[k] = v
 91     else
 92       filtered_args[k] = v
 93     end
 94   end
 95   template_args = filtered_args
 96 
 97   # inline_epp() logic sees all local variables, epp() all global
 98   if use_global_scope_only
 99     scope.with_global_scope do |global_scope|
100       parser.closure(body, global_scope).call_by_name(template_args, enforce_parameters)
101     end
102   else
103     parser.closure(body, scope).call_by_name(template_args, enforce_parameters)
104   end
105 end
handle_template_args(func_name, template_args) click to toggle source
    # File lib/puppet/pops/evaluator/epp_evaluator.rb
108 def self.handle_template_args(func_name, template_args)
109   if template_args.nil?
110     [{}, false]
111   else
112     unless template_args.is_a?(Hash)
113       #TRANSLATORS 'template_args' is a variable name and should not be translated
114       raise ArgumentError, _("%{function_name}(): the template_args must be a Hash, got a %{class_name}") %
115           { function_name: func_name, class_name: template_args.class }
116     end
117     [template_args, true]
118   end
119 end