diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-04-07 00:51:34 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-04-07 00:51:34 +0900 |
commit | b21fc705ec6472187cb2320726a7f3e28019c9f2 (patch) | |
tree | 56fa3e9caeeb51bd1d55e500819add069462cb87 /buildstream/_yaml.py | |
parent | 1136eac663be9757c097426f96c6fca478a534c9 (diff) | |
download | buildstream-b21fc705ec6472187cb2320726a7f3e28019c9f2.tar.gz |
Untangling _yaml and utils modules.
These had a circular import, which is only supported > python 3.5
but undesirable anyway.
Diffstat (limited to 'buildstream/_yaml.py')
-rw-r--r-- | buildstream/_yaml.py | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index c74d401ff..70f782c12 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -24,7 +24,6 @@ from enum import Enum from ruamel import yaml from . import ImplError, LoadError, LoadErrorReason -from . import utils # We store information in the loaded yaml on a DictProvenance @@ -463,7 +462,7 @@ def composite_dict(target, source, policy=CompositePolicy.OVERWRITE, typesafe=Fa # Ensure target has only copies of mutable source values if (isinstance(target_value, list) and isinstance(source_value, list)): - target[key] = utils._list_chain_copy(source_value) + target[key] = list_chain_copy(source_value) else: target[key] = source_value @@ -473,7 +472,7 @@ def composite_dict(target, source, policy=CompositePolicy.OVERWRITE, typesafe=Fa isinstance(source_value, list)): # Ensure target has only copies of mutable source values - target[key] += utils._list_chain_copy(source_value) + target[key] += list_chain_copy(source_value) # Append element provenances from source list to target target_list_provenance = target_provenance.members[key] @@ -514,3 +513,57 @@ def composite(target, source, policy=CompositePolicy.OVERWRITE, typesafe=False): e.expected_type.__name__, e.path, e.actual_type.__name__)) from e + + +# node_sanitize() +# +# Returnes an alphabetically ordered recursive copy +# of the source node with internal provenance information stripped. +# +# Only dicts are ordered, list elements are left in order. +# +def node_sanitize(node): + + if isinstance(node, collections.Mapping): + + result = collections.OrderedDict() + + for key in sorted(node): + if key == PROVENANCE_KEY: + continue + result[key] = node_sanitize(node[key]) + + return result + + elif isinstance(node, list): + return [node_sanitize(elt) for elt in node] + + return node + + +def node_chain_copy(source): + copy = collections.ChainMap({}, source) + for key, value in source.items(): + if isinstance(value, collections.Mapping): + copy[key] = node_chain_copy(value) + elif isinstance(value, list): + copy[key] = list_chain_copy(value) + elif isinstance(value, Provenance): + copy[key] = value.clone() + + return copy + + +def list_chain_copy(source): + copy = [] + for item in source: + if isinstance(item, collections.Mapping): + copy.append(node_chain_copy(item)) + elif isinstance(item, list): + copy.append(list_chain_copy(item)) + elif isinstance(item, Provenance): + copy.append(item.clone()) + else: + copy.append(item) + + return copy |