From 1ffcd0242eb6840a14cf51d7808ac7aa8166b40c Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 10 Apr 2019 13:11:23 +0000 Subject: [PATCH] Add some helpers to projectconfig_ruamellib So that it's easier to use in our scripts, add some helper sugar. First, add functions load and dump so that the library can be used similarly to yaml without the need to construct an object first. Then, remove the strip parameter and detect whether we're dumping a list or not. That way people won't forget to pass or not pass strip. Change-Id: I204af8a89c37f36f0480de3a2e669b65354eb73c --- tools/normalize_channels_yaml.py | 3 +-- tools/normalize_projects_yaml.py | 3 +-- tools/projectconfig_ruamellib.py | 23 ++++++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/tools/normalize_channels_yaml.py b/tools/normalize_channels_yaml.py index 714b07794d..1c986affb7 100755 --- a/tools/normalize_channels_yaml.py +++ b/tools/normalize_channels_yaml.py @@ -17,11 +17,10 @@ import locale import sys -import projectconfig_ruamellib +import projectconfig_ruamellib as yaml def main(): - yaml = projectconfig_ruamellib.YAML(strip=False) locale.setlocale(locale.LC_COLLATE, 'C') chandata = yaml.load(open('gerritbot/channels.yaml')) diff --git a/tools/normalize_projects_yaml.py b/tools/normalize_projects_yaml.py index f33f3affd0..2651b3a875 100755 --- a/tools/normalize_projects_yaml.py +++ b/tools/normalize_projects_yaml.py @@ -15,12 +15,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import projectconfig_ruamellib +import projectconfig_ruamellib as yaml def main(): - yaml = projectconfig_ruamellib.YAML() data = yaml.load(open('gerrit/projects.yaml')) for project in data: diff --git a/tools/projectconfig_ruamellib.py b/tools/projectconfig_ruamellib.py index c246fabc14..ade88d257e 100755 --- a/tools/projectconfig_ruamellib.py +++ b/tools/projectconfig_ruamellib.py @@ -21,18 +21,12 @@ def none_representer(dumper, data): class YAML(object): - def __init__(self, strip=True): - """Wrap construction of ruamel yaml object. - - :param bool strip: - Whether or not to strip additional leading spaces at the beginning - of the line. This is only needed when the root object is a list. - """ + def __init__(self): + """Wrap construction of ruamel yaml object.""" self.yaml = ruamel.yaml.YAML() self.yaml.allow_duplicate_keys = True self.yaml.representer.add_representer(type(None), none_representer) self.yaml.indent(mapping=2, sequence=4, offset=2) - self.strip = strip def load(self, stream): return self.yaml.load(stream) @@ -48,6 +42,17 @@ class YAML(object): return '\n'.join(newlines) def dump(self, data, *args, **kwargs): - if self.strip: + if isinstance(data, list): kwargs['transform'] = self.tr self.yaml.dump(data, *args, **kwargs) + + +_yaml = YAML() + + +def load(*args, **kwargs): + return _yaml.load(*args, **kwargs) + + +def dump(*args, **kwargs): + return _yaml.dump(*args, **kwargs)