diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-28 16:40:29 +0100 |
---|---|---|
committer | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-07-01 22:38:42 +0100 |
commit | 75296d85b20fa4a7ca894de729a990e3420ce16e (patch) | |
tree | 90091ccc77a7c008359623cc9140e9883c107405 | |
parent | 160f0a59ebe853aa75103e06a679fce1e224f741 (diff) | |
download | buildstream-75296d85b20fa4a7ca894de729a990e3420ce16e.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.pyx | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index 10ed76df4..acda87f26 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 @@ -1410,6 +1410,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, @@ -1542,31 +1554,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 |