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 _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