Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pop() method to ConfigTree and added KeyError to ConfigMissingException #120

Merged
merged 2 commits into from
Aug 16, 2017
Merged
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
28 changes: 28 additions & 0 deletions pyhocon/config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,34 @@ def get_string(self, key, default=UndefinedKey):
return string_value.lower()
return string_value

def pop(self, key, default=UndefinedKey):
"""Remove specified key and return the corresponding value.
If key is not found, default is returned if given, otherwise ConfigMissingException is raised

This method assumes the user wants to remove the last value in the chain so it parses via parse_key
and pops the last value out of the dict.

:param key: key to use (dot separated). E.g., a.b.c
:type key: basestring
:param default: default value if key not found
:type default: object
:param default: default value if key not found
:return: value in the tree located at key
"""
value = self.get(key, default)
if value == default:
return default

lst = ConfigTree.parse_key(key)
parent = self.KEY_SEP.join(lst[0:-1])
child = lst[-1]

if parent:
self.get(parent).__delitem__(child)
else:
self.__delitem__(child)
return value

def get_int(self, key, default=UndefinedKey):
"""Return int representation of value found at key

Expand Down
2 changes: 1 addition & 1 deletion pyhocon/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def __init__(self, message, ex=None):
self._exception = ex


class ConfigMissingException(ConfigException):
class ConfigMissingException(ConfigException, KeyError):
pass


Expand Down
55 changes: 55 additions & 0 deletions tests/test_config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,58 @@ def test_contains_with_quoted_keys(self):
assert 'a.c' not in config_tree
assert 'a.b."c.d"' in config_tree
assert 'a.b.c.d' not in config_tree

def test_configtree_pop(self):
config_tree = ConfigTree()
config_tree.put("string", "string")
assert config_tree.pop("string", "default") == "string"
assert config_tree.pop("string-new", "default") == "default"
assert config_tree == ConfigTree()

with pytest.raises(ConfigMissingException):
assert config_tree.pop("string-new")

config_tree.put("list", [1, 2, 3])
assert config_tree.pop("list", [4]) == [1, 2, 3]
assert config_tree.pop("list-new", [4]) == [4]
assert config_tree == ConfigTree()

config_tree.put("config", {'a': 5})
assert config_tree.pop("config", {'b': 1}) == {'a': 5}
assert config_tree.pop("config-new", {'b': 1}) == {'b': 1}
assert config_tree == ConfigTree()

config_tree = ConfigTree()
config_tree.put('a.b.c.one', 1)
config_tree.put('a.b.c.two', 2)
config_tree.put('"f.k".g.three', 3)

exp = OrderedDict()
exp['a'] = OrderedDict()
exp['a']['b'] = OrderedDict()
exp['a']['b']['c'] = OrderedDict()
exp['a']['b']['c']['one'] = 1
exp['a']['b']['c']['two'] = 2

exp['f.k'] = OrderedDict()
exp['f.k']['g'] = OrderedDict()
exp['f.k']['g']['three'] = 3

assert config_tree.pop('a.b.c').as_plain_ordered_dict() == exp['a']['b']['c']
assert config_tree.pop('a.b.c', None) is None

with pytest.raises(ConfigMissingException):
assert config_tree.pop('a.b.c')
with pytest.raises(ConfigMissingException):
assert config_tree['a']['b'].pop('c')

assert config_tree.pop('a').as_plain_ordered_dict() == OrderedDict(b=OrderedDict())
assert config_tree.pop('"f.k"').as_plain_ordered_dict() == OrderedDict(g=OrderedDict(three=3))
assert config_tree.as_plain_ordered_dict() == OrderedDict()

def test_keyerror_raised(self):
config_tree = ConfigTree()
config_tree.put("a", {'b': 5})

with pytest.raises(KeyError):
assert config_tree['c']