Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/rdflib/events.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 from __future__ import absolute_import | |
2 from __future__ import division | |
3 from __future__ import print_function | |
4 | |
5 __doc__ = """ | |
6 Dirt Simple Events | |
7 | |
8 A Dispatcher (or a subclass of Dispatcher) stores event handlers that | |
9 are 'fired' simple event objects when interesting things happen. | |
10 | |
11 Create a dispatcher: | |
12 | |
13 >>> d = Dispatcher() | |
14 | |
15 Now create a handler for the event and subscribe it to the dispatcher | |
16 to handle Event events. A handler is a simple function or method that | |
17 accepts the event as an argument: | |
18 | |
19 >>> def handler1(event): print(repr(event)) | |
20 >>> d.subscribe(Event, handler1) | |
21 | |
22 Now dispatch a new event into the dispatcher, and see handler1 get | |
23 fired: | |
24 | |
25 >>> d.dispatch(Event(foo='bar', data='yours', used_by='the event handlers')) | |
26 <rdflib.events.Event ['data', 'foo', 'used_by']> | |
27 """ | |
28 | |
29 __all__ = ['Event', 'Dispatcher'] | |
30 | |
31 | |
32 class Event(object): | |
33 """ | |
34 An event is a container for attributes. The source of an event | |
35 creates this object, or a subclass, gives it any kind of data that | |
36 the events handlers need to handle the event, and then calls | |
37 notify(event). | |
38 | |
39 The target of an event registers a function to handle the event it | |
40 is interested with subscribe(). When a sources calls | |
41 notify(event), each subscriber to that event will be called in no | |
42 particular order. | |
43 """ | |
44 | |
45 def __init__(self, **kw): | |
46 self.__dict__.update(kw) | |
47 | |
48 def __repr__(self): | |
49 attrs = sorted(self.__dict__.keys()) | |
50 return '<rdflib.events.Event %s>' % ([a for a in attrs],) | |
51 | |
52 | |
53 class Dispatcher(object): | |
54 """ | |
55 An object that can dispatch events to a privately managed group of | |
56 subscribers. | |
57 """ | |
58 | |
59 _dispatch_map = None | |
60 | |
61 def set_map(self, amap): | |
62 self._dispatch_map = amap | |
63 | |
64 def get_map(self): | |
65 return self._dispatch_map | |
66 | |
67 def subscribe(self, event_type, handler): | |
68 """ Subscribe the given handler to an event_type. Handlers | |
69 are called in the order they are subscribed. | |
70 """ | |
71 if self._dispatch_map is None: | |
72 self.set_map({}) | |
73 lst = self._dispatch_map.get(event_type, None) | |
74 if lst is None: | |
75 lst = [handler] | |
76 else: | |
77 lst.append(handler) | |
78 self._dispatch_map[event_type] = lst | |
79 | |
80 def dispatch(self, event): | |
81 """ Dispatch the given event to the subscribed handlers for | |
82 the event's type""" | |
83 if self._dispatch_map is not None: | |
84 lst = self._dispatch_map.get(type(event), None) | |
85 if lst is None: | |
86 raise ValueError("unknown event type: %s" % type(event)) | |
87 for l in lst: | |
88 l(event) | |
89 | |
90 | |
91 def test(): | |
92 import doctest | |
93 doctest.testmod() | |
94 | |
95 | |
96 if __name__ == '__main__': | |
97 test() |