def _update_value(self, cur, new): if isinstance(cur, Value): values = ValueList(cur, self._settings) elif isinstance(cur, ValueList): values = cur else: if isinstance(cur, (ParameterDict, ParameterList)): uri = cur.uri else: uri = self._uri values = ValueList(Value(cur, self._settings, uri), self._settings) if isinstance(new, Value): values.append(new) elif isinstance(new, ValueList): values.extend(new) else: if isinstance(new, (ParameterDict, ParameterList)): uri = new.uri else: uri = self._uri values.append( Value(new, self._settings, uri, parse_string=self._parse_strings)) return values
def test_render_multiple_items(self): val1 = Value('${foo}', SETTINGS, '') val2 = Value('${bar}', SETTINGS, '') composite = CompItem([val1, val2], SETTINGS) self.assertEquals('12', composite.render({'foo': 1, 'bar': 2}, None))
def test_merge_over_merge_composite(self): val1 = Value(None, SETTINGS, '') val2 = Value(None, SETTINGS, '') composite1 = CompItem([val1], SETTINGS) composite2 = CompItem([val2], SETTINGS) result = composite2.merge_over(composite1) self.assertEquals(result, composite2)
def test_assembleRefs_two_items_one_with_one_ref_one_without(self): val1 = Value('${foo}bar', SETTINGS, '') val2 = Value('baz', SETTINGS, '') expected_refs = ['foo'] composite = CompItem([val1, val2], SETTINGS) self.assertTrue(composite.has_references) self.assertEquals(composite.get_references(), expected_refs)
def test_assembleRefs_two_items_with_two_refs(self): val1 = Value('${foo}${baz}', SETTINGS, '') val2 = Value('${bar}${meep}', SETTINGS, '') expected_refs = ['foo', 'baz', 'bar', 'meep'] composite = CompItem([val1, val2], SETTINGS) self.assertTrue(composite.has_references) self.assertEquals(composite.get_references(), expected_refs)
def _merge_dict(self, cur, new, path): """Merge a dictionary with another dictionary. Iterate over keys in new. If this is not an initialization merge and the key begins with PARAMETER_DICT_KEY_OVERRIDE_PREFIX, override the value of the key in cur. Otherwise deeply merge the contents of the key in cur with the contents of the key in _merge_recurse over the item. Args: cur (dict): Current dictionary new (dict): Dictionary to be merged path (string): Merging path from recursion initmerge (bool): True if called as part of entity init Returns: dict: a merged dictionary """ ret = cur for (key, newvalue) in iteritems(new): if key.startswith(self._settings.dict_key_override_prefix ) and not self._keep_overrides: if not isinstance(newvalue, Value): newvalue = Value(newvalue, self._settings, self._uri, parse_string=self._parse_strings) newvalue.overwrite = True ret[key.lstrip( self._settings.dict_key_override_prefix)] = newvalue else: ret[key] = self._merge_recurse(ret.get(key), newvalue, path.new_subpath(key)) return ret
def _update_value(self, cur, new, path): if isinstance(cur, Value): values = ValueList(cur) elif isinstance(cur, ValueList): values = cur else: values = ValueList(Value(cur)) if isinstance(new, Value): values.append(new) elif isinstance(new, ValueList): values.extend(new) else: values.append(Value(new)) return values
def test_merge_over_merge_composite(self): scalar1 = CompItem(Value(1, SETTINGS, ''), SETTINGS) scalar2 = ScaItem([2], SETTINGS) result = scalar2.merge_over(scalar1) self.assertEquals(result.contents, scalar2.contents)
def _update_value(self, cur, new): if isinstance(cur, Value): values = ValueList(cur, self._settings) elif isinstance(cur, ValueList): values = cur else: values = ValueList(Value(cur, self._settings, self._uri), self._settings) if isinstance(new, Value): values.append(new) elif isinstance(new, ValueList): values.extend(new) else: values.append(Value(new, self._settings, self._uri)) return values
def test_string_representation(self): composite = CompItem(Value(1, SETTINGS, ''), SETTINGS) expected = '1' result = str(composite) self.assertEquals(result, expected)
def test_single_subst_start(self): s = _var('favcolour') + ' is my favourite colour' tv = Value(s, SETTINGS, '') self.assertTrue(tv.has_references) self.assertEqual( tv.render(CONTEXT, None), _poor_mans_template(s, 'favcolour', CONTEXT['favcolour']))
def test_single_subst_end(self): s = 'I like ' + _var('favcolour') tv = Value(s, SETTINGS, '') self.assertTrue(tv.has_references) self.assertEqual( tv.render(CONTEXT, None), _poor_mans_template(s, 'favcolour', CONTEXT['favcolour']))
def test_single_subst_bothends(self): s = 'I like ' + _var('favcolour') + ' and I like it' tv = Value(s) self.assertTrue(tv.has_references()) self.assertEqual( tv.render(CONTEXT, None), _poor_mans_template(s, 'favcolour', CONTEXT['favcolour']))
def test_deep_subst_solo(self): var = PARAMETER_INTERPOLATION_DELIMITER.join(('motd', 'greeting')) s = _var(var) tv = Value(s) self.assertTrue(tv.has_references()) self.assertEqual( tv.render(CONTEXT, None), _poor_mans_template(s, var, CONTEXT['motd']['greeting']))
def test_multiple_subst_flush(self): greet = PARAMETER_INTERPOLATION_DELIMITER.join(('motd', 'greeting')) s = _var(greet) + ' I like ' + _var('favcolour') tv = Value(s) self.assertTrue(tv.has_references()) want = _poor_mans_template(s, greet, CONTEXT['motd']['greeting']) want = _poor_mans_template(want, 'favcolour', CONTEXT['favcolour']) self.assertEqual(tv.render(CONTEXT, None), want)
def test_merge_over_merge_scalar(self): val1 = Value(None, SETTINGS, '') scalar = ScaItem(1, SETTINGS) composite = CompItem([val1], SETTINGS) result = composite.merge_over(scalar) self.assertEquals(result, composite)
def test_multiple_subst_flush(self): greet = SETTINGS.delimiter.join(('motd', 'greeting')) s = _var(greet) + ' I like ' + _var('favcolour') tv = Value(s, SETTINGS, '') self.assertTrue(tv.has_references) want = _poor_mans_template(s, greet, CONTEXT['motd']['greeting']) want = _poor_mans_template(want, 'favcolour', CONTEXT['favcolour']) self.assertEqual(tv.render(CONTEXT, None), want)
def test_assembleRefs_one_item_with_two_refs(self): val1 = Value('${foo}${bar}', SETTINGS, '') expected_refs = ['foo', 'bar'] composite = CompItem([val1], SETTINGS) self.assertTrue(composite.has_references) self.assertEquals(composite.get_references(), expected_refs)
def test_deep_subst_solo(self): motd = SETTINGS.delimiter.join(('motd', 'greeting')) s = _var(motd) tv = Value(s, SETTINGS, '') self.assertTrue(tv.has_references) self.assertEqual( tv.render(CONTEXT, None), _poor_mans_template(s, motd, CONTEXT['motd']['greeting']))
def _wrap_value(self, value): if isinstance(value, dict): return self._wrap_dict(value) elif isinstance(value, list): return self._wrap_list(value) elif isinstance(value, (Value, ValueList)): return value else: return Value(value, self._delimiter)
def _wrap_value(self, value, path): if isinstance(value, dict): return self._wrap_dict(value, path) elif isinstance(value, list): return self._wrap_list(value, path) elif isinstance(value, (Value, ValueList)): return value else: try: return Value(value, self._settings, self._uri) except InterpolationError as e: e.context = str(path) raise
def _wrap_value(self, value): if isinstance(value, (Value, ValueList)): return value elif isinstance(value, dict): return self._wrap_dict(value) elif isinstance(value, list): return self._wrap_list(value) else: try: return Value(value, self._settings, self._uri, parse_string=self._parse_strings) except InterpolationError as e: e.context = DictPath(self._settings.delimiter) raise
def _merge_dict(self, cur, new): """Merge a dictionary with another dictionary. Iterate over keys in new. If this is not an initialization merge and the key begins with PARAMETER_DICT_KEY_OVERRIDE_PREFIX, override the value of the key in cur. Otherwise deeply merge the contents of the key in cur with the contents of the key in _merge_recurse over the item. Args: cur (dict): Current dictionary new (dict): Dictionary to be merged initmerge (bool): True if called as part of entity init Returns: dict: a merged dictionary """ for (key, value) in iteritems(new): # check key for "control" preffixes (~,=,...) key = str(key) if key[0] in self._settings.dict_key_prefixes: newkey = key[1:] if not isinstance(value, Value): value = Value(value, self._settings, self._uri, parse_string=self._parse_strings) if key[0] == self._settings.dict_key_override_prefix: value.overwrite = True elif key[0] == self._settings.dict_key_constant_prefix: value.constant = True value = self._merge_recurse(cur.get(newkey), value) key = newkey else: value = self._merge_recurse(cur.get(key), value) cur[key] = value cur.uri = new.uri return cur
def test_incomplete_variable(self): s = REFERENCE_SENTINELS[0] + 'incomplete' with self.assertRaises(ParseError): tv = Value(s)
def _test_solo_ref(self, key): s = _var(key) tv = Value(s, SETTINGS, '') res = tv.render(CONTEXT, None) self.assertTrue(tv.has_references) self.assertEqual(res, CONTEXT[key])
def test_simple_string(self): s = 'my cat likes to hide in boxes' tv = Value(s, SETTINGS, '') self.assertFalse(tv.has_references) self.assertEquals(tv.render(CONTEXT, None), s)
def test_incomplete_variable(self): s = SETTINGS.reference_sentinels[0] + 'incomplete' with self.assertRaises(ParseError): tv = Value(s, SETTINGS, '')
def test_undefined_variable(self): s = _var('no_such_variable') tv = Value(s, SETTINGS, '') with self.assertRaises(ResolveError): tv.render(CONTEXT, None)
def test_render_single_item(self): val1 = Value('${foo}', SETTINGS, '') composite = CompItem([val1], SETTINGS) self.assertEquals(1, composite.render({'foo': 1}, None))
def test_undefined_variable(self): s = _var('no_such_variable') tv = Value(s) with self.assertRaises(UndefinedVariableError): tv.render(CONTEXT, None)