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 25 def self.epp(scope, file, env_name, template_args = nil) 26 unless file.is_a?(String) 27 #TRANSLATORS 'epp()' is a method name and should not be translated 28 raise ArgumentError, _("epp(): the first argument must be a String with the filename, got a %{class_name}") % { class_name: file.class } 29 end 30 31 unless Puppet::FileSystem.exist?(file) 32 unless file =~ /\.epp$/ 33 file = file + ".epp" 34 end 35 end 36 37 scope.debug "Retrieving epp template #{file}" 38 template_file = Puppet::Parser::Files.find_template(file, env_name) 39 if template_file.nil? || !Puppet::FileSystem.exist?(template_file) 40 raise Puppet::ParseError, _("Could not find template '%{file}'") % { file: file } 41 end 42 43 # Parse and validate the source 44 parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new 45 begin 46 result = parser.parse_file(template_file) 47 rescue Puppet::ParseError => e 48 #TRANSLATORS 'epp()' is a method name and 'EPP' refers to 'Embedded Puppet (EPP) template' and should not be translated 49 raise ArgumentError, _("epp(): Invalid EPP: %{detail}") % { detail: e.message } 50 end 51 52 # Evaluate (and check template_args) 53 evaluate(parser, 'epp', scope, true, result, template_args) 54 end
inline_epp(scope, epp_source, template_args = nil)
click to toggle source
# File lib/puppet/pops/evaluator/epp_evaluator.rb 5 def self.inline_epp(scope, epp_source, template_args = nil) 6 unless epp_source.is_a?(String) 7 #TRANSLATORS 'inline_epp()' is a method name and 'epp' refers to 'Embedded Puppet (EPP) template' and should not be translated 8 raise ArgumentError, _("inline_epp(): the first argument must be a String with the epp source text, got a %{class_name}") % 9 { class_name: epp_source.class } 10 end 11 12 # Parse and validate the source 13 parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new 14 begin 15 result = parser.parse_string(epp_source, 'inlined-epp-text') 16 rescue Puppet::ParseError => e 17 #TRANSLATORS 'inline_epp()' is a method name and 'EPP' refers to 'Embedded Puppet (EPP) template' and should not be translated 18 raise ArgumentError, _("inline_epp(): Invalid EPP: %{detail}") % { detail: e.message } 19 end 20 21 # Evaluate (and check template_args) 22 evaluate(parser, 'inline_epp', scope, false, result, template_args) 23 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 56 def self.evaluate(parser, func_name, scope, use_global_scope_only, parse_result, template_args) 57 template_args, template_args_set = handle_template_args(func_name, template_args) 58 59 body = parse_result.body 60 unless body.is_a?(Puppet::Pops::Model::LambdaExpression) 61 #TRANSLATORS 'LambdaExpression' is a class name and should not be translated 62 raise ArgumentError, _("%{function_name}(): the parser did not produce a LambdaExpression, got '%{class_name}'") % 63 { function_name: func_name, class_name: body.class } 64 end 65 unless body.body.is_a?(Puppet::Pops::Model::EppExpression) 66 #TRANSLATORS 'EppExpression' is a class name and should not be translated 67 raise ArgumentError, _("%{function_name}(): the parser did not produce an EppExpression, got '%{class_name}'") % 68 { function_name: func_name, class_name: body.body.class } 69 end 70 unless parse_result.definitions.empty? 71 #TRANSLATORS 'EPP' refers to 'Embedded Puppet (EPP) template' 72 raise ArgumentError, _("%{function_name}(): The EPP template contains illegal expressions (definitions)") % 73 { function_name: func_name } 74 end 75 76 parameters_specified = body.body.parameters_specified 77 if parameters_specified || template_args_set 78 enforce_parameters = parameters_specified 79 else 80 enforce_parameters = true 81 end 82 83 # filter out all qualified names and set them in qualified_variables 84 # only pass unqualified (filtered) variable names to the the template 85 filtered_args = {} 86 template_args.each_pair do |k, v| 87 if k =~ /::/ 88 k = k[2..-1] if k.start_with?('::') 89 scope[k] = v 90 else 91 filtered_args[k] = v 92 end 93 end 94 template_args = filtered_args 95 96 # inline_epp() logic sees all local variables, epp() all global 97 if use_global_scope_only 98 scope.with_global_scope do |global_scope| 99 parser.closure(body, global_scope).call_by_name(template_args, enforce_parameters) 100 end 101 else 102 parser.closure(body, scope).call_by_name(template_args, enforce_parameters) 103 end 104 end
handle_template_args(func_name, template_args)
click to toggle source
# File lib/puppet/pops/evaluator/epp_evaluator.rb 107 def self.handle_template_args(func_name, template_args) 108 if template_args.nil? 109 [{}, false] 110 else 111 unless template_args.is_a?(Hash) 112 #TRANSLATORS 'template_args' is a variable name and should not be translated 113 raise ArgumentError, _("%{function_name}(): the template_args must be a Hash, got a %{class_name}") % 114 { function_name: func_name, class_name: template_args.class } 115 end 116 [template_args, true] 117 end 118 end