summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-06-28 17:00:14 +0100
committerBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-01 22:38:46 +0100
commitd96c1a232b0cd41558e75aac10dc3badb9361594 (patch)
tree8caee16da8610a51d034d964efd61d26de651113
parent75296d85b20fa4a7ca894de729a990e3420ce16e (diff)
downloadbuildstream-d96c1a232b0cd41558e75aac10dc3badb9361594.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.pyx18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx
index acda87f26..52a700235 100644
--- a/src/buildstream/_yaml.pyx
+++ b/src/buildstream/_yaml.pyx
@@ -1410,6 +1410,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):
@@ -1552,8 +1568,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