/usr/share/pyshared/jsb/lib/eventhandler.py is in jsonbot 0.84.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # jsb/eventhandler.py
#
#
""" event handler. use to dispatch function in main loop. """
## jsb imports
from jsb.utils.exception import handle_exception
from jsb.utils.locking import lockdec
from threads import start_new_thread
## basic imports
import Queue
import thread
import logging
import time
## locks
handlerlock = thread.allocate_lock()
locked = lockdec(handlerlock)
## classes
class EventHandler(object):
"""
events are handled in 11 queues with different priorities:
queue0 is tried first queue10 last.
"""
def __init__(self):
self.sortedlist = []
try: self.queue = Queue.PriorityQueue()
except AttributeError: self.queue = Queue.Queue()
self.stopped = False
self.running = False
self.nooutput = False
def start(self):
""" start the eventhandler thread. """
self.stopped = False
if not self.running:
start_new_thread(self.handleloop, ())
self.running = True
def handle_one(self):
try:
speed, todo = self.queue.get_nowait()
self.dispatch(todo)
except Queue.Empty: pass
def stop(self):
""" stop the eventhandler thread. """
self.running = False
self.stopped = True
self.go.put('Yihaaa')
def put(self, speed, func, *args, **kwargs):
""" put item on the queue. """
self.queue.put_nowait((speed, (func, args, kwargs)))
def handleloop(self):
""" thread that polls the queues for items to dispatch. """
logging.warn('starting - %s ' % str(self))
while not self.stopped:
try:
(speed, todo) = self.queue.get()
logging.warn("running at speed %s - %s" % (speed, str(todo)))
self.dispatch(todo)
except Queue.Empty: time.sleep(0.1)
except Exception, ex: handle_exception()
logging.warn('stopping - %s' % str(self))
runforever = handleloop
def dispatch(self, todo):
""" dispatch functions from provided queue. """
try:
(func, args, kwargs) = todo
func(*args, **kwargs)
except ValueError:
try:
(func, args) = todo
func(*args)
except ValueError:
(func, ) = todo
func()
except: handle_exception()
## handler to use in main prog
mainhandler = EventHandler()
|