tidy up value wrapping and rendering methods
diff --git a/reclass/datatypes/parameters.py b/reclass/datatypes/parameters.py
index 2a6368a..4b93b31 100644
--- a/reclass/datatypes/parameters.py
+++ b/reclass/datatypes/parameters.py
@@ -76,24 +76,21 @@
def as_dict(self):
return self._base.copy()
- def _itemise_list(self, item_list):
- for n, value in enumerate(item_list):
- if isinstance(value, dict):
- self._itemise_dict(value)
- elif isinstance(value, list):
- self._itemise_list(value)
- elif not isinstance(value, (Value, ValueList)):
- item_list[n] = Value(value, self._delimiter)
+ def _wrap_container(self, container, key, value):
+ if isinstance(value, dict):
+ self._wrap_dict(value)
+ elif isinstance(value, list):
+ self._wrap_list(value)
+ elif not isinstance(value, (Value, ValueList)):
+ container[key] = Value(value, self._delimiter)
- def _itemise_dict(self, dictionary):
+ def _wrap_list(self, item_list):
+ for n, value in enumerate(item_list):
+ self._wrap_container(item_list, n, value)
+
+ def _wrap_dict(self, dictionary):
for key, value in dictionary.iteritems():
- if isinstance(value, dict):
- self._itemise_dict(value)
- elif isinstance(value, list):
- self._itemise_list(value)
- dictionary[key] = Value(value, self._delimiter)
- elif not isinstance(value, (Value, ValueList)):
- dictionary[key] = Value(value, self._delimiter)
+ self._wrap_container(dictionary, key, value)
def _update_value(self, cur, new, path):
if cur is None:
@@ -203,9 +200,9 @@
"""
if isinstance(other, dict):
- itemised_other = copy.deepcopy(other)
- self._itemise_dict(itemised_other)
- self._base = self._merge_recurse(self._base, itemised_other,
+ wrapped = copy.deepcopy(other)
+ self._wrap_dict(wrapped)
+ self._base = self._merge_recurse(self._base, wrapped,
DictPath(self.delimiter), initmerge)
elif isinstance(other, self.__class__):
@@ -219,62 +216,45 @@
def has_unresolved_refs(self):
return len(self._unrendered) > 0
- def resolve_simple(self, options=None):
+ def render_simple(self, options=None):
if options is None:
options = MergeOptions()
- self._resolve_simple_recurse_dict(self._base, DictPath(self.delimiter), options)
+ self._render_simple_dict(self._base, DictPath(self.delimiter), options)
- def _resolve_simple_recurse_dict(self, dictionary, path, options):
+ def _render_simple_container(self, container, key, value, path, options):
+ if isinstance(value, ValueList):
+ if value.has_references():
+ self._unrendered[path.new_subpath(key)] = True
+ return
+ else:
+ value = value.merge(options)
+ if isinstance(value, Value) and value.is_container():
+ value = value.contents()
+ if isinstance(value, dict):
+ self._render_simple_dict(value, path.new_subpath(key), options)
+ container[key] = value
+ elif isinstance(value, list):
+ self._render_simple_list(value, path.new_subpath(key), options)
+ container[key] = value
+ elif isinstance(value, Value):
+ if value.has_references():
+ self._unrendered[path.new_subpath(key)] = True
+ else:
+ container[key] = value.render({}, options)
+
+ def _render_simple_dict(self, dictionary, path, options):
for key, value in dictionary.iteritems():
- if isinstance(value, ValueList):
- if value.has_references():
- self._unrendered[path.new_subpath(key)] = True
- continue
- else:
- value = value.merge(options)
- if isinstance(value, Value) and value.is_container():
- value = value.contents()
+ self._render_simple_container(dictionary, key, value, path, options)
- if isinstance(value, dict):
- self._resolve_simple_recurse_dict(value, path.new_subpath(key), options)
- dictionary[key] = value
- elif isinstance(value, list):
- self._resolve_simple_recurse_list(value, path.new_subpath(key), options)
- dictionary[key] = value
- elif isinstance(value, Value):
- if value.has_references():
- self._unrendered[path.new_subpath(key)] = True
- else:
- dictionary[key] = value.render({}, options)
-
- def _resolve_simple_recurse_list(self, item_list, path, options):
+ def _render_simple_list(self, item_list, path, options):
for n, value in enumerate(item_list):
- if isinstance(value, ValueList):
- if value.has_references():
- self._unrendered[path.new_subpath(n)] = True
- continue
- else:
- value = value.merge(options)
- if isinstance(value, Value) and value.is_container():
- value = value.contents()
-
- if isinstance(value, dict):
- self._resolve_simple_recurse_dict(value, path.new_subpath(n), options)
- item_list[n] = value
- elif isinstance(value, list):
- self._resolve_simple_recurse_list(value, path.new_subpath(n), options)
- item_list[n] = value
- elif isinstance(value, Value):
- if value.has_references():
- self._unrendered[path.new_subpath(n)] = True
- else:
- item_list[n] = value.render({}, options)
+ self._render_simple_container(item_list, n, value, path, options)
def interpolate(self, options=None):
if options is None:
options = MergeOptions()
self._unrendered = {}
- self.resolve_simple(options)
+ self.render_simple(options)
while self.has_unresolved_refs():
# we could use a view here, but this is simple enough:
# _interpolate_inner removes references from the refs hash after
@@ -304,10 +284,10 @@
try:
new = value.render(self._base, options)
if isinstance(new, dict):
- self._resolve_simple_recurse_dict(new, path, options)
+ self._render_simple_dict(new, path, options)
path.set_value(self._base, copy.deepcopy(new))
elif isinstance(new, list):
- self._resolve_simple_recurse_list(new, path, options)
+ self._render_simple_list(new, path, options)
path.set_value(self._base, copy.deepcopy(new))
else:
path.set_value(self._base, new)
diff --git a/reclass/datatypes/tests/test_parameters.py b/reclass/datatypes/tests/test_parameters.py
index 97fb924..3e2bd86 100644
--- a/reclass/datatypes/tests/test_parameters.py
+++ b/reclass/datatypes/tests/test_parameters.py
@@ -115,7 +115,7 @@
"""def test_get_dict(self):
p, b = self._construct_mocked_params(SIMPLE)
- p.resolve_simple()
+ p.render_simple()
self.assertDictEqual(p.as_dict(), SIMPLE)
def test_merge_scalars(self):
@@ -123,7 +123,7 @@
mergee = {'five':5,'four':4,'None':None,'tuple':(1,2,3)}
p2, b2 = self._construct_mocked_params(mergee)
p1.merge(p2)
- p1.resolve_simple()
+ p1.render_simple()
for key, value in mergee.iteritems():
# check that each key, value in mergee resulted in a get call and
# a __setitem__ call against b1 (the merge target)
@@ -135,7 +135,7 @@
p2 = Parameters({'b' : mock.sentinel.goal})
p1.merge(p2)
p1.interpolate()
- p2.resolve_simple()
+ p2.render_simple()
self.assertEqual(p1.as_dict()['b'], mock.sentinel.goal)"""
@@ -145,7 +145,7 @@
p = Parameters(SIMPLE)
mergee = {'five':5,'four':4,'None':None,'tuple':(1,2,3)}
p.merge(mergee)
- p.resolve_simple()
+ p.render_simple()
goal = SIMPLE.copy()
goal.update(mergee)
self.assertDictEqual(p.as_dict(), goal)
@@ -154,7 +154,7 @@
p = Parameters(SIMPLE)
mergee = {'two':5,'four':4,'three':None,'one':(1,2,3)}
p.merge(mergee)
- p.resolve_simple()
+ p.render_simple()
goal = SIMPLE.copy()
goal.update(mergee)
self.assertDictEqual(p.as_dict(), goal)
@@ -165,7 +165,7 @@
p1 = Parameters(dict(list=l1[:]))
p2 = Parameters(dict(list=l2))
p1.merge(p2)
- p1.resolve_simple()
+ p1.render_simple()
self.assertListEqual(p1.as_dict()['list'], l1+l2)
def test_merge_list_into_scalar(self):
@@ -174,7 +174,7 @@
options.allow_list_over_scalar = True
p1 = Parameters(dict(key=l[0]))
p1.merge(Parameters(dict(key=l[1:])))
- p1.resolve_simple(options)
+ p1.render_simple(options)
self.assertListEqual(p1.as_dict()['key'], l)
def test_merge_scalar_over_list(self):
@@ -183,14 +183,14 @@
options.allow_scalar_over_list = True
p1 = Parameters(dict(key=l[:2]))
p1.merge(Parameters(dict(key=l[2])))
- p1.resolve_simple(options)
+ p1.render_simple(options)
self.assertEqual(p1.as_dict()['key'], l[2])
def test_merge_dicts(self):
mergee = {'five':5,'four':4,'None':None,'tuple':(1,2,3)}
p = Parameters(dict(dict=SIMPLE))
p.merge(Parameters(dict(dict=mergee)))
- p.resolve_simple()
+ p.render_simple()
goal = SIMPLE.copy()
goal.update(mergee)
self.assertDictEqual(p.as_dict(), dict(dict=goal))
@@ -199,7 +199,7 @@
mergee = {'two':5,'four':4,'three':None,'one':(1,2,3)}
p = Parameters(dict(dict=SIMPLE))
p.merge(Parameters(dict(dict=mergee)))
- p.resolve_simple()
+ p.render_simple()
goal = SIMPLE.copy()
goal.update(mergee)
self.assertDictEqual(p.as_dict(), dict(dict=goal))
@@ -214,7 +214,7 @@
'two': ['gamma']}
p = Parameters(dict(dict=base))
p.merge(Parameters(dict(dict=mergee)))
- p.resolve_simple()
+ p.render_simple()
self.assertDictEqual(p.as_dict(), dict(dict=goal))
def test_merge_dict_into_scalar(self):
@@ -229,7 +229,7 @@
options = MergeOptions()
options.allow_scalar_over_dict = True
p.merge(Parameters(mergee))
- p.resolve_simple(options)
+ p.render_simple(options)
self.assertDictEqual(p.as_dict(), mergee)
def test_interpolate_single(self):