Skip to content

Commit

Permalink
Access metadata vars as locals in python snippets
Browse files Browse the repository at this point in the history
Example:
FOO = "bar"
BAR = "${@foo + '/baz'}"

${BAR} == "bar/baz"

Signed-off-by: Chris Larson <chris_larson@mentor.com>
  • Loading branch information
kergoth committed Oct 6, 2010
1 parent 2321a59 commit 49cf79b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/traverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,26 @@ def generic_visit(self, node):
else:
return node


class MetadataMapping(dict):
def __init__(self, metadata, *args, **kwargs):
self.metadata = metadata
dict.__init__(self, *args, **kwargs)

def __missing__(self, key):
value = self.metadata.getVar(key, True)
if value is None:
raise KeyError(key)
return value

class Resolver(Transformer):
"""Convert a bbvalue tree into a string, optionally resolving
variable references"""

def __init__(self, metadata, crossref=True):
self.metadata = metadata
self.mapping = MetadataMapping(self.metadata)
self.mapping["d"] = metadata
self.crossref = crossref
super(Resolver, self).__init__()

Expand Down Expand Up @@ -126,8 +140,9 @@ def visit_VariableRef(self, node):
def visit_PythonValue(self, node):
code = self.generic_visit(node)
codeobj = compile(code.strip(), "<expansion>", "eval")

try:
value = str(utils.better_eval(codeobj, {"d": self.metadata}))
value = str(utils.better_eval(codeobj, self.mapping))
except Exception, exc:
raise PythonExpansionError(exc, self)
return self.visit(bbvalue.bbparse(value))
Expand Down

0 comments on commit 49cf79b

Please sign in to comment.