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 /yamlish.py | |
parent | 4813712507fdc51e915d98f26f87db407ad21d38 (diff) | |
download | yamlish-53d7ba3e2712585c51a19f6fcc3b7ff1e5e49e2e.tar.gz |
Multline strings are always compact.
Fixes #45
Also updates NEWS.txt
Diffstat (limited to 'yamlish.py')
-rw-r--r-- | yamlish.py | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -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) |