class SinkHandler(ThreadLoop): def __init__(self, name="", queue=None, *args, **kwargs): ThreadLoop.__init__(self, name, queue, *args, **kwargs) self.cbs = Dol() def register(self, cb, modname=None, cbtype=None, *args, **kwargs): """ register a sink callback. """ modname = modname or whichmodule(2) self.cbs.add(Sinker(cb, modname, cbtype, *args, **kwargs)) logging.warn("registered %s sink handler for the %s plugin" % (str(cb), modname)) def unregister(self, modname=None): """ unregister a sink callback. """ modname = modname or whichmodule(2) try: size = len(self.cbs[modname]) ; del self.cbs[modname] ; logging.warn("%s sink callbacks removed" % size) except KeyError: pass def handle(self, bot, event): """ handle a sink callback by pushing corresponding callbacks to the long runner. """ cbslists = self.cbs.values() for cb in cbslists: for sinker in cbs: if sinker.cbtype and sinker.cbtype != event.cbtype: continue event.sinker = sinker longrunner.put(event.speed, sinker.cb, bot, event)
def __init__(self): self.cbs = Dol()
class Callbacks(object): """ dict of lists containing callbacks. Callbacks object take care of dispatching the callbacks based on incoming events. see Callbacks.check() """ def __init__(self): self.cbs = Dol() def size(self): """ return number of callbacks. """ return len(self.cbs) def add(self, what, func, prereq=None, kwargs=None, threaded=False, nr=False, speed=5): """ add a callback. """ what = what.upper() modname = calledfrom(sys._getframe()) if not kwargs: kwargs = {} if nr != False: self.cbs.insert( nr, what, Callback(modname, func, prereq, kwargs, threaded, speed)) else: self.cbs.add( what, Callback(modname, func, prereq, kwargs, threaded, speed)) logging.debug('callbacks - added %s (%s)' % (what, modname)) return self def unload(self, modname): """ unload all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.modname == modname: unload.append((name, index)) index += 1 for callback in unload[::-1]: self.cbs.delete(callback[0], callback[1]) logging.debug('callbacks - unloaded %s (%s)' % (callback[0], modname)) def disable(self, plugname): """ disable all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = False def activate(self, plugname): """ activate all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = True def whereis(self, cmnd): """ show where ircevent.CMND callbacks are registered """ result = [] cmnd = cmnd.upper() for c, callback in self.cbs.iteritems(): if c == cmnd: for item in callback: if not item.plugname in result: result.append(item.plugname) return result def list(self): """ show all callbacks. """ result = [] for cmnd, callbacks in self.cbs.iteritems(): for cb in callbacks: result.append(getname(cb.func)) return result def check(self, bot, event): """ check for callbacks to be fired. """ type = event.cbtype or event.cmnd logging.debug("callbacks - %s - %s" % (event.userhost, type)) if self.cbs.has_key('ALL'): for cb in self.cbs['ALL']: self.callback(cb, bot, event) if self.cbs.has_key(type): target = self.cbs[type] for cb in target: self.callback(cb, bot, event) def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ if event.stop: logging.info("callbacks - event is stopped.") return event.calledfrom = cb.modname try: if event.status == "done": logging.debug("callback - event is done .. ignoring") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) return if cb.prereq: logging.debug('callbacks - executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.name, getname(cb.func), event.cbtype)) elif event.cbtype == "TICK": logging.debug('LOCAL - %s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) else: logging.info('%s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) event.iscallback = True logging.debug("callback - %s - trail - %s" % (getname(cb.func), callstack(sys._getframe())[::-1])) #if not event.direct and cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) if cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) else: if bot.isgae or event.direct: cb.func(bot, event) else: from runner import callbackrunner callbackrunner.put(cb.modname, cb.func, bot, event) return True except Exception, ex: handle_exception()
class Callbacks(object): """ dict of lists containing callbacks. Callbacks object take care of dispatching the callbacks based on incoming events. see Callbacks.check() """ def __init__(self): self.cbs = Dol() def size(self): """ return number of callbacks. """ return len(self.cbs) def add(self, what, func, prereq=None, kwargs=None, threaded=False, nr=False, speed=5): """ add a callback. """ what = what.upper() modname = calledfrom(sys._getframe()) if not kwargs: kwargs = {} if nr != False: self.cbs.insert(nr, what, Callback(modname, func, prereq, kwargs, threaded, speed)) else: self.cbs.add(what, Callback(modname, func, prereq, kwargs, threaded, speed)) logging.debug('callbacks - added %s (%s)' % (what, modname)) return self def unload(self, modname): """ unload all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.modname == modname: unload.append((name, index)) index += 1 for callback in unload[::-1]: self.cbs.delete(callback[0], callback[1]) logging.debug('callbacks - unloaded %s (%s)' % (callback[0], modname)) def disable(self, plugname): """ disable all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = False def activate(self, plugname): """ activate all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = True def whereis(self, cmnd): """ show where ircevent.CMND callbacks are registered """ result = [] cmnd = cmnd.upper() for c, callback in self.cbs.iteritems(): if c == cmnd: for item in callback: if not item.plugname in result: result.append(item.plugname) return result def list(self): """ show all callbacks. """ result = [] for cmnd, callbacks in self.cbs.iteritems(): for cb in callbacks: result.append(getname(cb.func)) return result def check(self, bot, event): """ check for callbacks to be fired. """ type = event.cbtype or event.cmnd logging.debug("callbacks - %s - %s" % (event.userhost, type)) if self.cbs.has_key('ALL'): for cb in self.cbs['ALL']: self.callback(cb, bot, event) if self.cbs.has_key(type): target = self.cbs[type] for cb in target: self.callback(cb, bot, event) def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ if event.stop: logging.info("callbacks - event is stopped.") ; return event.calledfrom = cb.modname try: if event.status == "done": logging.debug("callback - event is done .. ignoring") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) return if cb.prereq: logging.debug('callbacks - executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.name, getname(cb.func), event.cbtype)) elif event.cbtype == "TICK": logging.debug('LOCAL - %s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) else: logging.info('%s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) event.iscallback = True logging.debug("callback - %s - trail - %s" % (getname(cb.func), callstack(sys._getframe())[::-1])) #if not event.direct and cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) if cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) else: if bot.isgae or event.direct: cb.func(bot, event) else: from runner import callbackrunner callbackrunner.put(cb.modname, cb.func, bot, event) return True except Exception, ex: handle_exception()
class Callbacks(object): """ dict of lists containing callbacks. Callbacks object take care of dispatching the callbacks based on incoming events. see Callbacks.check() """ def __init__(self): self.cbs = Dol() def size(self): """ return number of callbacks. """ return self.cbs.size() def add(self, what, func, prereq=None, kwargs=None, threaded=False, nr=False, speed=5, force=False): """ add a callback. """ what = what.upper() modname = calledfrom(sys._getframe()) if not kwargs: kwargs = {} if nr != False: self.cbs.insert(nr, what, Callback(modname, func, prereq, kwargs, threaded, speed, force)) else: self.cbs.add(what, Callback(modname, func, prereq, kwargs, threaded, speed, force)) logging.debug('added %s (%s)' % (what, modname)) return self register = add def unload(self, modname): """ unload all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.modname == modname: unload.append((name, index)) index += 1 for callback in unload[::-1]: self.cbs.delete(callback[0], callback[1]) logging.debug('unloaded %s (%s)' % (callback[0], modname)) def disable(self, plugname): """ disable all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = False def activate(self, plugname): """ activate all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = True def whereis(self, cmnd): """ show where ircevent.CMND callbacks are registered """ result = [] cmnd = cmnd.upper() for c, callback in self.cbs.iteritems(): if c == cmnd: for item in callback: if not item.plugname in result: result.append(item.plugname) return result def list(self): """ show all callbacks. """ result = [] for cmnd, callbacks in self.cbs.iteritems(): for cb in callbacks: result.append(getname(cb.func)) return result def check(self, bot, event): """ check for callbacks to be fired. """ self.reloadcheck(bot, event) type = event.cbtype or event.cmnd if self.cbs.has_key('ALL'): for cb in self.cbs['ALL']: self.callback(cb, bot, event) if self.cbs.has_key(type): target = self.cbs[type] for cb in target: self.callback(cb, bot, event) def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ #if event.stop: logging.info("callbacks - event is stopped.") ; return if event.cbtype in bot.nocbs and not cb.force: logging.warn("%s in nocbs list, skipping" % event.cbtype) ; return event.calledfrom = cb.modname if not event.bonded: event.bind(bot) try: if event.status == "done": if not event.nolog: logging.debug("callback - event is done .. ignoring") stats.upitem("ignored") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) stats.upitem("denied") return if cb.prereq: if not event.nolog: logging.info('executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) elif not event.nolog: logging.info('%s - executing %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) event.iscallback = True if not event.nolog: logging.debug("%s - %s - trail - %s" % (bot.cfg.name, getname(cb.func), callstack(sys._getframe())[::-1])) time.sleep(0.01) if cb.threaded: logging.info("PURE THREAD STARTED %s" % str(cb.func)) ; start_new_thread(cb.func, (bot, event)) else: display = "%s/%s/%s" % (cb.plugname, bot.cfg.name, event.nick or event.channel) if event.cbtype == "API": from runner import apirunner apirunner.put(event.speed or cb.speed, display, cb.func, bot, event) elif event.direct: cb.func(bot, event) elif not event.dolong: from runner import callbackrunner callbackrunner.put(event.speed or cb.speed, display, cb.func, bot, event) else: from runner import longrunner longrunner.put(event.speed or cb.speed, display, cb.func, bot, event) stats.upitem(event.cbtype) stats.upitem("nrcallbacks") return True except Exception, ex: handle_exception()
class Callbacks(object): """ dict of lists containing callbacks. Callbacks object take care of dispatching the callbacks based on incoming events. see Callbacks.check() """ def __init__(self): self.cbs = Dol() def size(self): """ return number of callbacks. """ return self.cbs.size() def add(self, what, func, prereq=None, kwargs=None, threaded=False, nr=False, speed=5, force=False): """ add a callback. """ what = what.upper() modname = calledfrom(sys._getframe()) if not kwargs: kwargs = {} if nr != False: self.cbs.insert( nr, what, Callback(modname, func, prereq, kwargs, threaded, speed, force)) else: self.cbs.add( what, Callback(modname, func, prereq, kwargs, threaded, speed, force)) logging.debug('added %s (%s)' % (what, modname)) return self register = add def unload(self, modname): """ unload all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.modname == modname: unload.append((name, index)) index += 1 for callback in unload[::-1]: self.cbs.delete(callback[0], callback[1]) logging.debug('unloaded %s (%s)' % (callback[0], modname)) def disable(self, plugname): """ disable all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = False def activate(self, plugname): """ activate all callbacks registered in a plugin. """ unload = [] for name, cblist in self.cbs.iteritems(): index = 0 for item in cblist: if item.plugname == plugname: item.activate = True def whereis(self, cmnd): """ show where ircevent.CMND callbacks are registered """ result = [] cmnd = cmnd.upper() for c, callback in self.cbs.iteritems(): if c == cmnd: for item in callback: if not item.plugname in result: result.append(item.plugname) return result def list(self): """ show all callbacks. """ result = [] for cmnd, callbacks in self.cbs.iteritems(): for cb in callbacks: result.append(getname(cb.func)) return result def check(self, bot, event): """ check for callbacks to be fired. """ self.reloadcheck(bot, event) type = event.cbtype or event.cmnd if self.cbs.has_key('ALL'): for cb in self.cbs['ALL']: self.callback(cb, bot, event) if self.cbs.has_key(type): target = self.cbs[type] for cb in target: self.callback(cb, bot, event) def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ #if event.stop: logging.info("callbacks - event is stopped.") ; return if event.cbtype in bot.nocbs and not cb.force: logging.warn("%s in nocbs list, skipping" % event.cbtype) return event.calledfrom = cb.modname if not event.bonded: event.bind(bot) try: if event.status == "done": if not event.nolog: logging.debug("callback - event is done .. ignoring") stats.upitem("ignored") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) stats.upitem("denied") return if cb.prereq: if not event.nolog: logging.info('executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) elif not event.nolog: logging.info('%s - executing %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) event.iscallback = True if not event.nolog: logging.debug("%s - %s - trail - %s" % (bot.cfg.name, getname( cb.func), callstack(sys._getframe())[::-1])) time.sleep(0.01) if cb.threaded: logging.info("PURE THREAD STARTED %s" % str(cb.func)) start_new_thread(cb.func, (bot, event)) else: display = "%s/%s/%s" % (cb.plugname, bot.cfg.name, event.nick or event.channel) if event.cbtype == "API": from runner import apirunner apirunner.put(event.speed or cb.speed, display, cb.func, bot, event) elif event.direct: cb.func(bot, event) elif not event.dolong: from runner import callbackrunner callbackrunner.put(event.speed or cb.speed, display, cb.func, bot, event) else: from runner import longrunner longrunner.put(event.speed or cb.speed, display, cb.func, bot, event) stats.upitem(event.cbtype) stats.upitem("nrcallbacks") return True except Exception, ex: handle_exception()
def __init__(self, name="", queue=None, *args, **kwargs): ThreadLoop.__init__(self, name, queue, *args, **kwargs) self.cbs = Dol()