From 983626a715811187ea4efa87a53957c39baf250e Mon Sep 17 00:00:00 2001 From: Clint Adams Date: Mon, 28 Sep 2015 16:10:24 -0400 Subject: [PATCH] Factor out yaml emitter/dumper functions from normalize_projects_yaml.py This moves the functions used by normalize_projects_yaml.py out for the purposes of being imported by multiple scripts. Change-Id: I76a93124fdbe13bce34a0c5073fa66bc86806c0a --- tools/normalize_projects_yaml.py | 72 ++--------------------------- tools/projectconfig_yamllib.py | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 67 deletions(-) create mode 100755 tools/projectconfig_yamllib.py diff --git a/tools/normalize_projects_yaml.py b/tools/normalize_projects_yaml.py index 6a99a82d91..2de37ec3e3 100755 --- a/tools/normalize_projects_yaml.py +++ b/tools/normalize_projects_yaml.py @@ -17,76 +17,14 @@ import yaml from collections import OrderedDict - - -def project_representer(dumper, data): - return dumper.represent_mapping('tag:yaml.org,2002:map', - data.items()) - - -def construct_yaml_map(self, node): - data = OrderedDict() - yield data - value = self.construct_mapping(node) - - if isinstance(node, yaml.MappingNode): - self.flatten_mapping(node) - else: - raise yaml.constructor.ConstructorError( - None, None, - 'expected a mapping node, but found %s' % node.id, - node.start_mark) - - mapping = OrderedDict() - for key_node, value_node in node.value: - key = self.construct_object(key_node, deep=False) - try: - hash(key) - except TypeError as exc: - raise yaml.constructor.ConstructorError( - 'while constructing a mapping', node.start_mark, - 'found unacceptable key (%s)' % exc, key_node.start_mark) - value = self.construct_object(value_node, deep=False) - mapping[key] = value - data.update(mapping) - - -class IndentedEmitter(yaml.emitter.Emitter): - def expect_block_sequence(self): - self.increase_indent(flow=False, indentless=False) - self.state = self.expect_first_block_sequence_item - - -class IndentedDumper(IndentedEmitter, yaml.serializer.Serializer, - yaml.representer.Representer, yaml.resolver.Resolver): - def __init__(self, stream, - default_style=None, default_flow_style=None, - canonical=None, indent=None, width=None, - allow_unicode=None, line_break=None, - encoding=None, explicit_start=None, explicit_end=None, - version=None, tags=None): - IndentedEmitter.__init__( - self, stream, canonical=canonical, - indent=indent, width=width, - allow_unicode=allow_unicode, - line_break=line_break) - yaml.serializer.Serializer.__init__( - self, encoding=encoding, - explicit_start=explicit_start, - explicit_end=explicit_end, - version=version, tags=tags) - yaml.representer.Representer.__init__( - self, default_style=default_style, - default_flow_style=default_flow_style) - yaml.resolver.Resolver.__init__(self) - +import projectconfig_yamllib as pcy def main(): yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, - construct_yaml_map) + pcy.construct_yaml_map) - yaml.add_representer(OrderedDict, project_representer, - Dumper=IndentedDumper) + yaml.add_representer(OrderedDict, pcy.project_representer, + Dumper=pcy.IndentedDumper) data = yaml.load(open('gerrit/projects.yaml')) @@ -97,7 +35,7 @@ def main(): with open('gerrit/projects.yaml', 'w') as out: out.write(yaml.dump(data, default_flow_style=False, - Dumper=IndentedDumper, width=80)) + Dumper=pcy.IndentedDumper, width=80)) if __name__ == '__main__': main() diff --git a/tools/projectconfig_yamllib.py b/tools/projectconfig_yamllib.py new file mode 100755 index 0000000000..5095121534 --- /dev/null +++ b/tools/projectconfig_yamllib.py @@ -0,0 +1,79 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import yaml +from collections import OrderedDict + + +def project_representer(dumper, data): + return dumper.represent_mapping('tag:yaml.org,2002:map', + data.items()) + + +def construct_yaml_map(self, node): + data = OrderedDict() + yield data + value = self.construct_mapping(node) + + if isinstance(node, yaml.MappingNode): + self.flatten_mapping(node) + else: + raise yaml.constructor.ConstructorError( + None, None, + 'expected a mapping node, but found %s' % node.id, + node.start_mark) + + mapping = OrderedDict() + for key_node, value_node in node.value: + key = self.construct_object(key_node, deep=False) + try: + hash(key) + except TypeError as exc: + raise yaml.constructor.ConstructorError( + 'while constructing a mapping', node.start_mark, + 'found unacceptable key (%s)' % exc, key_node.start_mark) + value = self.construct_object(value_node, deep=False) + mapping[key] = value + data.update(mapping) + + +class IndentedEmitter(yaml.emitter.Emitter): + def expect_block_sequence(self): + self.increase_indent(flow=False, indentless=False) + self.state = self.expect_first_block_sequence_item + + +class IndentedDumper(IndentedEmitter, yaml.serializer.Serializer, + yaml.representer.Representer, yaml.resolver.Resolver): + def __init__(self, stream, + default_style=None, default_flow_style=None, + canonical=None, indent=None, width=None, + allow_unicode=None, line_break=None, + encoding=None, explicit_start=None, explicit_end=None, + version=None, tags=None): + IndentedEmitter.__init__( + self, stream, canonical=canonical, + indent=indent, width=width, + allow_unicode=allow_unicode, + line_break=line_break) + yaml.serializer.Serializer.__init__( + self, encoding=encoding, + explicit_start=explicit_start, + explicit_end=explicit_end, + version=version, tags=tags) + yaml.representer.Representer.__init__( + self, default_style=default_style, + default_flow_style=default_flow_style) + yaml.resolver.Resolver.__init__(self)