Skip to content

Commit

Permalink
Fixes #78
Browse files Browse the repository at this point in the history
Now if a box object is given to dump, it will call `to_dict`
on it before attempting to serialize it with `json` or `yaml`.
This prevents random, interal box information from showing up.
  • Loading branch information
loganasherjones committed Jun 12, 2018
1 parent 54c505b commit d7ad8fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/yapconf_test.py
Expand Up @@ -2,6 +2,8 @@
import os

import pytest
from box import Box
from mock import patch

import yapconf

Expand Down Expand Up @@ -111,6 +113,31 @@ def test_dump_data(tmpdir, data, file_type):
assert data == yapconf.load_file(filename, file_type)


def test_dump_box(ascii_data):
data = {'writes': ''}

def mock_write(x):
data['writes'] += x

writes = [
'db:\n',
' name: db_name\n',
' port: 123\n',
'foo: bar\n',
'items:\n',
'- 1\n',
'- 2\n',
'- 3\n',
]

boxed_data = Box(ascii_data)
with patch('sys.stdout') as mock_stdout:
mock_stdout.write = mock_write
yapconf.dump_data(boxed_data, file_type='yaml')

assert data['writes'] == ''.join(writes)


@pytest.mark.parametrize('original,expected', [
({}, {}),
({'foo': 'bar'}, {'foo': 'bar'}),
Expand Down
4 changes: 4 additions & 0 deletions yapconf/__init__.py
Expand Up @@ -6,6 +6,7 @@
import sys

import six
from box import Box

if sys.version_info.major < 3:
from io import open
Expand Down Expand Up @@ -152,6 +153,9 @@ def _dump(data, stream, file_type, **kwargs):
'encoding': 'utf-8'
}

if isinstance(data, Box):
data = data.to_dict()

if str(file_type).lower() == 'json':
dumped = json.dumps(data, **kwargs)
if isinstance(dumped, unicode):
Expand Down

0 comments on commit d7ad8fe

Please sign in to comment.