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