diff options
author | Matěj Cepl <mcepl@redhat.com> | 2012-03-31 11:55:15 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2012-03-31 13:21:23 +0200 |
commit | 53d7ba3e2712585c51a19f6fcc3b7ff1e5e49e2e (patch) | |
tree | 9106aa452e1ed53851857e4de57b56b3b11c0f0b | |
parent | 4813712507fdc51e915d98f26f87db407ad21d38 (diff) | |
download | yamlish-53d7ba3e2712585c51a19f6fcc3b7ff1e5e49e2e.tar.gz |
Multline strings are always compact.
Fixes #45
Also updates NEWS.txt
-rw-r--r-- | NEWS.txt | 12 | ||||
-rw-r--r-- | test/test_output.py | 2 | ||||
-rw-r--r-- | yamlish.py | 22 |
3 files changed, 28 insertions, 8 deletions
@@ -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, don’t play with strings too much in setup.py + version=yamlish.__version__ and that’s 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 diff --git a/test/test_output.py b/test/test_output.py index 907dea6..888f812 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -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) |