class Puppet::Parser::TypeLoader

Public Class Methods

new(env) click to toggle source
   # File lib/puppet/parser/type_loader.rb
47 def initialize(env)
48   self.environment = env
49 end

Public Instance Methods

environment() click to toggle source
   # File lib/puppet/parser/type_loader.rb
51 def environment
52   @environment
53 end
environment=(env) click to toggle source
   # File lib/puppet/parser/type_loader.rb
55 def environment=(env)
56   if env.is_a?(String) or env.is_a?(Symbol)
57     @environment = Puppet.lookup(:environments).get!(env)
58   else
59     @environment = env
60   end
61 end
import(pattern, dir) click to toggle source

Import manifest files that match a given file glob pattern.

@param pattern [String] the file glob to apply when determining which files

to load

@param dir [String] base directory to use when the file is not

found in a module

@api private

   # File lib/puppet/parser/type_loader.rb
17 def import(pattern, dir)
18   modname, files = Puppet::Parser::Files.find_manifests_in_modules(pattern, environment)
19   if files.empty?
20     abspat = File.expand_path(pattern, dir)
21     file_pattern = abspat + (File.extname(abspat).empty? ? '.pp' : '' )
22 
23     files = Dir.glob(file_pattern).uniq.reject { |f| FileTest.directory?(f) }
24     modname = nil
25 
26     if files.empty?
27       raise_no_files_found(pattern)
28     end
29   end
30 
31   load_files(modname, files)
32 end
import_all() click to toggle source

Load all of the manifest files in all known modules. @api private

   # File lib/puppet/parser/type_loader.rb
36 def import_all
37   # And then load all files from each module, but (relying on system
38   # behavior) only load files from the first module of a given name.  E.g.,
39   # given first/foo and second/foo, only files from first/foo will be loaded.
40   environment.modules.each do |mod|
41     load_files(mod.name, mod.all_manifests)
42   end
43 end
parse_file(file) click to toggle source
   # File lib/puppet/parser/type_loader.rb
83 def parse_file(file)
84   Puppet.debug { "importing '#{file}' in environment #{environment}" }
85   parser = Puppet::Parser::ParserFactory.parser
86   parser.file = file
87   return parser.parse
88 end
try_load_fqname(type, fqname) click to toggle source

Try to load the object with the given fully qualified name.

   # File lib/puppet/parser/type_loader.rb
64 def try_load_fqname(type, fqname)
65   return nil if fqname == "" # special-case main.
66   files_to_try_for(fqname).each do |filename|
67     begin
68       imported_types = import_from_modules(filename)
69       result = imported_types.find { |t| t.type == type and t.name == fqname }
70       if result
71         Puppet.debug {"Automatically imported #{fqname} from #{filename} into #{environment}"}
72         return result
73       end
74     rescue TypeLoaderError
75       # I'm not convinced we should just drop these errors, but this
76       # preserves existing behaviours.
77     end
78   end
79   # Nothing found.
80   return nil
81 end

Private Instance Methods

add_path_for_name(paths, name) click to toggle source
    # File lib/puppet/parser/type_loader.rb
143 def add_path_for_name(paths, name)
144   if paths.empty?
145     [name]
146   else
147     paths.unshift(File.join(paths.first, name))
148   end
149 end
files_to_try_for(qualified_name) click to toggle source

Return a list of all file basenames that should be tried in order to load the object with the given fully qualified name.

    # File lib/puppet/parser/type_loader.rb
137 def files_to_try_for(qualified_name)
138   qualified_name.split('::').inject([]) do |paths, name|
139     add_path_for_name(paths, name)
140   end
141 end
import_from_modules(pattern) click to toggle source
   # File lib/puppet/parser/type_loader.rb
92 def import_from_modules(pattern)
93   modname, files = Puppet::Parser::Files.find_manifests_in_modules(pattern, environment)
94   if files.empty?
95     raise_no_files_found(pattern)
96   end
97 
98   load_files(modname, files)
99 end
load_files(modname, files) click to toggle source
    # File lib/puppet/parser/type_loader.rb
105 def load_files(modname, files)
106   @loaded ||= {}
107   loaded_asts = []
108   files.reject { |file| @loaded[file] }.each do |file|
109   # The squelch_parse_errors use case is for parsing for the purpose of searching
110   # for information and it should not abort.
111   # There is currently one user in indirector/resourcetype/parser
112   #
113   if Puppet.lookup(:squelch_parse_errors) {|| false }
114     begin
115       loaded_asts << parse_file(file)
116     rescue => e
117       # Resume from errors so that all parseable files may
118       # still be parsed. Mark this file as loaded so that
119       # it would not be parsed next time (handle it as if
120       # it was successfully parsed).
121       Puppet.debug { "Unable to parse '#{file}': #{e.message}" }
122     end
123   else
124     loaded_asts << parse_file(file)
125   end
126 
127   @loaded[file] = true
128   end
129 
130   loaded_asts.collect do |ast|
131     known_resource_types.import_ast(ast, modname)
132   end.flatten
133 end
raise_no_files_found(pattern) click to toggle source
    # File lib/puppet/parser/type_loader.rb
101 def raise_no_files_found(pattern)
102   raise TypeLoaderError, _("No file(s) found for import of '%{pattern}'") % { pattern: pattern }
103 end