Class: Puppet::Parser::TypeLoader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/puppet/parser/type_loader.rb

Defined Under Namespace

Classes: TypeLoaderError

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TypeLoader

Returns a new instance of TypeLoader.



49
50
51
# File 'lib/puppet/parser/type_loader.rb', line 49

def initialize(env)
  self.environment = env
end

Instance Method Details

#environmentObject



53
54
55
# File 'lib/puppet/parser/type_loader.rb', line 53

def environment
  @environment
end

#environment=(env) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/puppet/parser/type_loader.rb', line 57

def environment=(env)
  if env.is_a?(String) or env.is_a?(Symbol)
    @environment = Puppet.lookup(:environments).get!(env)
  else
    @environment = env
  end
end

#import(pattern, dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Import manifest files that match a given file glob pattern.

Parameters:

  • pattern (String)

    the file glob to apply when determining which files to load

  • dir (String)

    base directory to use when the file is not found in a module



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/parser/type_loader.rb', line 19

def import(pattern, dir)
  modname, files = Puppet::Parser::Files.find_manifests_in_modules(pattern, environment)
  if files.empty?
    abspat = File.expand_path(pattern, dir)
    file_pattern = abspat + (File.extname(abspat).empty? ? '.pp' : '')

    files = Dir.glob(file_pattern).uniq.reject { |f| FileTest.directory?(f) }
    modname = nil

    if files.empty?
      raise_no_files_found(pattern)
    end
  end

  load_files(modname, files)
end

#import_allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load all of the manifest files in all known modules.



38
39
40
41
42
43
44
45
# File 'lib/puppet/parser/type_loader.rb', line 38

def import_all
  # And then load all files from each module, but (relying on system
  # behavior) only load files from the first module of a given name.  E.g.,
  # given first/foo and second/foo, only files from first/foo will be loaded.
  environment.modules.each do |mod|
    load_files(mod.name, mod.all_manifests)
  end
end

#parse_file(file) ⇒ Object



84
85
86
87
88
89
# File 'lib/puppet/parser/type_loader.rb', line 84

def parse_file(file)
  Puppet.debug { "importing '#{file}' in environment #{environment}" }
  parser = Puppet::Parser::ParserFactory.parser
  parser.file = file
  parser.parse
end

#try_load_fqname(type, fqname) ⇒ Object

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet/parser/type_loader.rb', line 66

def try_load_fqname(type, fqname)
  return nil if fqname == "" # special-case main.

  files_to_try_for(fqname).each do |filename|
    imported_types = import_from_modules(filename)
    result = imported_types.find { |t| t.type == type and t.name == fqname }
    if result
      Puppet.debug { "Automatically imported #{fqname} from #{filename} into #{environment}" }
      return result
    end
  rescue TypeLoaderError
    # I'm not convinced we should just drop these errors, but this
    # preserves existing behaviours.
  end
  # Nothing found.
  nil
end