From 2d3475b97f40f20df4f188751c4e26b70c2f1b3e Mon Sep 17 00:00:00 2001 From: Jeffrey Townsend Date: Thu, 8 Sep 2016 15:43:34 +0000 Subject: [PATCH] Enhance variable processing. Variable elements are now allowed to be dicts or arbitrary lists of dicts. --- tools/onlyaml.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/onlyaml.py b/tools/onlyaml.py index 5d1c038d..513292cf 100755 --- a/tools/onlyaml.py +++ b/tools/onlyaml.py @@ -21,6 +21,18 @@ class OnlYamlError(Exception): return self.value +def dflatten(rv, in_): + if type(in_) is dict: + rv.update(in_) + return rv + elif type(in_) is list: + for e in in_: + dflatten(rv, e) + return rv + else: + raise OnlYamlError("Element type '%s' cannot be added to the given dictionary." % (type(in_))) + + def loadf(fname, vard={}): # Apply variable interpolation: @@ -95,9 +107,10 @@ def loadf(fname, vard={}): raise OnlYamlError("%s\n(filename: %s)" % (e, fname)) if type(data) is dict: - variables.update(data.get('variables', {})) + _v = dflatten({}, data.get('variables', {})) + variables.update(_v) - for (k,v) in data.get('variables', {}).iteritems(): + for (k,v) in _v.iteritems(): k = interpolate(k, variables) v = interpolate(v, variables) variables[k] = v