diff options
| author | Luke Kanies <luke@madstop.com> | 2009-06-04 00:33:25 -0500 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-07-05 18:47:08 +1000 |
| commit | cddc365e9bac786c7a64240073b69bc54b6d2f2e (patch) | |
| tree | 3a68f908af4320e7957a2d27fd24d49565c0000f /lib/puppet | |
| parent | fc1f8cdbee606da0d2a1a162942295d28cdcbf64 (diff) | |
| download | puppet-cddc365e9bac786c7a64240073b69bc54b6d2f2e.tar.gz puppet-cddc365e9bac786c7a64240073b69bc54b6d2f2e.tar.xz puppet-cddc365e9bac786c7a64240073b69bc54b6d2f2e.zip | |
Switching to LoadedCode from ASTSet
I also took the opportunity to clean up and simplify
the interface to the parts of the parser that interact
with this. Mostly it was method renames.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/parser/ast/definition.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/parser/ast/node.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/parser/ast/resource_reference.rb | 4 | ||||
| -rw-r--r-- | lib/puppet/parser/compiler.rb | 10 | ||||
| -rw-r--r-- | lib/puppet/parser/functions/defined.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/parser/parser_support.rb | 111 | ||||
| -rw-r--r-- | lib/puppet/parser/resource/reference.rb | 8 | ||||
| -rw-r--r-- | lib/puppet/parser/scope.rb | 12 |
8 files changed, 69 insertions, 82 deletions
diff --git a/lib/puppet/parser/ast/definition.rb b/lib/puppet/parser/ast/definition.rb index 3cd8e79c7..092afef46 100644 --- a/lib/puppet/parser/ast/definition.rb +++ b/lib/puppet/parser/ast/definition.rb @@ -80,7 +80,7 @@ class Puppet::Parser::AST::Definition < Puppet::Parser::AST::Branch end def find_parentclass - @parser.findclass(namespace, parentclass) + @parser.find_hostclass(namespace, parentclass) end # Set our parent class, with a little check to avoid some potential diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb index 442518f44..faa7fa19a 100644 --- a/lib/puppet/parser/ast/node.rb +++ b/lib/puppet/parser/ast/node.rb @@ -33,6 +33,6 @@ class Puppet::Parser::AST::Node < Puppet::Parser::AST::HostClass # Search for the object matching our parent class. def find_parentclass - @parser.findnode(parentclass) + @parser.node(parentclass) end end diff --git a/lib/puppet/parser/ast/resource_reference.rb b/lib/puppet/parser/ast/resource_reference.rb index 5a521494f..6189ab8de 100644 --- a/lib/puppet/parser/ast/resource_reference.rb +++ b/lib/puppet/parser/ast/resource_reference.rb @@ -43,7 +43,7 @@ class Puppet::Parser::AST # Look up a fully qualified class name. def qualified_class(scope, title) # Look up the full path to the class - if classobj = scope.findclass(title) + if classobj = scope.find_hostclass(title) title = classobj.classname else raise Puppet::ParseError, "Could not find class %s" % title @@ -56,7 +56,7 @@ class Puppet::Parser::AST # We want a lower-case type. For some reason. objtype = @type.downcase unless builtintype?(objtype) - if dtype = scope.finddefine(objtype) + if dtype = scope.find_definition(objtype) objtype = dtype.classname else raise Puppet::ParseError, "Could not find resource type %s" % objtype diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb index 968e0b979..826e85a4a 100644 --- a/lib/puppet/parser/compiler.rb +++ b/lib/puppet/parser/compiler.rb @@ -44,7 +44,7 @@ class Puppet::Parser::Compiler # Do we use nodes found in the code, vs. the external node sources? def ast_nodes? - parser.nodes.length > 0 + parser.nodes? end # Store the fact that we've evaluated a class, and store a reference to @@ -133,7 +133,7 @@ class Puppet::Parser::Compiler found = [] classes.each do |name| # If we can find the class, then make a resource that will evaluate it. - if klass = scope.findclass(name) + if klass = scope.find_hostclass(name) found << name and next if class_scope(klass) resource = klass.evaluate(scope) @@ -217,10 +217,10 @@ class Puppet::Parser::Compiler # Now see if we can find the node. astnode = nil @node.names.each do |name| - break if astnode = @parser.nodes[name.to_s.downcase] + break if astnode = @parser.node(name.to_s.downcase) end - unless (astnode ||= @parser.nodes["default"]) + unless (astnode ||= @parser.node("default")) raise Puppet::ParseError, "Could not find default node or by name with '%s'" % node.names.join(", ") end @@ -298,7 +298,7 @@ class Puppet::Parser::Compiler # Find and evaluate our main object, if possible. def evaluate_main - @main = @parser.findclass("", "") || @parser.newclass("") + @main = @parser.find_hostclass("", "") || @parser.newclass("") @topscope.source = @main @main_resource = Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => @topscope, :source => @main) @topscope.resource = @main_resource diff --git a/lib/puppet/parser/functions/defined.rb b/lib/puppet/parser/functions/defined.rb index b25368ccc..5ad74a79e 100644 --- a/lib/puppet/parser/functions/defined.rb +++ b/lib/puppet/parser/functions/defined.rb @@ -10,7 +10,7 @@ Puppet::Parser::Functions::newfunction(:defined, :type => :rvalue, :doc => "Dete case val when String # For some reason, it doesn't want me to return from here. - if Puppet::Type.type(val) or finddefine(val) or findclass(val) + if Puppet::Type.type(val) or find_definition(val) or find_hostclass(val) result = true break end diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb index 1d81e6099..a7a980a0c 100644 --- a/lib/puppet/parser/parser_support.rb +++ b/lib/puppet/parser/parser_support.rb @@ -3,16 +3,7 @@ class Puppet::Parser::Parser require 'puppet/parser/functions' require 'puppet/parser/files' - - ASTSet = Struct.new(:classes, :definitions, :nodes) - - # Define an accessor method for each table. We hide the existence of - # the struct. - [:classes, :definitions, :nodes].each do |name| - define_method(name) do - @astset.send(name) - end - end + require 'puppet/parser/loaded_code' AST = Puppet::Parser::AST @@ -106,56 +97,51 @@ class Puppet::Parser::Parser end end - # Find a class definition, relative to the current namespace. - def findclass(namespace, name) - fqfind namespace, name, classes + [:hostclass, :definition, :node, :nodes?].each do |method| + define_method(method) do |*args| + @loaded_code.send(method, *args) + end end - # Find a component definition, relative to the current namespace. - def finddefine(namespace, name) - fqfind namespace, name, definitions + def find_hostclass(namespace, name) + find_or_load(namespace, name, :hostclass) end - # This is only used when nodes are looking up the code for their - # parent nodes. - def findnode(name) - fqfind "", name, nodes + def find_definition(namespace, name) + find_or_load(namespace, name, :definition) end - # The recursive method used to actually look these objects up. - def fqfind(namespace, name, table) - namespace = namespace.downcase - name = name.to_s.downcase + def find_or_load(namespace, name, type) + method = "find_" + type.to_s + if result = @loaded_code.send(method, namespace, name) + return result + end + + fullname = (namespace + "::" + name).sub(/^::/, '') + self.load(fullname) - # If our classname is fully qualified or we have no namespace, - # just try directly for the class, and return either way. - if name =~ /^::/ or namespace == "" - classname = name.sub(/^::/, '') - self.load(classname) unless table[classname] - return table[classname] + if result = @loaded_code.send(method, namespace, name) + return result end - # Else, build our namespace up piece by piece, checking - # for the class in each namespace. - ary = namespace.split("::") + # Try to load the module init file if we're a qualified + # name + if fullname.include?("::") + module_name = fullname.split("::")[0] - while ary.length > 0 - newname = (ary + [name]).join("::").sub(/^::/, '') - if obj = table[newname] or (self.load(newname) and obj = table[newname]) - return obj - end + self.load(module_name) - # Delete the second to last object, which reduces our namespace by one. - ary.pop + if result = @loaded_code.send(method, namespace, name) + return result + end end - # If we've gotten to this point without finding it, see if the name - # exists at the top namespace - if obj = table[name] or (self.load(name) and obj = table[name]) - return obj - end + # Now try to load the bare name on its own. This is + # appropriate if the class we're looking for is in a + # module that's different from our namespace. + self.load(name) - return nil + @loaded_code.send(method, namespace, name) end # Import our files. @@ -191,7 +177,7 @@ class Puppet::Parser::Parser end files.collect { |file| - parser = Puppet::Parser::Parser.new(:astset => @astset, :environment => @environment) + parser = Puppet::Parser::Parser.new(:loaded_code => @loaded_code, :environment => @environment) parser.files = self.files Puppet.debug("importing '%s'" % file) @@ -211,7 +197,7 @@ class Puppet::Parser::Parser end def initialize(options = {}) - @astset = options[:astset] || ASTSet.new({}, {}, {}) + @loaded_code = options[:loaded_code] || Puppet::Parser::LoadedCode.new @environment = options[:environment] initvars() end @@ -242,7 +228,7 @@ class Puppet::Parser::Parser # We don't know whether we're looking for a class or definition, so we have # to test for both. - return true if classes.include?(classname) || definitions.include?(classname) + return true if @loaded_code.hostclass(classname) || @loaded_code.definition(classname) unless @loaded.include?(filename) @loaded << filename @@ -256,7 +242,7 @@ class Puppet::Parser::Parser end # We don't know whether we're looking for a class or definition, so we have # to test for both. - return classes.include?(classname) || definitions.include?(classname) + return true if @loaded_code.hostclass(classname) || @loaded_code.definition(classname) end # Split an fq name into a namespace and name @@ -271,7 +257,7 @@ class Puppet::Parser::Parser def newclass(name, options = {}) name = name.downcase - if definitions.include?(name) + if @loaded_code.definition(name) raise Puppet::ParseError, "Cannot redefine class %s as a definition" % name end code = options[:code] @@ -279,7 +265,7 @@ class Puppet::Parser::Parser doc = options[:doc] # If the class is already defined, then add code to it. - if other = @astset.classes[name] + if other = @loaded_code.hostclass(name) || @loaded_code.definition(name) # Make sure the parents match if parent and other.parentclass and (parent != other.parentclass) error("Class %s is already defined at %s:%s; cannot redefine" % [name, other.file, other.line]) @@ -325,21 +311,21 @@ class Puppet::Parser::Parser args[:parentclass] = parent if parent args[:doc] = doc - @astset.classes[name] = ast AST::HostClass, args + @loaded_code.add_hostclass(name, ast(AST::HostClass, args)) end - return @astset.classes[name] + return @loaded_code.hostclass(name) end # Create a new definition. def newdefine(name, options = {}) name = name.downcase - if @astset.classes.include?(name) + if @loaded_code.hostclass(name) raise Puppet::ParseError, "Cannot redefine class %s as a definition" % name end # Make sure our definition doesn't already exist - if other = @astset.definitions[name] + if other = @loaded_code.definition(name) error("%s is already defined at %s:%s; cannot redefine" % [name, other.file, other.line]) end @@ -357,7 +343,7 @@ class Puppet::Parser::Parser args[param] = options[param] if options[param] end - @astset.definitions[name] = ast AST::Definition, args + @loaded_code.add_definition(name, ast(AST::Definition, args)) end # Create a new node. Nodes are special, because they're stored in a global @@ -367,7 +353,7 @@ class Puppet::Parser::Parser doc = lexer.getcomment names.collect do |name| name = name.to_s.downcase - if other = @astset.nodes[name] + if other = @loaded_code.node(name) error("Node %s is already defined at %s:%s; cannot redefine" % [other.name, other.file, other.line]) end name = name.to_s if name.is_a?(Symbol) @@ -382,9 +368,10 @@ class Puppet::Parser::Parser if options[:parent] args[:parentclass] = options[:parent] end - @astset.nodes[name] = ast(AST::Node, args) - @astset.nodes[name].classname = name - @astset.nodes[name] + node = ast(AST::Node, args) + node.classname = name + @loaded_code.add_node(name, node) + node end end @@ -448,7 +435,7 @@ class Puppet::Parser::Parser newclass("", :code => main) end @version = Time.now.to_i - return @astset + return @loaded_code ensure @lexer.clear end diff --git a/lib/puppet/parser/resource/reference.rb b/lib/puppet/parser/resource/reference.rb index cacd0707e..3ef981258 100644 --- a/lib/puppet/parser/resource/reference.rb +++ b/lib/puppet/parser/resource/reference.rb @@ -40,20 +40,20 @@ class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference case self.type when "Class" # look for host classes if self.title == :main - tmp = @scope.findclass("") + tmp = @scope.find_hostclass("") else - unless tmp = @scope.parser.classes[self.title] + unless tmp = @scope.parser.hostclass(self.title) fail Puppet::ParseError, "Could not find class '%s'" % self.title end end when "Node" # look for node definitions - unless tmp = @scope.parser.nodes[self.title] + unless tmp = @scope.parser.node(self.title) fail Puppet::ParseError, "Could not find node '%s'" % self.title end else # normal definitions # The resource type is capitalized, so we have to downcase. Really, # we should have a better interface for finding these, but eh. - tmp = @scope.parser.definitions[self.type.downcase] + tmp = @scope.parser.definition(self.type.downcase) end if tmp diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index bf34d2e29..be4dc12ed 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -82,18 +82,18 @@ class Puppet::Parser::Scope @level == 1 end - def findclass(name) + def find_hostclass(name) @namespaces.each do |namespace| - if r = parser.findclass(namespace, name) + if r = parser.find_hostclass(namespace, name) return r end end return nil end - def finddefine(name) + def find_definition(name) @namespaces.each do |namespace| - if r = parser.finddefine(namespace, name) + if r = parser.find_definition(namespace, name) return r end end @@ -165,14 +165,14 @@ class Puppet::Parser::Scope # Look up a defined type. def lookuptype(name) - finddefine(name) || findclass(name) + find_definition(name) || find_hostclass(name) end def lookup_qualified_var(name, usestring) parts = name.split(/::/) shortname = parts.pop klassname = parts.join("::") - klass = findclass(klassname) + klass = find_hostclass(klassname) unless klass warning "Could not look up qualified variable '%s'; class %s could not be found" % [name, klassname] return usestring ? "" : :undefined |
