1
#Copyright (c) 2006 Simon Wittber
3
#Permission is hereby granted, free of charge, to any person
4
#obtaining a copy of this software and associated documentation files
5
#(the "Software"), to deal in the Software without restriction,
6
#including without limitation the rights to use, copy, modify, merge,
7
#publish, distribute, sublicense, and/or sell copies of the Software,
8
#and to permit persons to whom the Software is furnished to do so,
9
#subject to the following conditions:
11
#The above copyright notice and this permission notice shall be
12
#included in all copies or substantial portions of the Software.
14
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18
#BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19
#ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
#CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
EventNet provides a generic, global dispatcher system for Event driven
31
To post an event, use:
33
eventnet.driver.post(event_name, **kw)
35
Any registered subscribers will be called with the event as the first
36
argument. An instance of an event has a name attribute, an args dictionary.
37
All keys in the args dictionary are also attributes of the instances.
40
= Handling Events with Functions =
42
To subscribe a function to an event, use the subscriber decorator. If the
43
event has arg1 and arg2 attributes, they will be passed to the function. The
44
function signature need only specify the event attributes it needs.
46
@eventnet.driver.subscribe(event_name)
47
def event_handler(arg1, arg2):
50
A subscribed function has a release method which will stop the function
51
from being called when subscribed event is posted.
54
= Handling Events with Classes =
56
A class which inherits from eventnet.driver.Subscriber can handle multiple
59
class Handler(eventnet.driver.Subscriber):
60
def EVT_Event(self, arg1, arg2):
63
def EVT_SomeOtherEvent(self, arg1):
67
The above class defined two methods which are prefixed with 'EVT_'. This
68
prefix causes the class to automatically subscribe those methods to with the
69
event name which is taken from the remainder of the method name. Eg: the
70
method EVT_Event would be called whenever
72
eventnet.driver.post('Event', arg1=1, arg2=2, arg3=3)
74
is called. Only the arguments in the method signature are supplied, the
75
method need not handle all potential event attributes.
77
To allow a class instance to start handling events, the capture method must
78
be called. To stop a class instance from handling events, the release method