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

Source Code for Module gozerbot.callbacks

  1  # gozerbot/callbacks.py 
  2  # 
  3  # 
  4   
  5  """ callbacks triggered by ircevent CMND """ 
  6   
  7  __copyright__ = 'this file is in the public domain' 
  8   
  9  from gozerbot.generic import rlog, handle_exception, calledfrom, makeargrest, \ 
 10  lockdec 
 11  from gozerbot.dol import Dol 
 12  from gozerbot.thr import start_new_thread 
 13  from gozerbot.runner import cbrunners 
 14  import sys, copy, thread 
 15   
 16  callbacklock = thread.allocate_lock() 
 17  locked = lockdec(callbacklock) 
18 19 -class Callback(object):
20 21 """ class representing a callback """ 22
23 - def __init__(self, func, prereq, plugname, kwargs, threaded=False, \ 24 speed=5):
25 # the callback function 26 self.func = func 27 # pre condition function 28 self.prereq = prereq 29 # plugin name 30 self.plugname = plugname 31 # kwargs to pass on to function 32 self.kwargs = kwargs 33 # flag to see if callback has to run in thread or in the mainloop 34 self.threaded = copy.deepcopy(threaded) 35 self.speed = copy.deepcopy(speed)
36
37 -class Callbacks(object):
38 39 """ dict of lists containing callbacks """ 40
41 - def __init__(self):
42 # cbs are the callbacks which is a dict of lists .. 1 list per 43 # ievent.CMND 44 self.cbs = Dol()
45
46 - def size(self):
47 """ return nr of callbacks """ 48 return len(self.cbs)
49
50 - def add(self, what, func, prereq=None, kwargs=None, threaded=False, \ 51 nr=False, speed=5):
52 """ add a callback """ 53 what = what.upper() 54 # get the plugin this callback was registered from 55 plugname = calledfrom(sys._getframe()) 56 # see if kwargs is set if not init to {} 57 if not kwargs: 58 kwargs = {} 59 # add callback to the dict of lists 60 if nr != False: 61 self.cbs.insert(nr, what, Callback(func, prereq, plugname, \ 62 kwargs, threaded, speed)) 63 else: 64 self.cbs.add(what, Callback(func, prereq, plugname, kwargs, \ 65 threaded, speed)) 66 rlog(-3, 'callbacks', 'added %s (%s)' % (what, plugname))
67
68 - def unload(self, plug):
69 """ unload plugin """ 70 unload = [] 71 # look for all callbacks in a plugin 72 for name, cblist in self.cbs.iteritems(): 73 index = 0 74 for item in cblist: 75 if item.plugname == plug: 76 unload.append((name, index)) 77 index += 1 78 # delete callbacks 79 for callback in unload[::-1]: 80 self.cbs.delete(callback[0], callback[1]) 81 rlog(1, 'callbacks', 'unloaded %s' % callback[0])
82
83 - def whereis(self, what):
84 """ show where ircevent.CMND callbacks are registered """ 85 result = [] 86 what = what.upper() 87 for cmnd, callback in self.cbs.iteritems(): 88 if cmnd == what: 89 for item in callback: 90 if not item.plugname in result: 91 result.append(item.plugname) 92 return result
93
94 -class Botcallbacks(Callbacks):
95 96 """ callback on ircevent """ 97
98 - def check(self, bot, ievent):
99 """ check for callbacks to be fired """ 100 # check for "ALL" callbacks 101 if self.cbs.has_key('ALL'): 102 for cb in self.cbs['ALL']: 103 self.callback(cb, bot, ievent) 104 cmnd = ievent.cmnd.upper() 105 # check for CMND callbacks 106 if self.cbs.has_key(cmnd): 107 for cb in self.cbs[cmnd]: 108 self.callback(cb, bot, ievent)
109 110 @locked
111 - def callback(self, cb, bot, ievent):
112 """ callback cb with bot and ievent as arguments """ 113 #if not bot or not ievent.bot: 114 # rlog(5, 'callbacks', 'no bot or ievent.bot') 115 # return 116 #if ievent.nick and ievent.nick == bot.nick: 117 # rlog(5, 'callbacks', 'callback loop on %s ==> %s' % (bot.nick, \ 118 #str(ievent))) 119 # return 120 try: 121 # see if the callback pre requirement succeeds 122 if cb.prereq: 123 rlog(-10, 'callback', 'excecuting in loop %s' % \ 124 str(cb.prereq)) 125 if not cb.prereq(bot, ievent): 126 return 127 # check if callback function is there 128 if not cb.func: 129 return 130 # start callback in its own thread 131 rlog(-10, 'callback', 'excecuting callback %s' % \ 132 str(cb.func)) 133 if cb.threaded: 134 start_new_thread(cb.func, (bot, ievent), cb.kwargs) 135 else: 136 cbrunners[10-cb.speed].put("cb-%s" % cb.plugname, cb.func, \ 137 bot, ievent, **cb.kwargs) 138 #cb.func(bot, ievent, **cb.kwargs) 139 except Exception, ex: 140 handle_exception()
141
142 -class Jabbercallbacks(Callbacks):
143 144 """ callback on ircevent """ 145
146 - def check(self, bot, ievent):
147 """ check for callbacks to be fired """ 148 # check for "ALL" callbacks 149 if self.cbs.has_key('ALL'): 150 for cb in self.cbs['ALL']: 151 self.callback(cb, bot, ievent) 152 cmnd = ievent.cmnd.upper() 153 # check for CMND callbacks 154 if self.cbs.has_key(cmnd): 155 for cb in self.cbs[cmnd]: 156 self.callback(cb, bot, ievent)
157
158 - def callback(self, cb, bot, ievent):
159 """ callback cb with bot and ievent as arguments """ 160 if not bot: 161 return 162 if str(bot.jid) in str(ievent.getFrom()): 163 return 164 try: 165 # see if the callback pre requirement succeeds 166 if cb.prereq: 167 rlog(-10, 'callback', 'excecuting in loop %s' % \ 168 str(cb.prereq)) 169 if not cb.prereq(bot, ievent): 170 return 171 # check if callback function is there 172 if not cb.func: 173 return 174 # start callback in its own thread 175 rlog(-10, 'callback', 'excecuting callback %s' % \ 176 str(cb.func)) 177 if cb.threaded: 178 start_new_thread(cb.func, (bot, ievent), cb.kwargs) 179 else: 180 cbrunners[10-cb.speed].put("cb-%s" % cb.plugname, cb.func, bot, ievent, \ 181 **cb.kwargs) 182 #cb.func(bot, ievent, **cb.kwargs) 183 except Exception, ex: 184 handle_exception()
185 186 callbacks = Botcallbacks() 187 jcallbacks = Jabbercallbacks() 188