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

Source Code for Module gozerbot.redispatcher

  1  # gozerbot/redispatcher.py 
  2  # 
  3  # 
  4   
  5  """ implement RE (regular expression) dispatcher """ 
  6   
  7  __copyright__ = 'this file is in the public domain' 
  8   
  9  from gozerbot.generic import rlog, calledfrom, handle_exception, lockdec 
 10  from gozerbot.runner import cmndrunners 
 11  import gozerbot.thr as thr 
 12  import sys, re, copy, types, thread 
 13   
 14  relock = thread.allocate_lock() 
 15  locked = lockdec(relock) 
 16   
17 -class Recallback(object):
18 19 """ a regular expression callback """ 20
21 - def __init__(self, index, regex, func, perm, plugname, speed=5, \ 22 threaded=True, allowqueue=True, options={}):
23 # index of place of RE callback in list 24 self.name = "noname" 25 self.index = index 26 # the RE to match 27 self.regex = regex 28 self.compiled = re.compile(regex) 29 # the function to call if RE matches 30 self.func = func 31 # make sure perms is a list 32 if type(perm) == types.ListType: 33 self.perms = list(perm) 34 else: 35 self.perms = [perm, ] 36 # plug name 37 self.plugname = plugname 38 # speed at which the functin is to be dispatched 39 self.speed = copy.deepcopy(speed) 40 # flag to see if RE callback is to be run in a thread or in the main 41 # loop 42 self.threaded = copy.deepcopy(threaded) 43 self.allowqueue = copy.deepcopy(allowqueue) 44 self.options = dict(options)
45
46 -class Redispatcher(object):
47 48 """ this is were the regexs callbacks live """ 49
50 - def __init__(self):
51 self.relist = []
52
53 - def size(self):
54 """ nr of callbacks """ 55 return len(self.relist)
56
57 - def whatperms(self):
58 """ return possible permissions """ 59 result = [] 60 for i in self.relist: 61 for j in i.perms: 62 if j not in result: 63 result.append(j) 64 return result
65
66 - def list(self, perm):
67 """ list re with permission perm """ 68 result = [] 69 perm = perm.upper() 70 for recom in self.relist: 71 if perm in recom.perms: 72 result.append(recom) 73 return result
74
75 - def getfuncnames(self, plug):
76 """ return function names in plugin """ 77 result = [] 78 for i in self.relist: 79 if i.plugname == plug: 80 result.append(i.func.func_name) 81 return result
82
83 - def permoverload(self, funcname, perms):
84 """ overload permission of function with funcname """ 85 perms = [perm.upper() for perm in perms] 86 got = 0 87 for nr in range(len(self.relist)): 88 try: 89 if self.relist[nr].func.func_name == funcname: 90 self.relist[nr].perms = list(perms) 91 rlog(0, 'redispatcher', '%s function overloaded with %s' \ 92 % (funcname, perms)) 93 got = 1 94 except AttributeError: 95 rlog(10, 'redispatcher', 'permoverload: no %s function' % \ 96 funcname) 97 if got: 98 return 1
99
100 - def add(self, index, regex, func, perm, speed=5, threaded=True, \ 101 allowqueue=True, options={}):
102 """ add a command """ 103 try: 104 # get plugin name from where callback is added 105 plugname = calledfrom(sys._getframe()) 106 # add Recallback 107 self.relist.append(Recallback(index, regex, func, perm, plugname, \ 108 speed, threaded, allowqueue, options)) 109 # sort of index number 110 self.relist.sort(lambda a, b: cmp(a.index, b.index)) 111 rlog(0, 'redispatcher', 'added %s (%s) ' % (regex, plugname)) 112 finally: 113 pass
114
115 - def unload(self, plugname):
116 """ unload regexs commands """ 117 got = 0 118 try: 119 for i in range(len(self.relist)-1, -1 , -1): 120 if self.relist[i].plugname == plugname: 121 rlog(1, 'redispatcher', 'unloading %s (%s)' % \ 122 (self.relist[i].regex, plugname)) 123 del self.relist[i] 124 got = 1 125 finally: 126 pass 127 if got: 128 return 1
129
130 - def getcallback(self, txt):
131 """ get re callback if txt matches """ 132 for i in self.relist: 133 try: 134 result = re.search(i.compiled, txt) 135 if result: 136 return i 137 except: 138 pass
139
140 - def dispatch(self, callback, txt):
141 """ dispatch callback on txt """ 142 try: 143 result = re.search(callback.compiled, txt) 144 if result: 145 if callback.threaded: 146 thr.start_new_thread(callback.func, (txt, result.groups())) 147 else: 148 cmndrunners.put(callback.plugname, callback.func, txt, \ 149 result.groups()) 150 return 1 151 except Exception, ex: 152 handle_exception()
153
154 -class Botredispatcher(Redispatcher):
155 156 """ dispatcher on ircevent """ 157
158 - def dispatch(self, callback, bot, ievent):
159 """ dispatch callback on ircevent """ 160 try: 161 result = re.search(callback.compiled, ievent.txt.strip()) 162 if result: 163 ievent.groups = list(result.groups()) 164 if callback.threaded: 165 thr.start_bot_command(callback.func, (bot, ievent)) 166 else: 167 cmndrunners.put(callback.plugname, callback.func, bot, \ 168 ievent) 169 return 1 170 except Exception, ex: 171 handle_exception(ievent)
172 173 rebefore = Botredispatcher() 174 reafter = Botredispatcher() 175