aboutsummaryrefslogtreecommitdiff
path: root/src_py/hatter/util.py
diff options
context:
space:
mode:
authorbozo.kopic <bozo.kopic@gmail.com>2017-08-22 16:01:58 +0200
committerbozo.kopic <bozo.kopic@gmail.com>2017-08-22 16:01:58 +0200
commitb82c0e53536dc100fbe93806dcd0062c197f4f83 (patch)
treec48d9747c2654305b2b28196b238e57496ae5617 /src_py/hatter/util.py
parentd269714518d0f31b4774c83e29ac8aee3e416a0e (diff)
backend
Diffstat (limited to 'src_py/hatter/util.py')
-rw-r--r--src_py/hatter/util.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/src_py/hatter/util.py b/src_py/hatter/util.py
index 5d23751..a4fff55 100644
--- a/src_py/hatter/util.py
+++ b/src_py/hatter/util.py
@@ -103,3 +103,45 @@ def monkeypatch_sqlite3():
return val
sqlite3.register_converter("timestamp", _sqlite_convert_timestamp)
+
+
+class RegisterCallbackHandle(collections.namedtuple(
+ 'RegisterCallbackHandle', ['cancel'])):
+ """Handle used for canceling callback registration
+
+ Attributes:
+ cancel (Callable[[],None]): cancel registered callback
+
+ """
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.cancel()
+
+
+class CallbackRegistry:
+ """Callback registry"""
+
+ def __init__(self):
+ self._cbs = []
+
+ def register(self, cb):
+ """Register callback
+
+ Args:
+ cb (Callable): callback
+
+ Returns:
+ RegisterCallbackHandle
+
+ """
+ self.cbs.append(cb)
+ return RegisterCallbackHandle(lambda: self.cbs.remove(cb))
+
+ def notify(self, *args, **kwargs):
+ """Notify all registered callbacks"""
+
+ for cb in self._cbs:
+ cb(*args, **kwargs)