diff options
author | Anthon van der Neut <anthon@mnt.org> | 2016-07-06 23:31:57 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2016-07-06 23:31:57 +0200 |
commit | a990e4568c7f341cc76f5ab572a3089bb1065da4 (patch) | |
tree | 437e9b9e007dddc73510e805a554e139a84bef50 | |
parent | bc5bd304c273b3c0067c2c8840efedd2bc4fc327 (diff) | |
download | ruamel.yaml-a990e4568c7f341cc76f5ab572a3089bb1065da4.tar.gz |
added optional preservation of quotes around scalars
-rw-r--r-- | .hgignore | 1 | ||||
-rw-r--r-- | Dockerfile | 19 | ||||
-rw-r--r-- | README.rst | 3 | ||||
-rw-r--r-- | __init__.py | 2 | ||||
-rw-r--r-- | _test/roundtrip.py | 9 | ||||
-rw-r--r-- | _test/test_string.py | 23 | ||||
-rw-r--r-- | composer.py | 15 | ||||
-rw-r--r-- | constructor.py | 8 | ||||
-rw-r--r-- | cyaml.py | 2 | ||||
-rw-r--r-- | docker-compose.yml | 11 | ||||
-rw-r--r-- | loader.py | 4 | ||||
-rw-r--r-- | main.py | 12 | ||||
-rw-r--r-- | representer.py | 24 | ||||
-rw-r--r-- | scalarstring.py | 13 | ||||
-rw-r--r-- | serializer.py | 13 | ||||
-rw-r--r-- | setup.py | 3 | ||||
-rw-r--r-- | tox.ini | 2 |
17 files changed, 135 insertions, 29 deletions
@@ -25,3 +25,4 @@ convert cmd TODO.rst _doc/_build +.dcw_alt.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..20151c6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM quay.io/pypa/manylinux1_x86_64 + +MAINTAINER Anthon van der Neut <a.van.der.neut@ruamel.eu> + +RUN echo 'cd /src' > /usr/bin/makewheel +RUN echo 'for PYBIN in /opt/python/$1*/bin/; do' >> /usr/bin/makewheel +RUN echo ' echo "$PYBIN"' >> /usr/bin/makewheel +RUN echo ' ${PYBIN}/pip wheel . -w /tmp' >> /usr/bin/makewheel +RUN echo 'done' >> /usr/bin/makewheel +RUN echo '' >> /usr/bin/makewheel +RUN echo 'for whl in /tmp/*.whl; do' >> /usr/bin/makewheel +RUN echo ' auditwheel repair "$whl" -w /src/dist/' >> /usr/bin/makewheel +RUN echo 'done' >> /usr/bin/makewheel +RUN chmod 755 /usr/bin/makewheel + +RUN yum install -y libyaml-devel + + +CMD /usr/bin/makewheel
\ No newline at end of file @@ -18,6 +18,9 @@ ChangeLog :: + 0.11.12 (2016-07-06): + - added + 0.11.0 (2016-02-18): - RoundTripLoader loads 1.2 by default (no sexagesimals, 012 octals nor yes/no/on/off booleans diff --git a/__init__.py b/__init__.py index 9be2082..142c2ef 100644 --- a/__init__.py +++ b/__init__.py @@ -9,7 +9,7 @@ from __future__ import absolute_import _package_data = dict( full_package_name="ruamel.yaml", - version_info=(0, 11, 11), + version_info=(0, 11, 12, "dev"), 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 diff --git a/_test/roundtrip.py b/_test/roundtrip.py index d82c001..9872239 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -22,9 +22,9 @@ def dedent(data): return textwrap.dedent(data) -def round_trip_load(inp): +def round_trip_load(inp, preserve_quotes=None): dinp = dedent(inp) - return ruamel.yaml.load(dinp, ruamel.yaml.RoundTripLoader) + return ruamel.yaml.load(dinp, ruamel.yaml.RoundTripLoader, preserve_quotes=preserve_quotes) def round_trip_dump(data, indent=None, block_seq_indent=None, top_level_colon_align=None, @@ -36,13 +36,14 @@ def round_trip_dump(data, indent=None, block_seq_indent=None, top_level_colon_al def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None, - block_seq_indent=None, top_level_colon_align=None, prefix_colon=None): + block_seq_indent=None, top_level_colon_align=None, prefix_colon=None, + preserve_quotes=None): if outp is None: outp = inp doutp = dedent(outp) if extra is not None: doutp += extra - data = round_trip_load(inp) + data = round_trip_load(inp, preserve_quotes=preserve_quotes) if intermediate is not None: if isinstance(intermediate, dict): for k, v in intermediate.items(): diff --git a/_test/test_string.py b/_test/test_string.py index 5aa0700..13474b5 100644 --- a/_test/test_string.py +++ b/_test/test_string.py @@ -22,13 +22,13 @@ import platform from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA -class TestYAML: +class TestPreservedScalarString: def test_basic_string(self): round_trip(""" a: abcdefg """, ) - def test_quoted_string(self): + def test_quoted_integer_string(self): round_trip(""" a: '12345' """) @@ -105,3 +105,22 @@ class TestYAML: def """, intermediate=dict(a='abc def\n\n')) + + +class TestQuotedScalarString: + def test_single_quoted_string(self): + round_trip(""" + a: 'abc' + """, preserve_quotes=True) + + def test_double_quoted_string(self): + round_trip(""" + a: "abc" + """, preserve_quotes=True) + + def test_non_preserved_double_quoted_string(self): + round_trip(""" + a: "abc" + """, outp=""" + a: abc + """) diff --git a/composer.py b/composer.py index cf2508e..e193fdb 100644 --- a/composer.py +++ b/composer.py @@ -3,19 +3,24 @@ from __future__ import absolute_import from __future__ import print_function -__all__ = ['Composer', 'ComposerError'] try: from .error import MarkedYAMLError - from .events import * # NOQA - from .nodes import * # NOQA from .compat import utf8 except (ImportError, ValueError): # for Jython from ruamel.yaml.error import MarkedYAMLError - from ruamel.yaml.events import * # NOQA - from ruamel.yaml.nodes import * # NOQA from ruamel.yaml.compat import utf8 +from ruamel.yaml.events import ( + StreamStartEvent, StreamEndEvent, MappingStartEvent, MappingEndEvent, + SequenceStartEvent, SequenceEndEvent, AliasEvent, ScalarEvent, +) +from ruamel.yaml.nodes import ( + MappingNode, ScalarNode, SequenceNode, +) + +__all__ = ['Composer', 'ComposerError'] + class ComposerError(MarkedYAMLError): pass diff --git a/constructor.py b/constructor.py index 81635de..f809df4 100644 --- a/constructor.py +++ b/constructor.py @@ -39,11 +39,12 @@ class BaseConstructor(object): yaml_constructors = {} yaml_multi_constructors = {} - def __init__(self): + def __init__(self, preserve_quotes=None): self.constructed_objects = {} self.recursive_objects = {} self.state_generators = [] self.deep_construct = False + self._preserve_quotes = preserve_quotes def check_data(self): # If there are more documents available? @@ -830,6 +831,11 @@ class RoundTripConstructor(SafeConstructor): if node.style == '|' and isinstance(node.value, text_type): return PreservedScalarString(node.value) + elif self._preserve_quotes and isinstance(node.value, text_type): + if node.style == "'": + return SingleQuotedScalarString(node.value) + if node.style == '"': + return DoubleQuotedScalarString(node.value) return node.value def construct_yaml_str(self, node): @@ -34,7 +34,7 @@ class CSafeLoader(CParser, SafeConstructor, Resolver): class CLoader(CParser, Constructor, Resolver): - def __init__(self, stream, version=None): + def __init__(self, stream, version=None, preserve_quotes=None): CParser.__init__(self, stream) Constructor.__init__(self) Resolver.__init__(self) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..87ee45c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '2' +user-data: + author: Anthon van der Neut <a.van.der.neut@ruamel.eu> + description: manylinux1 wheel build container for ruamel.yaml + # env-defaults: +services: + yamlmanylinux1: + container_name: yamlmanylinux1 + build: . + volumes: + - .:/src @@ -52,10 +52,10 @@ class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver): class RoundTripLoader(Reader, RoundTripScanner, RoundTripParser, Composer, RoundTripConstructor, VersionedResolver): - def __init__(self, stream, version=None): + def __init__(self, stream, version=None, preserve_quotes=None): Reader.__init__(self, stream) RoundTripScanner.__init__(self) RoundTripParser.__init__(self) Composer.__init__(self) - RoundTripConstructor.__init__(self) + RoundTripConstructor.__init__(self, preserve_quotes=preserve_quotes) VersionedResolver.__init__(self, version) @@ -65,12 +65,12 @@ def compose_all(stream, Loader=Loader): loader.dispose() -def load(stream, Loader=Loader, version=None): +def load(stream, Loader=Loader, version=None, preserve_quotes=None): """ Parse the first YAML document in a stream and produce the corresponding Python object. """ - loader = Loader(stream, version) + loader = Loader(stream, version, preserve_quotes=preserve_quotes) try: return loader.get_single_data() finally: @@ -108,22 +108,22 @@ def safe_load_all(stream, version=None): return load_all(stream, SafeLoader, version) -def round_trip_load(stream, version=None): +def round_trip_load(stream, version=None, preserve_quotes=None): """ Parse the first YAML document in a stream and produce the corresponding Python object. Resolve only basic YAML tags. """ - return load(stream, RoundTripLoader, version) + return load(stream, RoundTripLoader, version, preserve_quotes=preserve_quotes) -def round_trip_load_all(stream, version=None): +def round_trip_load_all(stream, version=None, preserve_quotes=None): """ Parse all YAML documents in a stream and produce corresponding Python objects. Resolve only basic YAML tags. """ - return load_all(stream, RoundTripLoader, version) + return load_all(stream, RoundTripLoader, version, preserve_quotes=preserve_quotes) def emit(events, stream=None, Dumper=Dumper, diff --git a/representer.py b/representer.py index e3f07ae..7498443 100644 --- a/representer.py +++ b/representer.py @@ -605,6 +605,22 @@ class RoundTripRepresenter(SafeRepresenter): tag = u'tag:yaml.org,2002:str' return self.represent_scalar(tag, data, style=style) + def represent_single_quoted_scalarstring(self, data): + tag = None + style = "'" + if PY2 and not isinstance(data, unicode): + data = unicode(data, 'ascii') + tag = u'tag:yaml.org,2002:str' + return self.represent_scalar(tag, data, style=style) + + def represent_double_quoted_scalarstring(self, data): + tag = None + style = '"' + if PY2 and not isinstance(data, unicode): + data = unicode(data, 'ascii') + tag = u'tag:yaml.org,2002:str' + return self.represent_scalar(tag, data, style=style) + def represent_sequence(self, tag, sequence, flow_style=None): value = [] # if the flow_style is None, the flow style tacked on to the object @@ -843,6 +859,14 @@ RoundTripRepresenter.add_representer( PreservedScalarString, RoundTripRepresenter.represent_preserved_scalarstring) +RoundTripRepresenter.add_representer( + SingleQuotedScalarString, + RoundTripRepresenter.represent_single_quoted_scalarstring) + +RoundTripRepresenter.add_representer( + DoubleQuotedScalarString, + RoundTripRepresenter.represent_double_quoted_scalarstring) + RoundTripRepresenter.add_representer(CommentedSeq, RoundTripRepresenter.represent_list) diff --git a/scalarstring.py b/scalarstring.py index 3885425..d3abaff 100644 --- a/scalarstring.py +++ b/scalarstring.py @@ -3,7 +3,8 @@ from __future__ import absolute_import from __future__ import print_function -__all__ = ["ScalarString", "PreservedScalarString"] +__all__ = ["ScalarString", "PreservedScalarString", "SingleQuotedScalarString", + "DoubleQuotedScalarString"] try: from .compat import text_type @@ -21,6 +22,16 @@ class PreservedScalarString(ScalarString): return ScalarString.__new__(cls, value) +class SingleQuotedScalarString(ScalarString): + def __new__(cls, value): + return ScalarString.__new__(cls, value) + + +class DoubleQuotedScalarString(ScalarString): + def __new__(cls, value): + return ScalarString.__new__(cls, value) + + def preserve_literal(s): return PreservedScalarString(s.replace('\r\n', '\n').replace('\r', '\n')) diff --git a/serializer.py b/serializer.py index 7ed9ff9..60dd517 100644 --- a/serializer.py +++ b/serializer.py @@ -6,15 +6,20 @@ import re try: from .error import YAMLError - from .events import * # NOQA - from .nodes import * # NOQA from .compat import nprint, DBG_NODE, dbg, string_types except (ImportError, ValueError): # for Jython from ruamel.yaml.error import YAMLError - from ruamel.yaml.events import * # NOQA - from ruamel.yaml.nodes import * # NOQA from ruamel.yaml.compat import nprint, DBG_NODE, dbg, string_types +from ruamel.yaml.events import ( + StreamStartEvent, StreamEndEvent, MappingStartEvent, MappingEndEvent, + SequenceStartEvent, SequenceEndEvent, AliasEvent, ScalarEvent, + DocumentStartEvent, DocumentEndEvent, +) +from ruamel.yaml.nodes import ( + MappingNode, ScalarNode, SequenceNode, +) + __all__ = ['Serializer', 'SerializerError'] @@ -7,9 +7,10 @@ from __future__ import print_function, absolute_import, division, unicode_litera import sys import os +import datetime sys.path = [path for path in sys.path if path not in [os.getcwd(), '']] import platform # NOQA -from _ast import * # NOQA +from _ast import * # NOQA: F405 from ast import parse # NOQA from setuptools import setup, Extension, Distribution # NOQA @@ -8,7 +8,7 @@ commands = /bin/bash -c 'py.test _test/test_*.py' deps = pytest - flake8 + flake8==2.5.5 [pytest] norecursedirs = test/lib .tox |