Merge lp:~cjwatson/storm/docstring-event into lp:storm

Proposed by Colin Watson
Status: Merged
Merged at revision: 564
Proposed branch: lp:~cjwatson/storm/docstring-event
Merge into: lp:storm
Diff against target: 67 lines (+39/-0)
1 file modified
storm/event.py (+39/-0)
To merge this branch: bzr merge lp:~cjwatson/storm/docstring-event
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Storm Developers Pending
Review via email: mp+384860@code.launchpad.net

Commit message

Add docstrings for storm.event.EventSystem.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/event.py'
2--- storm/event.py 2019-06-05 11:41:07 +0000
3+++ storm/event.py 2020-05-29 22:16:29 +0000
4@@ -29,12 +29,35 @@
5
6
7 class EventSystem(object):
8+ """A system for managing hooks that are called when events are emitted.
9+
10+ Hooks are callables that take the event system C{owner} as their first
11+ argument, followed by the arguments passed when emitting the event,
12+ followed by any additional C{data} arguments given when registering the
13+ hook.
14+
15+ Hooks registered for a given event C{name} are stored without ordering:
16+ no particular call order may be assumed when an event is emitted.
17+ """
18
19 def __init__(self, owner):
20+ """
21+ @param owner: The object that owns this event system. It is passed
22+ as the first argument to each hook function.
23+ """
24 self._owner_ref = weakref.ref(owner)
25 self._hooks = {}
26
27 def hook(self, name, callback, *data):
28+ """Register a hook.
29+
30+ @param name: The name of the event for which this hook should be
31+ called.
32+ @param callback: A callable which should be called when the event is
33+ emitted.
34+ @param data: Additional arguments to pass to the callable, after the
35+ C{owner} and any arguments passed when emitting the event.
36+ """
37 callbacks = self._hooks.get(name)
38 if callbacks is None:
39 self._hooks.setdefault(name, set()).add((callback, data))
40@@ -42,11 +65,27 @@
41 callbacks.add((callback, data))
42
43 def unhook(self, name, callback, *data):
44+ """Unregister a hook.
45+
46+ This ignores attempts to unregister hooks that were not already
47+ registered.
48+
49+ @param name: The name of the event for which this hook should no
50+ longer be called.
51+ @param callback: The callable to unregister.
52+ @param data: Additional arguments that were passed when registering
53+ the callable.
54+ """
55 callbacks = self._hooks.get(name)
56 if callbacks is not None:
57 callbacks.discard((callback, data))
58
59 def emit(self, name, *args):
60+ """Emit an event, calling any registered hooks.
61+
62+ @param name: The name of the event.
63+ @param args: Additional arguments to pass to hooks.
64+ """
65 owner = self._owner_ref()
66 if owner is not None:
67 callbacks = self._hooks.get(name)

Subscribers

People subscribed via source and target branches

to status/vote changes: