forked from natcap/geometamaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify the Dumper subclass state instead of the global yaml state. na…
- Loading branch information
Showing
1 changed file
with
10 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
import yaml | ||
|
||
|
||
def represent_str(dumper, data): | ||
def _represent_str(dumper, data): | ||
scalar = yaml.representer.SafeRepresenter.represent_str(dumper, data) | ||
if len(data.splitlines()) > 1: | ||
scalar.style = '|' # literal style, newline chars will be new lines | ||
return scalar | ||
|
||
|
||
# Patch the default string representer so that it uses | ||
# a literal block style when the data contain newline characters | ||
yaml.SafeDumper.add_representer(str, represent_str) | ||
class _SafeDumper(yaml.SafeDumper): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# Patch the default string representer to use a literal block | ||
# style when the data contain newline characters | ||
self.add_representer(str, _represent_str) | ||
|
||
# https://stackoverflow.com/questions/13518819/avoid-references-in-pyyaml | ||
class _NoAliasDumper(yaml.SafeDumper): | ||
"""Keep the yaml human-readable by avoiding anchors and aliases.""" | ||
|
||
# https://stackoverflow.com/questions/13518819/avoid-references-in-pyyaml | ||
def ignore_aliases(self, data): | ||
"""Keep the yaml human-readable by avoiding anchors and aliases.""" | ||
return True | ||
|
||
|
||
def yaml_dump(data): | ||
return yaml.dump( | ||
data, | ||
allow_unicode=True, | ||
Dumper=_NoAliasDumper) | ||
Dumper=_SafeDumper) |