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

Commit c870eb7

Browse files
committed
Fixes tracking of changes in nested configs
1 parent 270f374 commit c870eb7

6 files changed

Lines changed: 80 additions & 25 deletions

File tree

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.29.1
2+
current_version = 1.29.2
33
commit = true
44
tag = false
55

configmanager/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '1.29.1'
1+
__version__ = '1.29.2'
22

33
from .managers import Config
44
from .items import Item

configmanager/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _value_changed(self, item, old_value, new_value):
2525

2626
def push(self):
2727
assert self.hook is None
28-
self.hook = self.config.hooks.register_hook(self.config.hooks.item_value_changed, self._value_changed)
28+
self.hook = self.config.hooks.item_value_changed.register_hook(self._value_changed)
2929
self.config._tracking_contexts.append(self)
3030
return self
3131

configmanager/sections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,9 @@ def _trigger_event(self, event_, **kwargs):
713713
if self.section:
714714
return self.section._trigger_event(event_, **kwargs)
715715

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

721721

tests/test_hooks.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -352,36 +352,57 @@ def item_value_changed(**kwargs):
352352

353353

354354
def test_hooks_work_across_nested_configs():
355-
calls = []
356-
357-
uploads_config = Config({
358-
'db': Config({
359-
'user': 'root',
360-
})
355+
config = Config({
356+
'a': Config({
357+
'aa': Config({
358+
'aaa': 'aaa-default',
359+
}),
360+
'ab': {
361+
'aba': 'aba-default',
362+
},
363+
'ac': 'ac-default',
364+
}),
365+
'b': {
366+
'ba': Config({
367+
'baa': 'baa-default',
368+
}),
369+
'bb': {
370+
'bba': 'bba-default',
371+
},
372+
'bc': 'bc-default',
373+
},
374+
'c': 'c-default',
361375
})
362376

363-
@uploads_config.hooks.item_value_changed
364-
def value_changed(**kwargs):
365-
calls.append(('uploads', kwargs))
377+
calls = []
366378

367-
config = Config({
368-
'uploads': uploads_config,
369-
'greeting': 'Hello',
370-
})
379+
@config.hooks.item_value_changed
380+
def item_value_changed(item):
381+
calls.append(('root', '.'.join(item.get_path())))
371382

372-
config.greeting.value = 'Hey!'
373383
assert len(calls) == 0
374384

375-
config.uploads.db.user.value = 'admin'
385+
config.c.value = 'c-1'
376386
assert len(calls) == 1
377387

378-
@config.hooks.item_value_changed
379-
def value_changed(**kwargs):
380-
calls.append(('main', kwargs))
388+
config.a.ac.value = 'ac-1'
389+
assert len(calls) == 2
381390

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

394+
config.a.ab.aba.value = 'aba-1'
395+
assert len(calls) == 4
396+
397+
config.b.bc.value = 'bc-1'
398+
assert len(calls) == 5
399+
400+
config.b.ba.baa.value = 'baa-1'
401+
assert len(calls) == 6
402+
403+
config.b.bb.bba.value = 'bba-1'
404+
assert len(calls) == 7
405+
385406

386407
def test_not_found_hook_not_handled_if_contains_raises_not_found(simple_config):
387408
calls = []

tests/test_track_changes.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,37 @@ def test_resets_single_item_changes():
232232

233233
assert config.a.value == 'aaa'
234234
assert config.b.value == 'BBB'
235+
236+
237+
def test_config_of_configs():
238+
config = Config({
239+
'uploads': Config({
240+
'a': 1,
241+
'b': True,
242+
'c': 'ccc',
243+
}),
244+
'downloads': Config({
245+
'd': {
246+
'e': 'eee',
247+
},
248+
'f': 'fff',
249+
}),
250+
})
251+
252+
with config.tracking_context() as ctx:
253+
config.uploads.a.value = 2
254+
config.downloads.d.e.value = 'EEE'
255+
config.downloads.f.value = 'FFF'
256+
257+
assert len(ctx.changes) == 3
258+
assert ctx.changes[config.uploads.a] == 2
259+
assert ctx.changes[config.downloads.d.e] == 'EEE'
260+
assert ctx.changes[config.downloads.f] == 'FFF'
261+
262+
assert not config.is_default
263+
assert config.uploads.a.value == 2
264+
assert config.downloads.d.e.value == 'EEE'
265+
assert config.downloads.f.value == 'FFF'
266+
267+
ctx.reset_changes()
268+
assert config.is_default

0 commit comments

Comments
 (0)