Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit long strings using yaml multiline syntax #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion flamel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@
import os


# from:
# http://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data flake8: noqa
def should_use_block(value):
for c in u"\u000a\u000d\u001c\u001d\u001e\u0085\u2028\u2029":
if c in value:
return True
return False

def my_represent_scalar(self, tag, value, style=None):
if style is None:
if should_use_block(value):
style='|'
else:
style = self.default_style

node = yaml.representer.ScalarNode(tag, value, style=style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
return node

yaml.representer.BaseRepresenter.represent_scalar = my_represent_scalar

class YamlOrderedLoader(yaml.SafeLoader):
"""Specialized Loader which respects order."""

Expand All @@ -48,7 +70,7 @@ def unicode_representer(dumper, uni):
OrderedDumper.add_representer(collections.OrderedDict, _dict_representer)
OrderedDumper.add_representer(collections.defaultdict, _dict_representer)
OrderedDumper.add_representer(unicode, unicode_representer)
return yaml.dump(data, stream, OrderedDumper, **kwds)
return yaml.safe_dump(data, stream, OrderedDumper, **kwds)


def yaml_load(data):
Expand Down