Commit 53d7ba3e authored by Matěj Cepl's avatar Matěj Cepl

Multline strings are always compact.

Fixes #45
Also updates NEWS.txt
parent 48137125
......@@ -2,8 +2,8 @@
* Initial version: tests succeed
0.2 2012-03-08
* Just a packaging issue, don't play with strings too much in setup.py
version=yamlish.__version__ and that's it. No fancy formatting.
* Just a packaging issue, dont play with strings too much in setup.py
version=yamlish.__version__ and thats it. No fancy formatting.
0.3 2012-03-08
* Excuse me, son, but your tempfiles are leaking !!!
......@@ -13,4 +13,10 @@
* Missing requires field in setup.py
0.5 2012-03-14
* Make the module documentation string at least a bit presentable.
\ No newline at end of file
* Make the module documentation string at least a bit presentable.
0.6 2012-03-16
* package doesn’t install without NEWS.txt which was missing.
0.7 2012-03-29
* multiline strings are always compact (with "|" style).
\ No newline at end of file
......@@ -84,7 +84,7 @@ class TestOuptut(unittest.TestCase):
Test output to a file.
"""
outf = tempfile.TemporaryFile()
yaml.safe_dump(IN, outf)
yamlish.dump(IN, outf)
outf.seek(0)
got_str = outf.read()
outf.close()
......
......@@ -113,7 +113,7 @@ import logging
import yaml
__docformat__ = 'reStructuredText'
__version__ = "0.6"
__version__ = "0.7"
__author__ = "Matej Cepl <mcepl_at_redhat_dot_com>"
class _YamlishLoader(yaml.loader.SafeLoader):
......@@ -142,6 +142,20 @@ 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 '\n' in data:
style = '|'
data = data.decode('utf-8') # assumes all your strings are UTF-8 encoded
tag = u'tag:yaml.org,2002:str'
return dumper.represent_scalar(tag, data, style)
yaml.add_representer(str, str_representer_compact_multiline,
Dumper=_YamlishDumper)
def load(source):
"""
Return object loaded from a YAML document in source.
......@@ -172,10 +186,10 @@ def dump(source, destination):
if isinstance(destination, (str, unicode)):
with open(destination, "w") as outf:
dump(source, outf)
elif getattr(destination, "file"):
elif hasattr(destination, "fileno"):
yaml.dump(source, destination, encoding="utf-8",
default_flow_style=False, canonical=False,
Dumper=yaml.SafeDumper)
Dumper=_YamlishDumper)
else:
raise NameError
......@@ -186,4 +200,4 @@ def dumps(source):
return yaml.dump(source, encoding="utf-8",
explicit_start=True, explicit_end=True,
default_flow_style=False, default_style=False,
canonical=False, Dumper=yaml.SafeDumper)
canonical=False, Dumper=_YamlishDumper)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment