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

Commit

Permalink
* Make hook names easier to discover for IDEs
Browse files Browse the repository at this point in the history
* Docs update
  • Loading branch information
jbasko committed Jun 17, 2017
1 parent 990657a commit 270f374
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 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.0
current_version = 1.29.1
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.0'
__version__ = '1.29.1'

from .managers import Config
from .items import Item
Expand Down
13 changes: 10 additions & 3 deletions configmanager/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def settings(self):
return self._settings

def tracking_context(self):
"""
Returns:
_TrackingContext
"""
return _TrackingContext(self)

@property
Expand All @@ -187,7 +191,7 @@ def configparser(self):
``ConfigParser`` (or the backported configparser module in Python 2).
Returns:
:class:`.ConfigPersistenceAdapter`
ConfigPersistenceAdapter
"""
if self._configparser_adapter is None:
self._configparser_adapter = ConfigPersistenceAdapter(
Expand All @@ -204,7 +208,7 @@ def json(self):
Adapter to dump/load JSON format strings and files.
Returns:
:class:`.ConfigPersistenceAdapter`
ConfigPersistenceAdapter
"""
if self._json_adapter is None:
self._json_adapter = ConfigPersistenceAdapter(
Expand All @@ -219,7 +223,7 @@ def yaml(self):
Adapter to dump/load YAML format strings and files.
Returns:
:class:`.ConfigPersistenceAdapter`
ConfigPersistenceAdapter
"""
if self._yaml_adapter is None:
self._yaml_adapter = ConfigPersistenceAdapter(
Expand All @@ -232,6 +236,9 @@ def yaml(self):
def click(self):
"""
click extension
Returns:
ClickExtension
"""
if self._click_extension is None:
from .click_ext import ClickExtension
Expand Down
19 changes: 12 additions & 7 deletions configmanager/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
}


class _SectionHooks(HookRegistry):
def __init__(self, section):
super(_SectionHooks, self).__init__(section)
self.not_found = self.register_event('not_found')
self.item_added_to_section = self.register_event('item_added_to_section')
self.section_added_to_section = self.register_event('section_added_to_section')
self.item_value_changed = self.register_event('item_value_changed')


class Section(BaseSection):
"""
Represents a section consisting of items (instances of :class:`.Item`) and other sections
Expand All @@ -43,12 +52,8 @@ def __init__(self, schema=None, section=None):
#: Alias of this section with which it was added to its parent section
self._section_alias = None

#: Hooks registry
self._hooks = HookRegistry(self)
self._hooks.not_found = self._hooks.register_event('not_found')
self._hooks.item_added_to_section = self._hooks.register_event('item_added_to_section')
self._hooks.section_added_to_section = self._hooks.register_event('section_added_to_section')
self._hooks.item_value_changed = self._hooks.register_event('item_value_changed')
# Hooks registry
self._hooks = _SectionHooks(self)

# Listen for hook registration so we can enable per-section hooks only when they
# are actually used.
Expand Down Expand Up @@ -257,7 +262,7 @@ def get_proxy(self, *key):
def hooks(self):
"""
Returns:
HookRegistry
_SectionHooks
"""
return self._hooks

Expand Down
16 changes: 14 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,14 @@ to be called when this exception is raised:
If this function returns anything other than ``None``, the exception will not be raised.

How can I track changes of config values?
-----------------------------------------
How do I manage changesets of config values?
--------------------------------------------

.. code-block:: python
:emphasize-lines: 4,7,10,13,14
>>> config.greeting.value
'Hello, world!'
>>> with config.tracking_context() as ctx:
... config.greeting.value = 'Hey, what is up!'
Expand All @@ -339,3 +343,11 @@ How can I track changes of config values?
>>> ctx.changes[config.greeting]
'Hey, what is up!'
>>> ctx.reset_changes()
>>> ctx.changes
{}
>>> config.greeting.value
'Hello, world!'

0 comments on commit 270f374

Please sign in to comment.