diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-28 17:00:14 +0100 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2019-07-15 14:14:03 +0000 |
commit | 9e5facfab87268a80cb02f2de87897764a416c8a (patch) | |
tree | 0bdb1260eb1e03ad87d2f14cc9a8ce5bd7e201d2 /src/buildstream/_yaml.pyx | |
parent | 8521b5be47453e1937ef196bf60a9b05a18a23e3 (diff) | |
download | buildstream-9e5facfab87268a80cb02f2de87897764a416c8a.tar.gz |
_yaml: Automatically represent Yaml nodes into yaml
This removes the need of calling _yaml.dump(), as roundtrip_dump
is now equivalent.
Diffstat (limited to 'src/buildstream/_yaml.pyx')
-rw-r--r-- | src/buildstream/_yaml.pyx | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index 64f7101cd..365615b73 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -1407,6 +1407,22 @@ def assert_symbol_name(ProvenanceInformation provenance, str symbol_name, str pu # Roundtrip code +# Represent Nodes automatically + +def represent_mapping(self, MappingNode mapping): + return self.represent_dict(mapping.value) + +def represent_scalar(self, ScalarNode scalar): + return self.represent_str(scalar.value) + +def represent_sequence(self, SequenceNode sequence): + return self.represent_list(sequence.value) + + +yaml.RoundTripRepresenter.add_representer(MappingNode, represent_mapping) +yaml.RoundTripRepresenter.add_representer(ScalarNode, represent_scalar) +yaml.RoundTripRepresenter.add_representer(SequenceNode, represent_sequence) + # Represent simple types as strings def represent_as_str(self, value): @@ -1549,8 +1565,6 @@ def roundtrip_load_data(contents, *, filename=None): # file (any): The file to write to # def roundtrip_dump(contents, file=None): - assert type(contents) is not Node - with ExitStack() as stack: if type(file) is str: from . import utils |