diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2014-07-30 12:30:30 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2014-07-30 12:43:43 +0000 |
commit | 391d8cec9ae74a5f1d3b4101f4542a81f63d9860 (patch) | |
tree | be34cff00a09e3baba0e0045ebaa7004faa7781d /morphlib/morphloader.py | |
parent | f860d3e7b0e14b01b31d4f3d63a8c37572cd9fdc (diff) | |
download | morph-391d8cec9ae74a5f1d3b4101f4542a81f63d9860.tar.gz |
Dump multi-line strings in yaml documents in '|' formbaserock/richardmaw/bugfix/yaml-multi-line-dump
This prevents the description fields of morphologies being mangled.
This does not preserve the original formatting, so much as happen to
dump it in the same way we wrote it, but given we chose that form
because we think it looks the nicest, that's not a problem.
Diffstat (limited to 'morphlib/morphloader.py')
-rw-r--r-- | morphlib/morphloader.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py index 45416a19..21e10827 100644 --- a/morphlib/morphloader.py +++ b/morphlib/morphloader.py @@ -224,7 +224,7 @@ class DuplicateDeploymentNameError(MorphologyValidationError): % (cluster_filename, '\n ' + '\n '.join(duplicates))) -class OrderedDumper(yaml.SafeDumper): +class MorphologyDumper(yaml.SafeDumper): keyorder = ( 'name', 'kind', @@ -274,9 +274,36 @@ class OrderedDumper(yaml.SafeDumper): return dumper.represent_mapping('tag:yaml.org,2002:map', cls._iter_in_global_order(mapping)) + @classmethod + def _represent_str(cls, dumper, orig_data): + fallback_representer = yaml.representer.SafeRepresenter.represent_str + try: + data = unicode(orig_data, 'ascii') + if data.count('\n') == 0: + return fallback_representer(dumper, orig_data) + except UnicodeDecodeError: + try: + data = unicode(orig_data, 'utf-8') + if data.count('\n') == 0: + return fallback_representer(dumper, orig_data) + except UnicodeDecodeError: + return fallback_representer(dumper, orig_data) + return dumper.represent_scalar(u'tag:yaml.org,2002:str', + data, style='|') + + @classmethod + def _represent_unicode(cls, dumper, data): + if data.count('\n') == 0: + return yaml.representer.SafeRepresenter.represent_unicode(dumper, + data) + return dumper.represent_scalar(u'tag:yaml.org,2002:str', + data, style='|') + def __init__(self, *args, **kwargs): yaml.SafeDumper.__init__(self, *args, **kwargs) self.add_representer(dict, self._represent_dict) + self.add_representer(str, self._represent_str) + self.add_representer(unicode, self._represent_unicode) class MorphologyLoader(object): @@ -394,7 +421,7 @@ class MorphologyLoader(object): def save_to_string(self, morphology): '''Return normalised textual form of morphology.''' - return yaml.dump(morphology.data, Dumper=OrderedDumper, + return yaml.dump(morphology.data, Dumper=MorphologyDumper, default_flow_style=False) def save_to_file(self, filename, morphology): |