diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-28 17:00:14 +0100 |
---|---|---|
committer | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-07-11 18:34:20 +0100 |
commit | 0d9ae8bda93a3498a02c90a08b13b94e1ca9a151 (patch) | |
tree | 2f5353b27c8fe9b83c5f1d530382e5e797c34df2 | |
parent | 6edc8751210218a9c8c3deb2bd677f024292d7f2 (diff) | |
download | buildstream-0d9ae8bda93a3498a02c90a08b13b94e1ca9a151.tar.gz |
_yaml: Automatically represent Yaml nodes into yaml
This removes the need of calling _yaml.dump(), as roundtrip_dump
is now equivalent.
-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 |