aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.travis.yml1
-rw-r--r--setup.py6
-rw-r--r--test/__init__.py18
-rw-r--r--yamlish.py79
4 files changed, 71 insertions, 33 deletions
diff --git a/.travis.yml b/.travis.yml
index f6248d2..50abecc 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,7 +3,6 @@ python:
- "2.6"
- "2.7"
- "pypy"
- - "3.2"
- "3.3"
- "3.4"
script:
diff --git a/setup.py b/setup.py
index 1a970b2..23afc38 100644
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@ def read(fname):
setup(
name='yamlish',
- version="0.15.1",
+ version="0.16",
description='Python implementation of YAMLish',
author='Matěj Cepl',
author_email='mcepl@redhat.com',
@@ -25,6 +25,10 @@ setup(
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
+ "Programming Language :: Python :: 3",
+ "Programming Language :: Python :: Implementation :: PyPy",
+ "Programming Language :: Python :: 3.3",
+ "Programming Language :: Python :: 3.4",
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Intended Audience :: Information Technology",
diff --git a/test/__init__.py b/test/__init__.py
index 1cb1ad0..85cadbd 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -9,8 +9,12 @@ import textwrap
INPUT = 1
OUTPUT = 2
+if yamlish.py3k:
+ unicode = str
+
#logging.basicConfig(level=logging.DEBUG)
+
def _generate_test_name(source):
"""
Clean up human-friendly test name into a method name.
@@ -37,7 +41,8 @@ def _create_input_test(test_src, tested_function, options=None):
want = test_src['out']
got = tested_function(test_src['in'], options)
logging.debug('got = type %s', type(got))
- logging.debug("test_src['out'] = %s", unicode(test_src['out']))
+ logging.debug("test_src['out'] = %s",
+ unicode(test_src['out']))
self.assertEqual(got, want, """Result matches
expected = %s
@@ -60,7 +65,8 @@ def _create_output_test(test_src, tested_function, options=None):
# We currently don't throw any exceptions in Writer, so this
# this is always false
if 'error' in test_src:
- self.assertRaises(test_src['error'], yamlish.dumps, test_src['in'], options)
+ self.assertRaises(test_src['error'], yamlish.dumps,
+ test_src['in'], options)
else:
logging.debug("out:\n%s", textwrap.dedent(test_src['out']))
want = yaml.load(textwrap.dedent(test_src['out']))
@@ -77,7 +83,7 @@ def _create_output_test(test_src, tested_function, options=None):
def generate_testsuite(test_data, test_case_shell, test_fce, direction=INPUT,
- options=None):
+ options=None):
"""
Generate tests from the test data, class to build upon and function
to use for testing.
@@ -88,8 +94,10 @@ def generate_testsuite(test_data, test_case_shell, test_fce, direction=INPUT,
continue
name = _generate_test_name(in_test['name'])
if direction == INPUT:
- test_method = _create_input_test(in_test, test_fce, options=options)
+ test_method = _create_input_test(in_test, test_fce,
+ options=options)
elif direction == OUTPUT:
- test_method = _create_output_test(in_test, test_fce, options=options)
+ test_method = _create_output_test(in_test, test_fce,
+ options=options)
test_method.__name__ = str('test_%s' % name)
setattr(test_case_shell, test_method.__name__, test_method)
diff --git a/yamlish.py b/yamlish.py
index 0c84c3b..2d2bd7f 100644
--- a/yamlish.py
+++ b/yamlish.py
@@ -1,23 +1,24 @@
# -*- coding: utf-8 -*-
#Copyright (C) 2012 Red Hat, Inc.
#
-#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:
+#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 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.
+#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.
"""
Easy YAML serialisation compatible with TAP format.
@@ -111,6 +112,7 @@ Read more about TAP and YAMLish on `<http://testanything.org/wiki>`
from __future__ import absolute_import, print_function, unicode_literals
import logging
import yaml
+import sys
class NullHandler(logging.Handler):
@@ -125,6 +127,14 @@ __docformat__ = 'reStructuredText'
__version__ = "0.10"
__author__ = u"Matěj Cepl <mcepl_at_redhat_dot_com>"
+py3k = sys.version_info[0] > 2
+
+try:
+ isinstance('a', basestring)
+except NameError:
+ basestring = (bytes, str)
+
+
class _YamlishLoader(yaml.loader.SafeLoader):
"""
Remove a datetime resolving.
@@ -151,22 +161,32 @@ class _YamlishLoader(yaml.loader.SafeLoader):
_YamlishLoader.remove_implicit_resolver(u'tag:yaml.org,2002:timestamp')
+
class _YamlishDumper(yaml.dumper.SafeDumper):
pass
+
def str_representer_compact_multiline(dumper, data):
style = None
- if isinstance(data, str):
- data = data.decode('utf-8') # assumes all your strings are UTF-8 encoded
+ if not py3k and isinstance(data, str):
+ # assumes all your strings are UTF-8 encoded
+ data = data.decode('utf-8')
if '\n' in data:
style = '|'
tag = u'tag:yaml.org,2002:str'
return dumper.represent_scalar(tag, data, style)
-yaml.add_representer(str, str_representer_compact_multiline,
- Dumper=_YamlishDumper)
-yaml.add_representer(unicode, str_representer_compact_multiline,
- Dumper=_YamlishDumper)
+if py3k:
+ yaml.add_representer(bytes, str_representer_compact_multiline,
+ Dumper=_YamlishDumper)
+ yaml.add_representer(str, str_representer_compact_multiline,
+ Dumper=_YamlishDumper)
+else:
+ yaml.add_representer(str, str_representer_compact_multiline,
+ Dumper=_YamlishDumper)
+ yaml.add_representer(unicode, str_representer_compact_multiline,
+ Dumper=_YamlishDumper)
+
def load(source, ignore_wrong_characters=False):
"""
@@ -179,16 +199,21 @@ def load(source, ignore_wrong_characters=False):
out = None
log.debug("inobj: (%s)\n%s", type(source), source)
log.debug('before ignore_wrong_characters = %s', ignore_wrong_characters)
- if isinstance(source, (str, unicode)):
+ if isinstance(source, basestring):
out = yaml.load(source, Loader=_YamlishLoader)
log.debug("out (string) = %s", out)
elif hasattr(source, "__iter__"):
- inobj = ""
+ inobj = u""
for line in source:
try:
- inobj += line + '\n'
+ if not py3k or isinstance(line, bytes):
+ line = line.decode('utf8')
+ logging.debug('inobj, line ... %s, %s',
+ type(inobj), type(line))
+ inobj += line + u'\n'
except UnicodeDecodeError:
- log.debug('in ignore_wrong_characters = %s', ignore_wrong_characters)
+ log.debug('in ignore_wrong_characters = %s',
+ ignore_wrong_characters)
if ignore_wrong_characters:
inobj += line.decode('utf8', 'ignore') + '\n'
else:
@@ -199,13 +224,14 @@ def load(source, ignore_wrong_characters=False):
log.debug("out (iter) = type %s", type(out))
return out
+
def dump(source, destination):
"""
Store source in destination file.
Destination is either a file object or a string with a filename.
"""
- if isinstance(destination, (str, unicode)):
+ if isinstance(destination, basestring):
with open(destination, "w") as outf:
dump(source, outf)
elif hasattr(destination, "fileno"):
@@ -215,6 +241,7 @@ def dump(source, destination):
else:
raise NameError
+
def dumps(source):
"""
Return YAMLish string from given source.