Skip to content

Commit

Permalink
Merge pull request #6 from jedipi/feature/overwriting-yaml-with-missi…
Browse files Browse the repository at this point in the history
…ng-subsections

support layering a yaml file over another without having all sections
  • Loading branch information
staffanm committed Jul 27, 2016
2 parents 9e9b4f0 + e66033b commit 8e74bed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions layeredconfig/yamlfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, yamlfilename=None, writable=True, **kwargs):
yamlfilename = kwargs['parent'].yamlfilename
if 'defaults' in kwargs:
self.source = kwargs['defaults']
elif kwargs.get('empty', False):
self.source = {}
else:
with codecs.open(yamlfilename, encoding="utf-8") as fp:
# do we need safe_load?
Expand Down
31 changes: 31 additions & 0 deletions tests/test_layeredconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,37 @@ def test_write(self):
got = fp.read().replace("\r\n", "\n")
self.assertEqual(want, got)


class YamlOverwritingTestCase(unittest.TestCase):
def tearDown(self):
os.unlink('source1.yaml')
os.unlink('source2.yaml')

def test_overwriting_yaml_with_missing_subsections(self):
with open('source1.yaml', 'w') as fp:
fp.write("""
section:
key1: key1_value1
subsection:
subsection_key1: subsection_key1_value1
""")

with open('source2.yaml', 'w') as fp:
fp.write("""
section:
key1: key1_value2
""")
config = LayeredConfig(
YAMLFile('source1.yaml'),
YAMLFile('source2.yaml')
)
self.assertEquals(config.section.key1, 'key1_value2')
self.assertEquals(
config.section.subsection.subsection_key1,
'subsection_key1_value1'
)


class TestPListFile(unittest.TestCase, TestConfigSourceHelper):

supported_types = (str, int, bool, list, datetime)
Expand Down

0 comments on commit 8e74bed

Please sign in to comment.