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

« back to all changes in this revision

Viewing changes to debian/gozerbot/usr/lib/python2.5/site-packages/gozerbot/redispatcher.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

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