Skip to content

Commit

Permalink
Merge pull request #211 from jacebrowning/update-yaml
Browse files Browse the repository at this point in the history
Update ruamel.yaml
  • Loading branch information
jacebrowning committed Apr 17, 2021
2 parents 288fd0a + e67c35b commit e31f190
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
30 changes: 13 additions & 17 deletions datafiles/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,29 @@ def extensions(cls):

@classmethod
def deserialize(cls, file_object):
from ruamel import yaml
from ruamel.yaml import YAML

yaml = YAML()
yaml.preserve_quotes = True # type: ignore
try:
return yaml.round_trip_load(file_object, preserve_quotes=True)
return yaml.load(file_object)
except NotImplementedError as e:
log.error(str(e))
return {}

@classmethod
def serialize(cls, data):
from ruamel import yaml

yaml.representer.RoundTripRepresenter.add_representer(
types.List, yaml.representer.RoundTripRepresenter.represent_list
)
yaml.representer.RoundTripRepresenter.add_representer(
types.Dict, yaml.representer.RoundTripRepresenter.represent_dict
)
from ruamel.yaml import YAML

yaml = YAML()
yaml.register_class(types.List)
yaml.register_class(types.Dict)
if settings.INDENT_YAML_BLOCKS:
f = StringIO()
y = yaml.YAML()
y.indent(mapping=2, sequence=4, offset=2)
y.dump(data, f)
text = f.getvalue().strip() + '\n'
else:
text = yaml.round_trip_dump(data) or ""
yaml.indent(mapping=2, sequence=4, offset=2)

stream = StringIO()
yaml.dump(data, stream)
text = stream.getvalue().strip() + '\n'

if text == "{}\n":
return ""
Expand Down
8 changes: 8 additions & 0 deletions datafiles/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
class List(list):
"""Patchable `list` type."""

@classmethod
def to_yaml(cls, representer, node):
return representer.represent_list(node)


class Dict(dict):
"""Patchable `dict` type."""

@classmethod
def to_yaml(cls, representer, node):
return representer.represent_dict(node)
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ python = "^3.7"

# Formats
PyYAML = "^5.2"
"ruamel.yaml" = "~0.16.10"
"ruamel.yaml" = "~0.17.4"
tomlkit = "~0.5.3"

# ORM
Expand Down

0 comments on commit e31f190

Please sign in to comment.