summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-06-28 16:40:29 +0100
committerBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-09 16:55:54 +0100
commit4662a9faf9329b0c6e73b1de7413d5b009b79a85 (patch)
treea444b8412f6c7a73432c8007416b24218c8e3961
parent18937bf6419a01e5ca8420858abd8fd79c5413c5 (diff)
downloadbuildstream-4662a9faf9329b0c6e73b1de7413d5b009b79a85.tar.gz
_yaml: Stop stringifying manually in roundtrip_dump
This removes the need of manually stringifying the values in roundtrip_dump by registering special Ruamel representer for each value we might expect.
-rw-r--r--src/buildstream/_yaml.pyx41
1 files changed, 14 insertions, 27 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx
index 82262f60b..64f7101cd 100644
--- a/src/buildstream/_yaml.pyx
+++ b/src/buildstream/_yaml.pyx
@@ -21,12 +21,12 @@
# James Ennis <james.ennis@codethink.co.uk>
# Benjamin Schubert <bschubert@bloomberg.net>
+import datetime
import sys
import string
from contextlib import ExitStack
from collections import OrderedDict
-from collections.abc import Mapping, Sequence
-from copy import deepcopy
+from collections.abc import Mapping
from ruamel import yaml
@@ -1407,6 +1407,18 @@ def assert_symbol_name(ProvenanceInformation provenance, str symbol_name, str pu
# Roundtrip code
+# Represent simple types as strings
+
+def represent_as_str(self, value):
+ return self.represent_str(str(value))
+
+yaml.RoundTripRepresenter.add_representer(type(None), represent_as_str)
+yaml.RoundTripRepresenter.add_representer(int, represent_as_str)
+yaml.RoundTripRepresenter.add_representer(float, represent_as_str)
+yaml.RoundTripRepresenter.add_representer(bool, represent_as_str)
+yaml.RoundTripRepresenter.add_representer(datetime.datetime, represent_as_str)
+yaml.RoundTripRepresenter.add_representer(datetime.date, represent_as_str)
+
# Always represent things consistently:
yaml.RoundTripRepresenter.add_representer(OrderedDict,
@@ -1539,31 +1551,6 @@ def roundtrip_load_data(contents, *, filename=None):
def roundtrip_dump(contents, file=None):
assert type(contents) is not Node
- def stringify_dict(thing):
- for k, v in thing.items():
- if type(v) is str:
- pass
- elif isinstance(v, Mapping):
- stringify_dict(v)
- elif isinstance(v, Sequence):
- stringify_list(v)
- else:
- thing[k] = str(v)
-
- def stringify_list(thing):
- for i, v in enumerate(thing):
- if type(v) is str:
- pass
- elif isinstance(v, Mapping):
- stringify_dict(v)
- elif isinstance(v, Sequence):
- stringify_list(v)
- else:
- thing[i] = str(v)
-
- contents = deepcopy(contents)
- stringify_dict(contents)
-
with ExitStack() as stack:
if type(file) is str:
from . import utils