class Puppet::Parser::TypeLoader

Public Class Methods

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

Public Instance Methods

environment() click to toggle source
   # File lib/puppet/parser/type_loader.rb
52 def environment
53   @environment
54 end
environment=(env) click to toggle source
   # File lib/puppet/parser/type_loader.rb
56 def environment=(env)
57   if env.is_a?(String) or env.is_a?(Symbol)
58     @environment = Puppet.lookup(:environments).get!(env)
59   else
60     @environment = env
61   end
62 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
18 def import(pattern, dir)
19   modname, files = Puppet::Parser::Files.find_manifests_in_modules(pattern, environment)
20   if files.empty?
21     abspat = File.expand_path(pattern, dir)
22     file_pattern = abspat + (File.extname(abspat).empty? ? '.pp' : '' )
23 
24     files = Dir.glob(file_pattern).uniq.reject { |f| FileTest.directory?(f) }
25     modname = nil
26 
27     if files.empty?
28       raise_no_files_found(pattern)
29     end
30   end
31 
32   load_files(modname, files)
33 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
37 def import_all
38   # And then load all files from each module, but (relying on system
39   # behavior) only load files from the first module of a given name.  E.g.,
40   # given first/foo and second/foo, only files from first/foo will be loaded.
41   environment.modules.each do |mod|
42     load_files(mod.name, mod.all_manifests)
43   end
44 end
parse_file(file) click to toggle source
   # File lib/puppet/parser/type_loader.rb
84 def parse_file(file)
85   Puppet.debug { "importing '#{file}' in environment #{environment}" }
86   parser = Puppet::Parser::ParserFactory.parser
87   parser.file = file
88   return parser.parse
89 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
65 def try_load_fqname(type, fqname)
66   return nil if fqname == "" # special-case main.
67   files_to_try_for(fqname).each do |filename|
68     begin
69       imported_types = import_from_modules(filename)
70       result = imported_types.find { |t| t.type == type and t.name == fqname }
71       if result
72         Puppet.debug {"Automatically imported #{fqname} from #{filename} into #{environment}"}
73         return result
74       end
75     rescue TypeLoaderError
76       # I'm not convinced we should just drop these errors, but this
77       # preserves existing behaviours.
78     end
79   end
80   # Nothing found.
81   return nil
82 end

Private Instance Methods

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