Skip to content

Commit 46faa94

Browse files
committed
Merge branch 'release/2.4.0'
2 parents 39b876d + c852136 commit 46faa94

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python:
44
- '2.7'
55
- '3.5'
66
- '3.6'
7+
- '3.7'
78
- pypy
89
install:
910
- pip install -U pip

doc/advanced.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ Handlers can be registered with *tokens*. A token is anything that supports equa
183183
184184
185185
186+
Registration Guard
187+
------------------
188+
189+
Sometimes it is desirable to have a way to enable/disable registrations dynamically. For this purpose, gossip supports guards on handlers. A guard is a callable object, returning whether or not the handler should be triggered
190+
191+
.. code-block:: python
192+
193+
>>> class State(object): enabled=False
194+
>>> state = State()
195+
>>> @gossip.register('guarded_hook', guard=lambda: state.enabled)
196+
... def handler1():
197+
... print('Called')
198+
>>> gossip.trigger('guarded_hook')
199+
>>> state.enabled = True
200+
>>> gossip.trigger('guarded_hook')
201+
Called
202+
203+
186204
Getting Hooks by Name
187205
---------------------
188206

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
=========
33

4+
* :release:`2.4.0 <03-09-2019>`
5+
* :feature:`39` Add ``guard`` argument to ``register``
46
* :release:`2.3.1 <28-08-2018>`
57
* :bug:`36` Ensure ``define_hook`` handles hook kwargs even if it already being defined
68
* :bug:`30` Switch to PBR

gossip/hooks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def trigger(self, kwargs, tags=None):
224224
while True:
225225
any_resolved = False
226226
for registration in list(registrations):
227-
if not registration.valid:
227+
if not registration.is_active():
228+
_logger.trace("Skipping {} because it is inactive", registration)
228229
continue
229230
if not registration.has_tags(tags):
230231
continue
@@ -343,7 +344,8 @@ def define(hook_name, **kwargs):
343344
return returned
344345

345346

346-
def register(func=None, hook_name=None, token=None, tags=None, needs=None, provides=None, reentrant=True, toggles_on=None, toggles_off=None, priority=0):
347+
def register(func=None, hook_name=None, token=None, tags=None, needs=None, provides=None, reentrant=True,
348+
toggles_on=None, toggles_off=None, priority=0, guard=None):
347349
"""Registers a new function to a hook
348350
349351
:param hook_name: full name of hook to register to
@@ -374,13 +376,14 @@ def register(func=None, hook_name=None, token=None, tags=None, needs=None, provi
374376
toggles_on=toggles_on,
375377
toggles_off=toggles_off,
376378
priority=priority,
379+
guard=guard,
377380
)
378381
assert hook_name is not None
379382
registration = get_or_create_hook(
380383
hook_name).register(
381384
func, token=token, tags=tags, needs=needs, provides=provides, reentrant=reentrant,
382385
toggles_on=toggles_on, toggles_off=toggles_off,
383-
priority=priority,
386+
priority=priority, guard=guard,
384387
)
385388
assert registration
386389
return func

gossip/registration.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
class Registration(object):
1515

16-
def __init__(self, func, hook, token=None, tags=None, needs=None, provides=None, reentrant=True, toggles_on=None, toggles_off=None, priority=0):
16+
def __init__(self, func, hook, token=None, tags=None, needs=None, provides=None, reentrant=True, toggles_on=None,
17+
toggles_off=None, priority=0, guard=None):
1718
super(Registration, self).__init__()
1819

1920
assert not (toggles_off is not None and toggles_on is not None), 'Cannot specify both toggles_on and toggles_off'
21+
assert (guard is None) or callable(guard), "Registration 'guard' argument must be callable if specified"
2022
self.id = next(_registration_id)
2123
self.hook = hook
2224
self.func = func
@@ -33,6 +35,7 @@ def __init__(self, func, hook, token=None, tags=None, needs=None, provides=None,
3335
self._toggles_on = toggles_on
3436
self._toggles_off = toggles_off
3537
self._priority = priority
38+
self._guard = guard
3639

3740
def get_priority(self):
3841
return self._priority
@@ -55,7 +58,11 @@ def unregister(self):
5558
assert self.hook is None
5659

5760
def is_active(self):
58-
return self.hook is not None
61+
if (self.hook is None) or (not self.valid):
62+
return False
63+
if self._guard is None:
64+
return True
65+
return self._guard()
5966

6067
def is_being_called(self):
6168
return self._is_being_called
@@ -91,7 +98,7 @@ def _check_toggle(self):
9198
return True
9299

93100
def __repr__(self):
94-
return repr(self.func)
101+
return "<{}: {}>".format(self.__class__.__name__, self.func)
95102

96103

97104
def _normalize_deps(deps):

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ classifiers =
44
Programming Language :: Python :: 2.7
55
Programming Language :: Python :: 3.5
66
Programming Language :: Python :: 3.6
7+
Programming Language :: Python :: 3.7
78
description = Signaling and hooking library
89
license = BSD3
910
author = Rotem Yaari

0 commit comments

Comments
 (0)