Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broker: pass archetype to the object profile #89

Open
wants to merge 2 commits into
base: upstream
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/aquilon/worker/templates/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def body(self, lines):

lines.append("")
dbos = self.dbobj.operating_system
pan_assign(lines, "system/archetype/name", self.dbobj.archetype.name)
pan_assign(lines, "system/archetype/os", dbos.name)
pan_assign(lines, "system/archetype/model", dbos.version)
pan_assign(lines, "system/archetype/os_lifecycle", dbos.lifecycle)
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 false if 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