summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-06-28 16:40:29 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2019-07-15 07:40:32 +0000
commitf855689a55ba543b995a4ee1f91dbbf0d8a0dbf1 (patch)
tree64caae5c1ce70dca58fdb42b0c43b2dc6261670d
parentf9933f7e1f4bd57bebc08f9faccf3e91ad1ef6f0 (diff)
downloadbuildstream-f855689a55ba543b995a4ee1f91dbbf0d8a0dbf1.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