Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"pytest-cov",
"pytest-runner",
"python-etcd",
"ruamel.yaml",
"ruamel.yaml>=0.15",
"tox",
],
"yaml": ["ruamel.yaml"],
"yaml": ["ruamel.yaml>=0.15"],
}

setup(
Expand Down
13 changes: 8 additions & 5 deletions tests/spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,18 @@ def test_migrate_config_file_does_not_exist_create(basic_spec):
@patch('os.path.isfile', Mock(return_value=False))
def test_migrate_config_file_create_yaml(basic_spec):
open_path = 'yapconf.open'
with patch('yapconf.yaml.safe_dump') as dump_mock:
with patch('yapconf.yaml.dump') as dump_mock:
with patch(open_path, mock_open()) as mock_file:
new_config = basic_spec.migrate_config_file(
'/path/to/file', create=True, output_file_type='yaml')
dump_mock.assert_called_with(
if type(yapconf.yaml).__name__== "YAML":
dump_mock.assert_called_with(
{"foo": None},
mock_file())
else:
dump_mock.assert_called_with(
{"foo": None},
mock_file(),
default_flow_style=False,
encoding='utf-8')
mock_file(), default_flow_style=False, encoding='utf-8')

assert new_config == {"foo": None}

Expand Down
23 changes: 14 additions & 9 deletions yapconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@
# we want our code to be the same whether or not PyYaml or
# ruamel.yaml is installed.
import ruamel.yaml as yaml
from ruamel.yaml import YAML

# ruamel.yaml depricated support for safe_load in 0.17.0
if packaging.version.parse(yaml.__version__) < packaging.version.parse("0.18.0"):
yaml.load = yaml.safe_load
else:
from ruamel.yaml import YAML

yaml = YAML(typ="safe", pure=True)
yaml = YAML(typ="safe", pure=True)


except ImportError:
Expand Down Expand Up @@ -183,7 +178,13 @@ def _dump(data, stream, file_type, **kwargs):
else:
stream.write(six.u(dumped))
elif str(file_type).lower() == "yaml":
yaml.safe_dump(data, stream, **kwargs)

# Depending on the yaml module loaded, need to handle arguments differently
if type(yaml).__name__ == "YAML":
yaml.default_flow_style = kwargs.get("default_flow_style", False)
yaml.dump(data, stream)
else:
yaml.dump(data, stream, **kwargs)
else:
raise NotImplementedError(
"Someone forgot to implement dump for file " "type: %s" % file_type
Expand Down Expand Up @@ -225,7 +226,11 @@ def load_file(
if str(file_type).lower() == "json":
data = json.load(conf_file, **load_kwargs)
elif str(file_type).lower() == "yaml":
data = yaml.load(conf_file.read())
# Depending on the yaml module loaded, need to handle arguments differently
if type(yaml).__name__ == "YAML":
data = yaml.load(conf_file.read())
else:
data = yaml.safe_load(conf_file.read())
else:
raise NotImplementedError(
"Someone forgot to implement how to load a %s file_type." % file_type
Expand Down
9 changes: 7 additions & 2 deletions yapconf/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,14 @@ def get_data(self):

nested_config = result.data[self.key]
if self.config_type == "json":
return json.loads(nested_config,)
return json.loads(
nested_config,
)
elif self.config_type == "yaml":
return yapconf.yaml.load(nested_config)
if type(yapconf.yaml).__name__ == "YAML":
return yapconf.yaml.load(nested_config)
else:
return yapconf.yaml.safe_load(nested_config)
else:
raise NotImplementedError(
"Cannot load config with type %s" % self.config_type
Expand Down
Loading