Skip to content

Commit

Permalink
Added dump_default_config()
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarteberg committed Jan 7, 2019
1 parent 730b6cc commit 76f2031
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion confiddler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from jsonschema import ValidationError
from .core import load_config, dump_default_config, validate, emit_defaults, flow_style, convert_to_base_types
from .core import load_config, dump_config, dump_default_config, validate, emit_defaults, flow_style, convert_to_base_types
from . import json
19 changes: 19 additions & 0 deletions confiddler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ def _load_config(f, schema):
return _load_config(path_or_file, schema)


def dump_config(config_data, path_or_file=None):
"""
Convenience wrapper for YAML().dump()
Dump the given config data to the given path or file.
If no path or file is given, return it as a string.
"""
if path_or_file is None:
f = io.StringIO()
yaml.dump(config_data, f)
f.seek(0)
return f.getvalue()
elif isinstance(path_or_file, PathLike):
with open(path_or_file, 'w') as f:
yaml.dump(config_data, f)
else:
yaml.dump(config_data, path_or_file)


def dump_default_config(schema, f=None, format="yaml"): #@ReservedAssignment
"""
Convenience wrapper around :py:func:`emit_defaults()`.
Expand Down
12 changes: 11 additions & 1 deletion confiddler/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import copy
import textwrap
import tempfile
from io import StringIO

import pytest
from ruamel.yaml import YAML

from confiddler import load_config, emit_defaults, validate, dump_default_config, flow_style, ValidationError, convert_to_base_types
from confiddler import (load_config, emit_defaults, validate, dump_default_config,
flow_style, ValidationError, convert_to_base_types, dump_config)

yaml = YAML()
yaml.default_flow_style = False
Expand Down Expand Up @@ -309,5 +311,13 @@ def test_convert_to_base_types():
assert type(d2['b']) == dict


def test_dump_config():
data = {'a': flow_style([1,2,3])}
dumped = dump_config(data)
expected = textwrap.dedent("""\
a: [1, 2, 3]
""")
assert dumped == expected

if __name__ == "__main__":
pytest.main(['-s', '--tb=native', '--pyargs', 'confiddler.tests.test_core'])

0 comments on commit 76f2031

Please sign in to comment.