Skip to content

Commit

Permalink
Merge pull request #75 from adamrothman/master
Browse files Browse the repository at this point in the history
Add ConfigNamespace::get_config_dict
  • Loading branch information
dnephin committed Mar 29, 2016
2 parents ad21162 + caa0269 commit 45a3d07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions staticconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def get_config_values(self):
"""
return self.configuration_values

def get_config_dict(self):
"""Reconstruct the nested structure of this object's configuration
and return it as a dict.
"""
config_dict = {}
for dotted_key, value in self.get_config_values().items():
subkeys = dotted_key.split('.')
d = config_dict
for key in subkeys:
d = d.setdefault(key, value if key == subkeys[-1] else {})
return config_dict

def get_known_keys(self):
return set(vproxy.config_key for vproxy in self.get_value_proxies())

Expand Down
2 changes: 1 addition & 1 deletion staticconf/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

version = "0.9.0"
version = "0.9.1"
25 changes: 25 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ def test_get_config_values(self):
values = self.namespace.get_config_values()
assert_equal(values, {'stars': 'foo'})

def test_get_config_dict(self):
self.namespace['one.two.three.four'] = 5
self.namespace['one.two.three.five'] = 'six'
self.namespace['one.b.cats'] = [1, 2, 3]
self.namespace['a.two'] = 'c'
self.namespace['first'] = True
d = self.namespace.get_config_dict()
assert_equal(d, {
'one': {
'b': {
'cats': [1, 2, 3],
},
'two': {
'three': {
'four': 5,
'five': 'six',
},
},
},
'a': {
'two': 'c',
},
'first': True,
})

def test_get_known_keys(self):
proxies = [mock.Mock(), mock.Mock()]
for mock_proxy in proxies:
Expand Down

0 comments on commit 45a3d07

Please sign in to comment.