Merge "Don't try to re-wrap things that already have newlines"
This commit is contained in:
commit
2c2637626d
@ -20,6 +20,12 @@ import yaml
|
||||
import yamlordereddictloader
|
||||
|
||||
|
||||
def _has_newline(data):
|
||||
if "\n" in data or "\r" in data:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class PrettySafeDumper(yaml.dumper.SafeDumper):
|
||||
"""Yaml dumper that tries to not alter original formats (to much)."""
|
||||
|
||||
@ -49,10 +55,13 @@ class PrettySafeDumper(yaml.dumper.SafeDumper):
|
||||
def represent_string(self, data):
|
||||
if isinstance(data, six.binary_type):
|
||||
data = data.decode(self.BINARY_ENCODING)
|
||||
if len(data) > self.MAX_LINE_LENGTH:
|
||||
# NOTE(harlowja): Try to nicely format it unless its already been
|
||||
# formatted by someone else, which we check by seeing if newlines
|
||||
# already exist and assume the person knew what they were doing...
|
||||
if len(data) > self.MAX_LINE_LENGTH and not _has_newline(data):
|
||||
data = textwrap.fill(data)
|
||||
style = "plain"
|
||||
if "\n" in data:
|
||||
if _has_newline(data):
|
||||
style = "|"
|
||||
return yaml.representer.ScalarNode('tag:yaml.org,2002:str',
|
||||
data, style=style)
|
||||
|
Loading…
x
Reference in New Issue
Block a user