Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Unify to a single code base compatible with both Python 2 & 3 #245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions MANIFEST.in
@@ -1,7 +1,5 @@
include README LICENSE CHANGES setup.py
recursive-include lib/yaml *.py
recursive-include lib3/yaml *.py
recursive-include examples *.py *.cfg *.yaml
recursive-include tests/data *
recursive-include tests/lib *.py
recursive-include tests/lib3 *.py
44 changes: 21 additions & 23 deletions lib/yaml/__init__.py
@@ -1,21 +1,24 @@
from __future__ import unicode_literals

from error import *
import six

from tokens import *
from events import *
from nodes import *
from .error import *

from loader import *
from dumper import *
from .tokens import *
from .events import *
from .nodes import *

__version__ = '5.1b7'
from .loader import *
from .dumper import *

__version__ = '5.1b7'
try:
from cyaml import *
from .cyaml import *
__with_libyaml__ = True
except ImportError:
__with_libyaml__ = False

import io

#------------------------------------------------------------------------------
# Warnings control
Expand Down Expand Up @@ -200,8 +203,7 @@ def emit(events, stream=None, Dumper=Dumper,
"""
getvalue = None
if stream is None:
from StringIO import StringIO
stream = StringIO()
stream = io.StringIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Expand All @@ -216,7 +218,7 @@ def emit(events, stream=None, Dumper=Dumper,
def serialize_all(nodes, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding='utf-8', explicit_start=None, explicit_end=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None):
"""
Serialize a sequence of representation trees into a YAML stream.
Expand All @@ -225,10 +227,9 @@ def serialize_all(nodes, stream=None, Dumper=Dumper,
getvalue = None
if stream is None:
if encoding is None:
from StringIO import StringIO
stream = io.StringIO()
else:
from cStringIO import StringIO
stream = StringIO()
stream = io.BytesIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break,
Expand All @@ -255,7 +256,7 @@ def dump_all(documents, stream=None, Dumper=Dumper,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding='utf-8', explicit_start=None, explicit_end=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
"""
Serialize a sequence of Python objects into a YAML stream.
Expand All @@ -264,10 +265,9 @@ def dump_all(documents, stream=None, Dumper=Dumper,
getvalue = None
if stream is None:
if encoding is None:
from StringIO import StringIO
stream = io.StringIO()
else:
from cStringIO import StringIO
stream = StringIO()
stream = io.BytesIO()
getvalue = stream.getvalue
dumper = Dumper(stream, default_style=default_style,
default_flow_style=default_flow_style,
Expand Down Expand Up @@ -374,13 +374,12 @@ def __init__(cls, name, bases, kwds):
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
cls.yaml_dumper.add_representer(cls, cls.to_yaml)

class YAMLObject(object):
class YAMLObject(six.with_metaclass(YAMLObjectMetaclass)):
"""
An object that can dump itself to a YAML stream
and load itself from a YAML stream.
"""

__metaclass__ = YAMLObjectMetaclass
__slots__ = () # no direct instantiation, so allow immutable subclasses

yaml_loader = Loader
Expand All @@ -389,18 +388,17 @@ class YAMLObject(object):
yaml_tag = None
yaml_flow_style = None

@classmethod
def from_yaml(cls, loader, node):
"""
Convert a representation node to a Python object.
"""
return loader.construct_yaml_object(node, cls)
from_yaml = classmethod(from_yaml)

@classmethod
def to_yaml(cls, dumper, data):
"""
Convert a Python object to a representation node.
"""
return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
flow_style=cls.yaml_flow_style)
to_yaml = classmethod(to_yaml)

18 changes: 9 additions & 9 deletions lib/yaml/composer.py
@@ -1,9 +1,10 @@
from __future__ import unicode_literals

__all__ = ['Composer', 'ComposerError']

from error import MarkedYAMLError
from events import *
from nodes import *
from .error import MarkedYAMLError
from .events import *
from .nodes import *

class ComposerError(MarkedYAMLError):
pass
Expand Down Expand Up @@ -66,14 +67,14 @@ def compose_node(self, parent, index):
anchor = event.anchor
if anchor not in self.anchors:
raise ComposerError(None, None, "found undefined alias %r"
% anchor.encode('utf-8'), event.start_mark)
% anchor, event.start_mark)
return self.anchors[anchor]
event = self.peek_event()
anchor = event.anchor
if anchor is not None:
if anchor in self.anchors:
raise ComposerError("found duplicate anchor %r; first occurrence"
% anchor.encode('utf-8'), self.anchors[anchor].start_mark,
% anchor, self.anchors[anchor].start_mark,
"second occurrence", event.start_mark)
self.descend_resolver(parent, index)
if self.check_event(ScalarEvent):
Expand All @@ -88,7 +89,7 @@ def compose_node(self, parent, index):
def compose_scalar_node(self, anchor):
event = self.get_event()
tag = event.tag
if tag is None or tag == u'!':
if tag is None or tag == '!':
tag = self.resolve(ScalarNode, event.value, event.implicit)
node = ScalarNode(tag, event.value,
event.start_mark, event.end_mark, style=event.style)
Expand All @@ -99,7 +100,7 @@ def compose_scalar_node(self, anchor):
def compose_sequence_node(self, anchor):
start_event = self.get_event()
tag = start_event.tag
if tag is None or tag == u'!':
if tag is None or tag == '!':
tag = self.resolve(SequenceNode, None, start_event.implicit)
node = SequenceNode(tag, [],
start_event.start_mark, None,
Expand All @@ -117,7 +118,7 @@ def compose_sequence_node(self, anchor):
def compose_mapping_node(self, anchor):
start_event = self.get_event()
tag = start_event.tag
if tag is None or tag == u'!':
if tag is None or tag == '!':
tag = self.resolve(MappingNode, None, start_event.implicit)
node = MappingNode(tag, [],
start_event.start_mark, None,
Expand All @@ -136,4 +137,3 @@ def compose_mapping_node(self, anchor):
end_event = self.get_event()
node.end_mark = end_event.end_mark
return node