summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2017-06-14 22:27:47 +0200
committerAnthon van der Neut <anthon@mnt.org>2017-06-14 22:27:47 +0200
commit0bb105c8060e156d902efeb911b58554c7201ad8 (patch)
tree535fc4dc7472d147b420f58ec5bb7788b6868f36
parentef97cadfed7733c637428ccecd3a7840604814e9 (diff)
downloadruamel.yaml-0bb105c8060e156d902efeb911b58554c7201ad8.tar.gz
add plug-in mechanism for load/dump pre resp. post-processing0.15.7
-rw-r--r--CHANGES3
-rw-r--r--README.rst3
-rw-r--r--__init__.py10
-rw-r--r--main.py26
4 files changed, 39 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index a401d38..9ef21b7 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,6 @@
+[0, 15, 7]: 2017-06-14
+ - add plug-in mechanism for load/dump pre resp. post-processing
+
[0, 15, 6]: 2017-06-10
- a set() with duplicate elements now throws error in rt loading
- support for toplevel column zero literal/folded scalar in explicit documents
diff --git a/README.rst b/README.rst
index 89ee836..fa503a9 100644
--- a/README.rst
+++ b/README.rst
@@ -32,6 +32,9 @@ ChangeLog
.. should insert NEXT: at the beginning of line for next key
+0.15.7 (2017-06-14):
+ - add plug-in mechanism for load/dump pre resp. post-processing
+
0.15.6 (2017-06-10):
- a set() with duplicate elements now throws error in rt loading
- support for toplevel column zero literal/folded scalar in explicit documents
diff --git a/__init__.py b/__init__.py
index 1224815..ba29709 100644
--- a/__init__.py
+++ b/__init__.py
@@ -11,12 +11,13 @@ if False: # MYPY
_package_data = dict(
full_package_name='ruamel.yaml',
- version_info=(0, 15, 6),
- __version__='0.15.6',
+ version_info=(0, 15, 7),
+ __version__='0.15.7',
author='Anthon van der Neut',
author_email='a.van.der.neut@ruamel.eu',
description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA
entry_points=None,
+ since=2014,
extras_require={':platform_python_implementation=="CPython" and python_version<="2.7"': [
'ruamel.ordereddict',
]},
@@ -49,6 +50,11 @@ _package_data = dict(
read_the_docs='yaml',
many_linux='libyaml-devel',
supported=[(2, 7), (3, 3)], # minimum
+ tox=dict(
+ env='*p',
+ deps='ruamel.std.pathlib',
+ fl8excl='_test/lib',
+ ),
) # type: Dict[Any, Any]
version_info = _package_data['version_info']
diff --git a/main.py b/main.py
index c238d2d..6240d14 100644
--- a/main.py
+++ b/main.py
@@ -3,7 +3,11 @@
from __future__ import absolute_import, unicode_literals
import sys
+import os
import warnings
+import glob
+from importlib import import_module
+
import ruamel.yaml
from ruamel.yaml.error import * # NOQA
@@ -40,7 +44,7 @@ enforce = object()
# subset of abbreviations, which should all caps according to PEP8
class YAML(object):
- def __init__(self, _kw=enforce, typ=None, pure=False):
+ def __init__(self, _kw=enforce, typ=None, pure=False, plug_ins=None):
# type: (Any, Any, Any) -> None
"""
_kw: not used, forces keyword arguments in 2.7 (in 3 you can do (*, safe_load=..)
@@ -49,12 +53,17 @@ class YAML(object):
'unsafe' -> normal/unsafe Loader/Dumper
'base' -> baseloader
pure: if True only use Python modules
+ plug_ins: a list of plug-in files
"""
if _kw is not enforce:
raise TypeError("{}.__init__() takes no positional argument but at least "
"one was given ({!r})".format(self.__class__.__name__, _kw))
self.typ = 'rt' if typ is None else typ
+ self.plug_ins = []
+ for pu in ([] if plug_ins is None else plug_ins) + self.official_plug_ins():
+ file_name = pu.replace('/', '.')
+ self.plug_ins.append(import_module(file_name))
self.Resolver = ruamel.yaml.resolver.VersionedResolver # type: Any
self.allow_unicode = True
self.Reader = None # type: Any
@@ -89,6 +98,14 @@ class YAML(object):
self.Parser = ruamel.yaml.parser.Parser if pure or CParser is None else CParser
self.Composer = ruamel.yaml.composer.Composer
self.Constructor = ruamel.yaml.constructor.Constructor
+ else:
+ for module in self.plug_ins:
+ if getattr(module, 'typ', None) == self.typ:
+ module.init_typ(self)
+ break
+ else:
+ raise NotImplementedError(
+ 'typ "{}"not recognised (need to install plug-in?)'.format(self.typ))
self.stream = None
self.canonical = None
self.indent = None
@@ -427,6 +444,13 @@ class YAML(object):
else:
return list(*args)
+ # helpers
+ def official_plug_ins(self):
+ bd = os.path.dirname(__file__)
+ gpbd = os.path.dirname(os.path.dirname(bd))
+ res = [x.replace(gpbd, '')[1:-3] for x in glob.glob(bd + '/*/yaml_plugin.py')]
+ return res
+
########################################################################################