#log(INFO, "Received an unhandled packet: %s" % (packet.name))
pass
classEventNotifier(object):
""" access points for subscribing to application wide events. timeout = 0 for no timeout """
def__init__(self,event_name,settings,timeout=0):
""" initialize an event notifier by name, with an optional timeout """
self.event=Event()
self.event_name=event_name
self.settings=settings
iftype(timeout)==int:
self.timeout=timeout
else:
raiseDataParsingError("Timeout must be an integer creating an event watcher for %s"%(event_name))
defsubscribe(self,*args,**kwdargs):
""" register a callback handler for a specific event, starting the timer if != 0, otherwise it will watch until forced to unsubscribe by the caller """
self.args=args
self.kwdargs=kwdargs
self.event.subscribe(*args,**kwdargs)
ifself.timeout!=0:
self._start_timer()
defreceived(self,event):
""" notifies subscribers about an event firing and passes along the data """
self.event(event)
defunsubscribe(self,*args,**kwdargs):
""" stop watching this event """
self.event.unsubscribe(*args,**kwdargs)
ifself.settings.LOG_VERBOSE:log(DEBUG,"Removed the monitor for %s by %s"%(args,kwdargs))
def_start_timer(self):
""" begins the timer when a timeout value is specified. returns None when the timer expires, then unsubscribes """
now=time.time()
start=now
# spawn an empty coroutine for the duration of the timeout
whilenow-start<self.timeout:
api.sleep()
now=time.time()
# once the timeout has expired...
ifself.settings.LOG_VERBOSE:log(DEBUG,"Timing out the monitor for %s by %s"%(self.args,self.kwdargs))
# return None to the callback handler
self.received(None)
# unsubscribe the watcher due to the timeout
self.unsubscribe(*self.args,**self.kwdargs)
def__len__(self):
returnlen(self.event)
__call__=received
##########################
# Application Level Events
##########################
classInstantMessageReceived(object):
""" event data conduit for received instant messages """