Skip to content
This repository has been archived by the owner on Feb 11, 2023. It is now read-only.

Commit

Permalink
Fixes tracking of changes in nested configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbasko committed Jun 17, 2017
1 parent 270f374 commit c870eb7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.29.1
current_version = 1.29.2
commit = true
tag = false

Expand Down
2 changes: 1 addition & 1 deletion configmanager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.29.1'
__version__ = '1.29.2'

from .managers import Config
from .items import Item
Expand Down
2 changes: 1 addition & 1 deletion configmanager/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _value_changed(self, item, old_value, new_value):

def push(self):
assert self.hook is None
self.hook = self.config.hooks.register_hook(self.config.hooks.item_value_changed, self._value_changed)
self.hook = self.config.hooks.item_value_changed.register_hook(self._value_changed)
self.config._tracking_contexts.append(self)
return self

Expand Down
6 changes: 3 additions & 3 deletions configmanager/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,9 @@ def _trigger_event(self, event_, **kwargs):
if self.section:
return self.section._trigger_event(event_, **kwargs)

elif self.is_config and self.section:
# Settings only apply to within one Config instance in the tree.
# Hooks still may need to be called in parent Configs.
elif self.section:
# Settings only apply to one section, so must still let
# parent sections trigger the event
self.section._trigger_event(event_, **kwargs)


Expand Down
59 changes: 40 additions & 19 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,36 +352,57 @@ def item_value_changed(**kwargs):


def test_hooks_work_across_nested_configs():
calls = []

uploads_config = Config({
'db': Config({
'user': 'root',
})
config = Config({
'a': Config({
'aa': Config({
'aaa': 'aaa-default',
}),
'ab': {
'aba': 'aba-default',
},
'ac': 'ac-default',
}),
'b': {
'ba': Config({
'baa': 'baa-default',
}),
'bb': {
'bba': 'bba-default',
},
'bc': 'bc-default',
},
'c': 'c-default',
})

@uploads_config.hooks.item_value_changed
def value_changed(**kwargs):
calls.append(('uploads', kwargs))
calls = []

config = Config({
'uploads': uploads_config,
'greeting': 'Hello',
})
@config.hooks.item_value_changed
def item_value_changed(item):
calls.append(('root', '.'.join(item.get_path())))

config.greeting.value = 'Hey!'
assert len(calls) == 0

config.uploads.db.user.value = 'admin'
config.c.value = 'c-1'
assert len(calls) == 1

@config.hooks.item_value_changed
def value_changed(**kwargs):
calls.append(('main', kwargs))
config.a.ac.value = 'ac-1'
assert len(calls) == 2

uploads_config.db.user.value = 'Administrator'
config.a.aa.aaa.value = 'aaa-1'
assert len(calls) == 3

config.a.ab.aba.value = 'aba-1'
assert len(calls) == 4

config.b.bc.value = 'bc-1'
assert len(calls) == 5

config.b.ba.baa.value = 'baa-1'
assert len(calls) == 6

config.b.bb.bba.value = 'bba-1'
assert len(calls) == 7


def test_not_found_hook_not_handled_if_contains_raises_not_found(simple_config):
calls = []
Expand Down
34 changes: 34 additions & 0 deletions tests/test_track_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,37 @@ def test_resets_single_item_changes():

assert config.a.value == 'aaa'
assert config.b.value == 'BBB'


def test_config_of_configs():
config = Config({
'uploads': Config({
'a': 1,
'b': True,
'c': 'ccc',
}),
'downloads': Config({
'd': {
'e': 'eee',
},
'f': 'fff',
}),
})

with config.tracking_context() as ctx:
config.uploads.a.value = 2
config.downloads.d.e.value = 'EEE'
config.downloads.f.value = 'FFF'

assert len(ctx.changes) == 3
assert ctx.changes[config.uploads.a] == 2
assert ctx.changes[config.downloads.d.e] == 'EEE'
assert ctx.changes[config.downloads.f] == 'FFF'

assert not config.is_default
assert config.uploads.a.value == 2
assert config.downloads.d.e.value == 'EEE'
assert config.downloads.f.value == 'FFF'

ctx.reset_changes()
assert config.is_default

0 comments on commit c870eb7

Please sign in to comment.