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

« back to all changes in this revision

Viewing changes to gozerbot/thr.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/thr.py
2
 
#
3
 
#
4
 
 
5
 
""" own threading wrapper """
6
 
 
7
 
__copyright__ = 'this file is in the public domain'
8
 
 
9
 
from gozerbot.generic import handle_exception, rlog, lockdec
10
 
import threading, re, time, thread
11
 
 
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)
15
 
 
16
 
threadlock = thread.allocate_lock()
17
 
locked = lockdec(threadlock)
18
 
 
19
 
class Botcommand(threading.Thread):
20
 
 
21
 
    """ thread for running bot commands .. give feedback of exceptions to
22
 
        ircevent argument (second after bot) """
23
 
 
24
 
    def __init__(self, group, target, name, args, kwargs):
25
 
        self.ievent = args[1]
26
 
        rlog(-14, 'thr', 'running %s .. args: %s' % (name, args)) 
27
 
        threading.Thread.__init__(self, None, target, name, args, kwargs)
28
 
        self.setDaemon(True)
29
 
 
30
 
    def run(self):
31
 
        """ run the bot command """
32
 
        try:
33
 
            threading.Thread.run(self)
34
 
            if self.ievent.queues:
35
 
                for i in self.ievent.queues:
36
 
                    i.put_nowait(None)
37
 
        except Exception, ex:
38
 
            handle_exception(self.ievent)
39
 
            time.sleep(0.1)
40
 
 
41
 
class Thr(threading.Thread):
42
 
 
43
 
    """ thread wrapper """
44
 
 
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)
48
 
        self.setDaemon(True)
49
 
 
50
 
    def run(self):
51
 
        """ run the thread """
52
 
        try:
53
 
            threading.Thread.run(self)
54
 
        except Exception, ex:
55
 
            handle_exception()
56
 
            time.sleep(0.1)
57
 
 
58
 
def getname(func):
59
 
    """ get name of function/method """
60
 
    name = ""
61
 
    method = re.search(methodre, str(func))
62
 
    if method:
63
 
        name = method.group(1)
64
 
    else: 
65
 
        function = re.search(funcre, str(func))
66
 
        if function:
67
 
            name = function.group(1)
68
 
        else:
69
 
            name = str(func)
70
 
    return name
71
 
 
72
 
def start_new_thread(func, arglist, kwargs=None):
73
 
    """ start a new thread .. set name to function/method name"""
74
 
    time.sleep(0.0001)
75
 
    if not kwargs:
76
 
        kwargs = {}
77
 
    try:
78
 
        name = getname(func)
79
 
        if not name:
80
 
            name = 'noname'
81
 
        thread = Thr(None, target=func, name=name, args=arglist, \
82
 
kwargs=kwargs)
83
 
        thread.start()
84
 
        return thread
85
 
    except:
86
 
        handle_exception()
87
 
        time.sleep(1)
88
 
 
89
 
@locked  
90
 
def start_bot_command(func, arglist, kwargs=None):
91
 
    """ start a new thread .. set name to function/method name"""
92
 
    time.sleep(0.0001)
93
 
    if not kwargs:
94
 
        kwargs = {}
95
 
    try:
96
 
        name = getname(func)
97
 
        if not name:
98
 
            name = 'noname'
99
 
        thread = Botcommand(group=None, target=func, name=name, args=arglist, \
100
 
kwargs=kwargs)
101
 
        thread.start()
102
 
        return thread
103
 
    except:
104
 
        handle_exception()
105
 
        time.sleep(1)