mirror of
https://github.com/arsenetar/dupeguru.git
synced 2026-01-22 14:41:39 +00:00
Reorganized hscommon documentation
Removed hscommon's "docs" folder and moved all documentation directly into docstrings. Then, in dupeGuru's developer documentation, added autodoc references to relevant modules. The result is a much more usable hscommon documentation.
This commit is contained in:
@@ -4,9 +4,19 @@
|
||||
# which should be included with this package. The terms are also available at
|
||||
# http://www.hardcoded.net/licenses/bsd_license
|
||||
|
||||
"""Very simple inter-object notification system.
|
||||
|
||||
This module is a brain-dead simple notification system involving a :class:`Broadcaster` and a
|
||||
:class:`Listener`. A listener can only listen to one broadcaster. A broadcaster can have multiple
|
||||
listeners. If the listener is connected, whenever the broadcaster calls :meth:`~Broadcaster.notify`,
|
||||
the method with the same name as the broadcasted message is called on the listener.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
class Broadcaster:
|
||||
"""Broadcasts messages that are received by all listeners.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.listeners = set()
|
||||
|
||||
@@ -14,6 +24,10 @@ class Broadcaster:
|
||||
self.listeners.add(listener)
|
||||
|
||||
def notify(self, msg):
|
||||
"""Notify all connected listeners of ``msg``.
|
||||
|
||||
That means that each listeners will have their method with the same name as ``msg`` called.
|
||||
"""
|
||||
for listener in self.listeners.copy(): # listeners can change during iteration
|
||||
if listener in self.listeners: # disconnected during notification
|
||||
listener.dispatch(msg)
|
||||
@@ -23,6 +37,8 @@ class Broadcaster:
|
||||
|
||||
|
||||
class Listener:
|
||||
"""A listener is initialized with the broadcaster it's going to listen to. Initially, it is not connected.
|
||||
"""
|
||||
def __init__(self, broadcaster):
|
||||
self.broadcaster = broadcaster
|
||||
self._bound_notifications = defaultdict(list)
|
||||
@@ -38,9 +54,13 @@ class Listener:
|
||||
self._bound_notifications[message].append(func)
|
||||
|
||||
def connect(self):
|
||||
"""Connects the listener to its broadcaster.
|
||||
"""
|
||||
self.broadcaster.add_listener(self)
|
||||
|
||||
def disconnect(self):
|
||||
"""Disconnects the listener from its broadcaster.
|
||||
"""
|
||||
self.broadcaster.remove_listener(self)
|
||||
|
||||
def dispatch(self, msg):
|
||||
|
||||
Reference in New Issue
Block a user