~ubuntu-branches/ubuntu/maverick/fibranet/maverick

« back to all changes in this revision

Viewing changes to eventnet/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2006-10-27 01:02:56 UTC
  • Revision ID: james.westby@ubuntu.com-20061027010256-n265y24bk98s9lbe
Tags: upstream-10
ImportĀ upstreamĀ versionĀ 10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#Copyright (c) 2006 Simon Wittber
 
2
#
 
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:
 
10
#
 
11
#The above copyright notice and this permission notice shall be
 
12
#included in all copies or substantial portions of the Software.
 
13
#
 
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
 
21
#SOFTWARE.
 
22
 
 
23
"""
 
24
 
 
25
EventNet provides a generic, global dispatcher system for Event driven
 
26
systems.
 
27
 
 
28
 
 
29
= Posting Events =
 
30
 
 
31
To post an event, use:
 
32
 
 
33
eventnet.driver.post(event_name, **kw)
 
34
 
 
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.
 
38
 
 
39
 
 
40
= Handling Events with Functions =
 
41
 
 
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.
 
45
 
 
46
@eventnet.driver.subscribe(event_name)
 
47
def event_handler(arg1, arg2):
 
48
    print arg1, arg2
 
49
 
 
50
A subscribed function has a release method which will stop the function
 
51
from being called when subscribed event is posted.
 
52
 
 
53
 
 
54
= Handling Events with Classes =
 
55
 
 
56
A class which inherits from eventnet.driver.Subscriber can handle multiple
 
57
events.
 
58
 
 
59
class Handler(eventnet.driver.Subscriber):
 
60
    def EVT_Event(self, arg1, arg2):
 
61
        print arg1, arg2
 
62
 
 
63
    def EVT_SomeOtherEvent(self, arg1):
 
64
        print arg1
 
65
 
 
66
 
 
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
 
71
 
 
72
eventnet.driver.post('Event', arg1=1, arg2=2, arg3=3)
 
73
 
 
74
is called. Only the arguments in the method signature are supplied, the 
 
75
method need not handle all potential event attributes.
 
76
 
 
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
 
79
must be called.
 
80
 
 
81
"""
 
82
 
 
83
import driver
 
84
 
 
85
 
 
86