Skip to content

Commit

Permalink
Merge pull request #79 from loganasherjones/issue/78
Browse files Browse the repository at this point in the history
Issue/78
  • Loading branch information
loganasherjones committed Jun 12, 2018
2 parents 54c505b + e7efed6 commit 36442fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -13,7 +13,7 @@
requirements = [
'six<2',
'python-box<4',
'watchdog',
'watchdog<1',
]

extras = {
Expand Down
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 36442fc

Please sign in to comment.