Skip to content

Commit

Permalink
Merge pull request #127 from jacebrowning/enable-old-serialization
Browse files Browse the repository at this point in the history
Add an option to use unindented YAML blocks
  • Loading branch information
jacebrowning committed Aug 31, 2019
2 parents fbd2ea2 + 81ef70e commit c081616
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.5 (unreleased)

- Added an `INDENT_YAML_BLOCKS` setting to optionally use the old serialization behavior.

# 0.4.1 (2019-08-25)

- Fixed a `TypeError` when converting non-builtin attributes.
Expand Down
2 changes: 1 addition & 1 deletion datafiles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import field

from . import converters
from . import converters, settings
from .decorators import auto, datafile
from .manager import Missing
from .model import Model
15 changes: 10 additions & 5 deletions datafiles/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import tomlkit
from ruamel import yaml

from . import settings


class Formatter(metaclass=ABCMeta):
"""Base class for object serialization and text deserialization."""
Expand Down Expand Up @@ -77,11 +79,14 @@ def deserialize(cls, file_object):

@classmethod
def serialize(cls, data):
f = StringIO()
y = yaml.YAML()
y.indent(mapping=2, sequence=4, offset=2)
y.dump(data, f)
text = f.getvalue().strip() + '\n'
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 ""
return "" if text == "{}\n" else text


Expand Down
4 changes: 2 additions & 2 deletions datafiles/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Shared configuration flags."""

HOOKS_ENABLED = True

HIDE_TRACEBACK_IN_HOOKS = True
HOOKS_ENABLED = True
INDENT_YAML_BLOCKS = True
33 changes: 32 additions & 1 deletion datafiles/tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,38 @@

import pytest

from datafiles import formats
from datafiles import formats, settings
from datafiles.utils import dedent


def describe_serialize():
@pytest.fixture
def data():
return {'key': "value", 'items': [1, 2]}

def describe_yaml():
def it_indents_blocks_by_default(expect, data):
text = formats.YAML.serialize(data)
expect(text) == dedent(
"""
key: value
items:
- 1
- 2
"""
)

def it_can_render_lists_inline(expect, data, monkeypatch):
monkeypatch.setattr(settings, 'INDENT_YAML_BLOCKS', False)
text = formats.YAML.serialize(data)
expect(text) == dedent(
"""
key: value
items:
- 1
- 2
"""
)


def describe_deserialize():
Expand Down
54 changes: 54 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<h1>Settings</h1>

For libraries that wish to temporarly alter any of the previosly described behavior, a handful of settings can be set at the module level. All boolean settings default to `True` unless otherwise noted.

# `HIDE_TRACEBACK_IN_HOOKS`

When an exception occurs in patched method, this traceback is hidden by default for `pytest`. If this information is required to debug a complex issue enable it as follows:

```python
import datafiles

datafiles.settings.HIDE_TRACEBACK_IN_HOOKS = False
```

# `HOOKS_ENABLED`

When running unit tests for a library using `datafiles`,
it can be helpful to disable automatic loading/saving in patched methods for performance:

```python
def pytest_runtest_setup(item):
"""Disable file storage during unit tests."""
datafiles.settings.HOOKS_ENABLED = False
```



# `INDENT_YAML_BLOCKS`

In `datafiles >= 0.4`, YAML blocks are now indented by default, like so:

```yaml
items:
- 1
- 2
- 3
```

To make it easier to upgrade to this version, a library can disable this functionality:

```python
import datafiles

datafiles.settings.INDENT_YAML_BLOCKS = False
```

to produce YAML like:

```yaml
items:
- 1
- 2
- 3
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nav:
- Custom Types: types/custom.md
- File Formats: formats.md
- Utilities: utilities.md
- Settings: settings.md
- About:
- Release Notes: about/changelog.md
- Contributing: about/contributing.md
Expand Down
8 changes: 4 additions & 4 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
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.4.1"
version = "0.5b1"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit c081616

Please sign in to comment.