~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/eventhandler.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/eventhandler.py
 
2
#
 
3
#
 
4
 
 
5
"""
 
6
    event handler. use to dispatch function in main loop.
 
7
 
 
8
"""
 
9
 
 
10
__copyright__ = 'this file is in the public domain'
 
11
 
 
12
# ==============
 
13
# IMPORT SECTION
 
14
 
 
15
# gozerbot imports
 
16
from utils.exception import handle_exception
 
17
from utils.log import rlog
 
18
from utils.locking import lockdec
 
19
from threads.thr import start_new_thread
 
20
 
 
21
# basic imports
 
22
import Queue, thread
 
23
 
 
24
# END IMPORT
 
25
# ==========
 
26
 
 
27
# ============
 
28
# LOCK SECTION
 
29
 
 
30
# locks
 
31
handlerlock = thread.allocate_lock()
 
32
locked = lockdec(handlerlock)
 
33
 
 
34
# END LOCK
 
35
# ========
 
36
 
 
37
## START
 
38
 
 
39
class Eventhandler(object):
 
40
 
 
41
    """
 
42
        events are handled in 11 queues with different priorities:
 
43
        queue0 is tried first queue10 last.
 
44
 
 
45
    """
 
46
 
 
47
    def __init__(self):
 
48
        self.sortedlist = []
 
49
        self.queues = {}
 
50
 
 
51
        for i in range(11):
 
52
            self.queues[i] = Queue.Queue()
 
53
            self.sortedlist.append(i)
 
54
 
 
55
        self.sortedlist.sort()
 
56
        self.go = Queue.Queue()
 
57
        self.stopped = False
 
58
        self.running = False
 
59
        self.nooutput = False
 
60
 
 
61
    def start(self):
 
62
 
 
63
        """
 
64
            start the eventhandler thread.
 
65
 
 
66
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
67
                   :pyobject: Eventhandler.start
 
68
 
 
69
        """
 
70
 
 
71
        self.stopped = False
 
72
 
 
73
        if not self.running:
 
74
            start_new_thread(self.handleloop, ())
 
75
            self.running = True
 
76
 
 
77
    def stop(self):
 
78
 
 
79
        """
 
80
            stop the eventhandler thread.
 
81
 
 
82
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
83
                :pyobject: Eventhandler.stop
 
84
 
 
85
        """
 
86
 
 
87
        self.running = False
 
88
        self.stopped = True
 
89
        self.go.put('Yihaaa')
 
90
 
 
91
    def put(self, speed, func, *args, **kwargs):
 
92
 
 
93
        """
 
94
             put item on the queue.
 
95
 
 
96
             .. literalinclude:: ../../gozerbot/eventhandler.py
 
97
                 :pyobject: Eventhandler.put
 
98
 
 
99
        """
 
100
 
 
101
        self.queues[10-speed].put_nowait((func, args, kwargs))
 
102
        self.go.put('go')
 
103
 
 
104
    def getready(self):
 
105
 
 
106
        """
 
107
            check queues from available functions to execute.
 
108
 
 
109
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
110
                :pyobject: Eventhandler.getready
 
111
 
 
112
        """
 
113
 
 
114
        ready = []
 
115
 
 
116
        for i in self.sortedlist:
 
117
            if self.queues[i].qsize():
 
118
                ready.append(i)
 
119
                break
 
120
 
 
121
        return ready
 
122
 
 
123
    def handle_one(self):
 
124
 
 
125
        """
 
126
            do 1 loop over ready queues.
 
127
 
 
128
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
129
                :pyobject: Eventhandler.handle_one
 
130
 
 
131
        """
 
132
 
 
133
        ready = self.getready()
 
134
 
 
135
        for i in ready:
 
136
            self.dispatch(self.queues[i])
 
137
 
 
138
    def handleloop(self):
 
139
 
 
140
        """
 
141
            thread that polls the queues for items to dispatch.
 
142
 
 
143
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
144
                :pyobject: Eventhandler.handleloop
 
145
 
 
146
        """
 
147
 
 
148
        rlog(0, 'eventhandler', 'starting handle thread')
 
149
 
 
150
        while not self.stopped:
 
151
            self.go.get()
 
152
            self.handle_one()
 
153
 
 
154
        rlog(0, 'eventhandler', 'stopping %s' % str(self))
 
155
 
 
156
    def dispatch(self, queue):
 
157
 
 
158
        """
 
159
            dispatch functions from provided queue.
 
160
 
 
161
            .. literalinclude:: ../../gozerbot/eventhandler.py
 
162
                :pyobject: Eventhandler.dispatch
 
163
 
 
164
        """
 
165
 
 
166
 
 
167
        try:
 
168
            todo = queue.get_nowait()
 
169
        except Queue.Empty:
 
170
            return
 
171
 
 
172
        try:
 
173
            (func, args, kwargs) = todo
 
174
            func(*args, **kwargs)
 
175
        except ValueError:
 
176
            try:
 
177
                (func, args) = todo
 
178
                func(*args)
 
179
            except ValueError:
 
180
                (func, ) = todo
 
181
                func()
 
182
 
 
183
        except:
 
184
            handle_exception()
 
185
 
 
186
# INIT SECTION
 
187
 
 
188
# handler to use in main prog
 
189
mainhandler = Eventhandler()
 
190
 
 
191
 
 
192
# END INIT
 
 
b'\\ No newline at end of file'