Skip to content
Snippets Groups Projects
Commit 93db5c85 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Notifier has listener enable/disable/skip methods

parent e6831b51
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@ as a mixin for providing a simple notification API.
import logging
import inspect
import contextlib
import collections
import props
......@@ -156,6 +157,49 @@ class Notifier(object):
log.debug('{}: De-registered listener {}'.format(
type(self).__name__, listener))
def enable(self, name, topic=None):
"""Enables the specified listener. """
if topic is None:
topic = DEFAULT_TOPIC
self.__listeners[topic][name].enabled = True
def disable(self, name, topic=None):
"""Disables the specified listener. """
if topic is None:
topic = DEFAULT_TOPIC
self.__listeners[topic][name].enabled = False
def isEnabled(self, name, topic=None):
"""Returns ``True`` if the specified listener is enabled, ``False``
otherwise.
"""
if topic is None:
topic = DEFAULT_TOPIC
return self.__listeners[topic][name].enabled
@contextlib.contextmanager
def skip(self, name, topic=None):
"""Context manager which disables the speciifed listener, and
restores its state before returning.
"""
state = self.isEnabled(name, topic)
self.disable(name, topic)
try:
yield
finally:
if state: self.enable( name, topic)
else: self.disable(name, topic)
def notify(self, *args, **kwargs):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment