Package gozerbot :: Module eventhandler
[hide private]
[frames] | no frames]

Source Code for Module gozerbot.eventhandler

  1  # gozerbot/eventhandler.py 
  2  # 
  3  # 
  4   
  5  """ event handlers """ 
  6   
  7  __copyright__ = 'this file is in the public domain' 
  8   
  9  from gozerbot.generic import handle_exception, rlog, lockdec 
 10  from gozerbot.thr import start_new_thread 
 11  import Queue, thread 
 12   
 13  handlerlock = thread.allocate_lock() 
 14  locked = lockdec(handlerlock) 
 15   
16 -class Eventhandler(object):
17 18 """ events are handled in 11 queues with different priorities: 19 queue0 is tried first queue10 last 20 """ 21
22 - def __init__(self):
23 self.sortedlist = [] 24 self.queues = {} 25 for i in range(11): 26 self.queues[i] = Queue.Queue() 27 self.sortedlist.append(i) 28 self.sortedlist.sort() 29 self.go = Queue.Queue() 30 self.stopped = False 31 self.running = False 32 self.nooutput = False
33
34 - def start(self):
35 """ start the eventhandler thread """ 36 self.stopped = False 37 if not self.running: 38 start_new_thread(self.handleloop, ()) 39 self.running = True
40
41 - def stop(self):
42 """ stop the eventhandler thread """ 43 self.running = False 44 self.stopped = True 45 self.go.put('Yihaaa')
46
47 - def put(self, speed, func, *args, **kwargs):
48 """ put item on the queue """ 49 self.queues[10-speed].put_nowait((func, args, kwargs)) 50 self.go.put('go')
51
52 - def getready(self):
53 """ check queues from available data """ 54 ready = [] 55 for i in self.sortedlist: 56 if self.queues[i].qsize(): 57 ready.append(i) 58 break 59 return ready
60
61 - def handle_one(self):
62 """ do 1 loop over ready queues """ 63 ready = self.getready() 64 for i in ready: 65 self.dispatch(self.queues[i])
66
67 - def handleloop(self):
68 """ thread that polls the queues for items to dispatch """ 69 rlog(0, 'eventhandler', 'starting handle thread') 70 while not self.stopped: 71 self.go.get() 72 self.handle_one() 73 rlog(0, 'eventhandler', 'stopping %s' % str(self))
74
75 - def dispatch(self, queue):
76 """ dispatch functions from provided queue """ 77 try: 78 todo = queue.get_nowait() 79 except Queue.Empty: 80 return 81 try: 82 (func, args, kwargs) = todo 83 func(*args, **kwargs) 84 except ValueError: 85 try: 86 (func, args) = todo 87 func(*args) 88 except ValueError: 89 (func, ) = todo 90 func() 91 except: 92 handle_exception()
93
94 -class Irchandler(Eventhandler):
95 96 """ handles ircevents """ 97
98 - def put(self, speed, bot, ievent):
99 """ put item on the queue """ 100 self.queues[10-speed].put_nowait((bot, ievent)) 101 self.go.put('go')
102
103 - def dispatch(self, queue):
104 """ dispatch ircevents from provided queue """ 105 try: 106 (bot, ievent) = queue.get_nowait() 107 bot.handle_ievent(ievent) 108 except Queue.Empty: 109 return 110 except: 111 handle_exception()
112
113 -class Commandhandler(Eventhandler):
114 115 """ handle bot commands """ 116
117 - def put(self, speed, what, com, bot, ievent):
118 """ put item on the queue """ 119 self.queues[10-speed].put_nowait((what, com, bot, ievent)) 120 self.go.put('go')
121
122 - def dispatch(self, queue):
123 """ dispatch bot commands from provided queue """ 124 try: 125 (what, com, bot, ievent) = queue.get_nowait() 126 rlog(9, 'eventhandler', '%s (%s) executing %s at speed %s' % \ 127 (ievent.nick, ievent.userhost, ievent.txt, ievent.speed)) 128 what.dispatch(com, bot, ievent) 129 except Queue.Empty: 130 return 131 except: 132 handle_exception()
133
134 -class Outputhandler(Eventhandler):
135 136 """ handle bot output """ 137
138 - def __init__(self, bot):
139 Eventhandler.__init__(self) 140 self.bot = bot 141 self.lastout = 0
142
143 - def handle_one(self):
144 """ do 1 loop over ready queues """ 145 if self.nooutput: 146 self.go.put('later') 147 return 148 if self.bot.outputlock.locked(): 149 self.go.put('later') 150 return 151 ready = self.getready() 152 for i in ready: 153 self.dispatch(self.queues[i])
154
155 - def dispatch(self, queue):
156 """ dispatch bot commands from provided queue """ 157 try: 158 (bot, printto, what, how, who, fromm) = queue.get_nowait() 159 bot.output(printto, what, how, who, fromm) 160 except Queue.Empty: 161 return 162 except: 163 handle_exception()
164 165 # handler to use in main prog 166 mainhandler = Eventhandler() 167 commandhandler = Commandhandler() 168