5
""" own threading wrapper """
7
__copyright__ = 'this file is in the public domain'
9
from gozerbot.generic import handle_exception, rlog, lockdec
10
import threading, re, time, thread
12
# regular expression to determine thread name
13
methodre = re.compile('method\s+(\S+)', re.I)
14
funcre = re.compile('function\s+(\S+)', re.I)
16
threadlock = thread.allocate_lock()
17
locked = lockdec(threadlock)
19
class Botcommand(threading.Thread):
21
""" thread for running bot commands .. give feedback of exceptions to
22
ircevent argument (second after bot) """
24
def __init__(self, group, target, name, args, kwargs):
26
rlog(-14, 'thr', 'running %s .. args: %s' % (name, args))
27
threading.Thread.__init__(self, None, target, name, args, kwargs)
31
""" run the bot command """
33
threading.Thread.run(self)
34
if self.ievent.queues:
35
for i in self.ievent.queues:
38
handle_exception(self.ievent)
41
class Thr(threading.Thread):
43
""" thread wrapper """
45
def __init__(self, group, target, name, args, kwargs):
46
rlog(-14, 'thr', 'running %s .. args: %s' % (name, args))
47
threading.Thread.__init__(self, None, target, name, args, kwargs)
51
""" run the thread """
53
threading.Thread.run(self)
59
""" get name of function/method """
61
method = re.search(methodre, str(func))
63
name = method.group(1)
65
function = re.search(funcre, str(func))
67
name = function.group(1)
72
def start_new_thread(func, arglist, kwargs=None):
73
""" start a new thread .. set name to function/method name"""
81
thread = Thr(None, target=func, name=name, args=arglist, \
90
def start_bot_command(func, arglist, kwargs=None):
91
""" start a new thread .. set name to function/method name"""
99
thread = Botcommand(group=None, target=func, name=name, args=arglist, \