Support nested variables in varReplace.

This commit is contained in:
Jeroen Hoekx 2012-05-09 16:05:06 +02:00
parent 036b779188
commit 82144fd543
2 changed files with 171 additions and 3 deletions

View file

@ -198,7 +198,19 @@ def parse_json(data):
return { "failed" : True, "parsed" : False, "msg" : data }
return results
_KEYCRE = re.compile(r"\$(\w+)")
def varLookup(name, vars):
''' find the contents of a possibly complex variable in vars. '''
path = name.split('.')
space = vars
for part in path:
if part in space:
space = space[part]
else:
return
return space
_KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.]+|\w+))(?(complex)\})")
# if { -> complex if complex, allow . and need trailing }
def varReplace(raw, vars):
'''Perform variable replacement of $vars
@ -220,8 +232,9 @@ def varReplace(raw, vars):
# Determine replacement value (if unknown variable then preserve
# original)
varname = m.group(1).lower()
replacement = str(vars.get(varname, m.group()))
varname = m.group(2).lower()
replacement = str(varLookup(varname, vars) or m.group())
start, end = m.span()
done.append(raw[:start]) # Keep stuff leading up to token