Skip to content

Commit

Permalink
Allow to define a variable based on another variable
Browse files Browse the repository at this point in the history
- Do not replace the variable used as the value by its actual value
- Define initial LOADPATH as the ARCHETYPE variable

Change-Id: I314e4a23d284054ff0fbad43fc3e114692e0af5e
  • Loading branch information
Aquilon main user authored and jouvin committed May 24, 2018
1 parent b75b46f commit b5e6a3e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/aquilon/worker/templates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,10 @@ def _generate_content(self):
lines.append("")

if self.loadpath(self.dbobj):
pan_variable(lines, "ARCHETYPE", self.loadpath(self.dbobj))
pan_variable(lines, "LOADPATH", [self.loadpath(self.dbobj)])
archetype_var_name = "ARCHETYPE"
pan_variable(lines, archetype_var_name, self.loadpath(self.dbobj))
# Define LOADPATH based on ARCHETYPE variabble
pan_variable(lines, "LOADPATH", [archetype_var_name], quoted=False)
lines.append("")
pan_assign(lines, "/metadata/template/branch/name",
self.dbobj.branch.name)
Expand Down
36 changes: 24 additions & 12 deletions lib/aquilon/worker/templates/panutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@
_valid_id = re.compile(r"^[a-zA-Z_][\w.+\-]*$")


def pan(obj, indent=0):
"""pan(OBJ) -- return a string representing OBJ in the PAN language"""
def pan(obj, indent=0, quoted=True):
"""
pan(OBJ) -- return a string representing OBJ in the PAN language
:param quoted: if true, a string value is quoted. Can be set to fale is the value is
a variable name.
"""

spaces = " " * (indent + 1)
accumulator = list()

if isinstance(obj, string_types):
quote = '"'
if '"' in obj:
quote = "'"
if quoted:
quote = '"'
if '"' in obj:
quote = "'"
else:
quote = ''
accumulator.append("%s%s%s" % (quote, obj, quote))

elif isinstance(obj, bool):
Expand All @@ -56,22 +64,22 @@ def pan(obj, indent=0):
# Enforce a deterministic order to avoid recompilations due to change in
# ordering. This also helps with the testsuite.
for key in sorted(obj):
val = pan(obj[key], indent + 1)
val = pan(obj[key], indent + 1, quoted)
if isinstance(key, string_types):
if not _valid_id.match(str(key)): # pragma: no cover
raise ValueError("Invalid nlist key '%s'." % key)
else: # pragma: no cover
raise TypeError("The value of an nlist key must be a string, "
"optionally escaped (it was: %r)" % key)
accumulator.append("%s%s, %s," % (spaces, pan(key), val))
accumulator.append("%s%s, %s," % (spaces, pan(key, quoted), val))
# remove the last comma
accumulator[-1] = accumulator[-1].rstrip(",")
accumulator.append("%s)" % (" " * indent))

elif isinstance(obj, Iterable):
accumulator.append("list(")
for item in obj:
val = pan(item, indent + 1)
val = pan(item, indent + 1, quoted)
accumulator.append("%s%s," % (spaces, val))
# remove the last comma
accumulator[-1] = accumulator[-1].rstrip(",")
Expand All @@ -81,7 +89,7 @@ def pan(obj, indent=0):
accumulator.append("null")

else:
accumulator.append(pan(str(obj)))
accumulator.append(pan(str(obj), quoted))

if len(accumulator) == 1:
return accumulator[0]
Expand Down Expand Up @@ -142,11 +150,15 @@ def pan_include_if_exists(lines, templates):
lines.extend('include { if_exists("%s") };' % tpl for tpl in templates)


def pan_variable(lines, variable, value, final=False):
def pan_variable(lines, variable, value, final=False, quoted=True):
"""
:param final: mark the variable as final if true
:param quoted: quotes the value if true (can be set to false if the value is a variable name)
"""
if final:
lines.append('final variable %s = %s;' % (variable, pan(value)))
lines.append('final variable %s = %s;' % (variable, pan(value, quoted=quoted)))
else:
lines.append('variable %s = %s;' % (variable, pan(value)))
lines.append('variable %s = %s;' % (variable, pan(value, quoted=quoted)))


class PanObject(object):
Expand Down

0 comments on commit b5e6a3e

Please sign in to comment.