yamlordereddictloader-0.4.0/0000755000175000017500000000000013140654025017507 5ustar francoisfrancois00000000000000yamlordereddictloader-0.4.0/LICENSE.txt0000644000175000017500000000204213076070145021333 0ustar francoisfrancois00000000000000Copyright 2017 François Ménabé Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. yamlordereddictloader-0.4.0/PKG-INFO0000644000175000017500000001007113140654025020603 0ustar francoisfrancois00000000000000Metadata-Version: 1.1 Name: yamlordereddictloader Version: 0.4.0 Summary: YAML loader and dump for PyYAML allowing to keep keys order. Home-page: https://github.com/fmenabe/python-yamlordereddictloader Author: François Ménabé Author-email: francois.menabe@gmail.com License: MIT License Download-URL: https://github.com/fmenabe/python-yamlordereddictloader Description: python-yamlordereddictloader ============================ .. image:: https://img.shields.io/pypi/l/yamlordereddictloader.svg :target: https://opensource.org/licenses/MIT :alt: License .. image:: https://img.shields.io/pypi/pyversions/yamlordereddictloader.svg :target: https://pypi.python.org/pypi/yamlordereddictloader :alt: Versions .. image:: https://img.shields.io/pypi/v/yamlordereddictloader.svg :target: https://pypi.python.org/pypi/yamlordereddictloader :alt: PyPi .. image:: https://img.shields.io/badge/github-repo-yellow.jpg :target: https://github.com/fmenabe/python-yamlordereddictloader :alt: Code repo .. image:: https://landscape.io/github/fmenabe/python-yamlordereddictloader/master/landscape.svg?style=flat :target: https://landscape.io/github/fmenabe/python-yamlordereddictloader/master :alt: Code Health This module provide a loader and a dumper for PyYAML allowing to keep items order when loading a file (by putting them in ``OrderedDict`` objects) and to manage ``OrderedDict`` objects when dumping to a file. The loader is based on stackoverflow topic (thanks to Eric Naeseth): http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts#answer-5121963 Self promotion: I use it a lot with `clg `_, which allows to generate command-line definition from a configuration file, for keeping order of subcommands, options and arguments in the help message! To install it ------------- .. code-block:: bash $ pip install yamlordereddictloader Loader usage ------------ .. code-block:: python import yaml import yamlordereddictloader data = yaml.load(open('myfile.yml'), Loader=yamlordereddictloader.Loader) **Note:** For using the safe loader (which want standard YAML tags and does not construct arbitrary Python objects), replace ``yamlorderdictloader.Loader`` by ``yamlorderedictloader.SafeLoader``. Dumper usage ------------ .. code-block:: python import yaml import yamlordereddictloader from collections import OrderedDict data = OrderedDict([ ('key1', 'val1'), ('key2', OrderedDict([('key21', 'val21'), ('key22', 'val22')])) ]) yaml.dump( data, open('myfile.yml', 'w'), Dumper=yamlordereddictloader.Dumper, default_flow_style=False) **Note:** For using the safe dumper (which produce standard YAML tags and does not represent arbitrary Python objects), replace ``yamlorderdictloader.Dumper`` by ``yamlorderedictloader.SafeDumper``. Keywords: YAML,loader,dumper,ordered,OrderedDict,pyyaml Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: Intended Audience :: System Administrators Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Utilities yamlordereddictloader-0.4.0/setup.py0000644000175000017500000000236313140653651021231 0ustar francoisfrancois00000000000000# -*- coding: utf-8 -*- import sys from distutils.core import setup requires = ['pyyaml'] if float('%d.%d' % sys.version_info[:2]) < 2.7: requires.append('ordereddict') setup( name='yamlordereddictloader', version='0.4.0', author='François Ménabé', author_email='francois.menabe@gmail.com', url='https://github.com/fmenabe/python-yamlordereddictloader', download_url='https://github.com/fmenabe/python-yamlordereddictloader', license='MIT License', description='YAML loader and dump for PyYAML allowing to keep keys order.', long_description=open('README.rst').read(), keywords=['YAML', 'loader', 'dumper', 'ordered', 'OrderedDict', 'pyyaml'], classifiers=['Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'Intended Audience :: System Administrators', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Topic :: Utilities'], py_modules=['yamlordereddictloader'], install_requires=requires) yamlordereddictloader-0.4.0/README.rst0000644000175000017500000000512513140653631021203 0ustar francoisfrancois00000000000000python-yamlordereddictloader ============================ .. image:: https://img.shields.io/pypi/l/yamlordereddictloader.svg :target: https://opensource.org/licenses/MIT :alt: License .. image:: https://img.shields.io/pypi/pyversions/yamlordereddictloader.svg :target: https://pypi.python.org/pypi/yamlordereddictloader :alt: Versions .. image:: https://img.shields.io/pypi/v/yamlordereddictloader.svg :target: https://pypi.python.org/pypi/yamlordereddictloader :alt: PyPi .. image:: https://img.shields.io/badge/github-repo-yellow.jpg :target: https://github.com/fmenabe/python-yamlordereddictloader :alt: Code repo .. image:: https://landscape.io/github/fmenabe/python-yamlordereddictloader/master/landscape.svg?style=flat :target: https://landscape.io/github/fmenabe/python-yamlordereddictloader/master :alt: Code Health This module provide a loader and a dumper for PyYAML allowing to keep items order when loading a file (by putting them in ``OrderedDict`` objects) and to manage ``OrderedDict`` objects when dumping to a file. The loader is based on stackoverflow topic (thanks to Eric Naeseth): http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts#answer-5121963 Self promotion: I use it a lot with `clg `_, which allows to generate command-line definition from a configuration file, for keeping order of subcommands, options and arguments in the help message! To install it ------------- .. code-block:: bash $ pip install yamlordereddictloader Loader usage ------------ .. code-block:: python import yaml import yamlordereddictloader data = yaml.load(open('myfile.yml'), Loader=yamlordereddictloader.Loader) **Note:** For using the safe loader (which want standard YAML tags and does not construct arbitrary Python objects), replace ``yamlorderdictloader.Loader`` by ``yamlorderedictloader.SafeLoader``. Dumper usage ------------ .. code-block:: python import yaml import yamlordereddictloader from collections import OrderedDict data = OrderedDict([ ('key1', 'val1'), ('key2', OrderedDict([('key21', 'val21'), ('key22', 'val22')])) ]) yaml.dump( data, open('myfile.yml', 'w'), Dumper=yamlordereddictloader.Dumper, default_flow_style=False) **Note:** For using the safe dumper (which produce standard YAML tags and does not represent arbitrary Python objects), replace ``yamlorderdictloader.Dumper`` by ``yamlorderedictloader.SafeDumper``. yamlordereddictloader-0.4.0/yamlordereddictloader.py0000644000175000017500000000534313140653631024432 0ustar francoisfrancois00000000000000# -*- coding: utf-8 -*- """YAML loaders and dumpers for PyYAML allowing to keep keys order.""" import sys import yaml if float('%d.%d' % sys.version_info[:2]) < 2.7: from ordereddict import OrderedDict else: from collections import OrderedDict # ## Loaders # def construct_yaml_map(self, node): data = OrderedDict() yield data value = self.construct_mapping(node) data.update(value) def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: msg = 'expected a mapping node, but found %s' % node.id raise yaml.constructor.ConstructError(None, None, msg, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as err: raise yaml.constructor.ConstructError( 'while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % err, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping class Loader(yaml.Loader): def __init__(self, *args, **kwargs): yaml.Loader.__init__(self, *args, **kwargs) self.add_constructor( 'tag:yaml.org,2002:map', type(self).construct_yaml_map) self.add_constructor( 'tag:yaml.org,2002:omap', type(self).construct_yaml_map) construct_yaml_map = construct_yaml_map construct_mapping = construct_mapping class SafeLoader(yaml.SafeLoader): def __init__(self, *args, **kwargs): yaml.SafeLoader.__init__(self, *args, **kwargs) self.add_constructor( 'tag:yaml.org,2002:map', type(self).construct_yaml_map) self.add_constructor( 'tag:yaml.org,2002:omap', type(self).construct_yaml_map) construct_yaml_map = construct_yaml_map construct_mapping = construct_mapping # ## Dumpers # def represent_ordereddict(self, data): return self.represent_mapping('tag:yaml.org,2002:map', data.items()) class Dumper(yaml.Dumper): def __init__(self, *args, **kwargs): yaml.Dumper.__init__(self, *args, **kwargs) self.add_representer(OrderedDict, type(self).represent_ordereddict) represent_ordereddict = represent_ordereddict class SafeDumper(yaml.SafeDumper): def __init__(self, *args, **kwargs): yaml.SafeDumper.__init__(self, *args, **kwargs) self.add_representer(OrderedDict, type(self).represent_ordereddict) represent_ordereddict = represent_ordereddict