Path: blob/develop/awscli/customizations/eks/ordered_yaml.py
1567 views
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import yaml14from botocore.compat import OrderedDict151617class SafeOrderedLoader(yaml.SafeLoader):18""" Safely load a yaml file into an OrderedDict."""192021class SafeOrderedDumper(yaml.SafeDumper):22""" Safely dump an OrderedDict as yaml."""232425def _ordered_constructor(loader, node):26loader.flatten_mapping(node)27return OrderedDict(loader.construct_pairs(node))282930SafeOrderedLoader.add_constructor(31yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,32_ordered_constructor)333435def _ordered_representer(dumper, data):36return dumper.represent_mapping(37yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,38data.items())394041SafeOrderedDumper.add_representer(OrderedDict, _ordered_representer)424344def ordered_yaml_load(stream):45""" Load an OrderedDict object from a yaml stream."""46return yaml.load(stream, SafeOrderedLoader)474849def ordered_yaml_dump(to_dump, stream=None):50"""51Dump an OrderedDict object to yaml.5253:param to_dump: The OrderedDict to dump54:type to_dump: OrderedDict5556:param stream: The file to dump to57If not given or if None, only return the value58:type stream: file59"""60return yaml.dump(to_dump, stream,61SafeOrderedDumper, default_flow_style=False)626364