def testDispatcherResult(self): d = EventDispatcher() msg = Element(("ns", "message")) pres = Element(("ns", "presence")) cb = CallbackTracker() d.addObserver("/presence", cb.call) result = d.dispatch(msg) self.assertEquals(False, result) result = d.dispatch(pres) self.assertEquals(True, result)
def test_cleanUpOnetimeEventObserver(self): """ Test observer clean-up after onetime named events. """ d = EventDispatcher() cb = CallbackTracker() d.addOnetimeObserver('//event/test', cb.call) d.dispatch(None, '//event/test') self.assertEqual(1, cb.called) self.assertEqual(0, len(d._eventObservers.pop(0)))
def testOrderedXPathDispatch(self): d = EventDispatcher() cb = OrderedCallbackTracker() d.addObserver("/message/body", cb.call2) d.addObserver("/message", cb.call3, -1) d.addObserver("/message/body", cb.call1, 1) msg = Element(("ns", "message")) msg.addElement("body") d.dispatch(msg) self.assertEquals( cb.callList, [cb.call1, cb.call2, cb.call3], "Calls out of order: %s" % repr([c.__name__ for c in cb.callList]))
def test_cleanUpOnetimeXPathObserver(self): """ Test observer clean-up after onetime XPath events. """ d = EventDispatcher() cb = CallbackTracker() msg = Element((None, "message")) d.addOnetimeObserver('/message', cb.call) d.dispatch(msg) self.assertEqual(1, cb.called) self.assertEqual(0, len(d._xpathObservers.pop(0)))
class XmlStreamStub(object): """ Stub for testing objects that communicate through XML Streams. Instances of this stub hold an object in L{xmlstream} that acts like an L{XmlStream<twisted.words.xish.xmlstream.XmlStream} after connection stream initialization. Stanzas can be sent through it by calling its C{send} method with an object implementing L{IElement<twisted.words.xish.domish.IElement>} as its first argument. These appear in sequence in the L{output} instance variable of the stub. For the reverse direction, stanzas passed to L{send} of the stub, will be dispatched in the stubbed XmlStream as if it was received over the wire, so that registered observers will be called. Example:: >>> stub = XmlStreamStub() >>> stub.xmlstream.send(domish.Element((None, 'presence'))) >>> stub.output[-1].toXml() u'<presence/>' >>> def cb(stanza): ... print("Got: %r" stanza.toXml()) >>> stub.xmlstream.addObserver('/presence') >>> stub.send(domish.Element((None, 'presence'))) Got: u'<presence/>' @ivar xmlstream: Stubbed XML Stream. @type xmlstream: L{EventDispatcher} @ivar output: List of stanzas sent to the XML Stream. @type output: L{list} """ def __init__(self): self.output = [] self.xmlstream = EventDispatcher() self.xmlstream.send = self.output.append def send(self, obj): """ Pass an element to the XML Stream as if received. @param obj: Element to be dispatched to C{self.xmlstream}. @type obj: object implementing L{IElement<twisted.words.xish.domish.IElement>}. """ self.xmlstream.dispatch(obj)
def testOrderedXPathDispatch(self): d = EventDispatcher() cb = OrderedCallbackTracker() d.addObserver("/message/body", cb.call2) d.addObserver("/message", cb.call3, -1) d.addObserver("/message/body", cb.call1, 1) msg = Element(("ns", "message")) msg.addElement("body") d.dispatch(msg) self.assertEquals(cb.callList, [cb.call1, cb.call2, cb.call3], "Calls out of order: %s" % repr([c.__name__ for c in cb.callList]))
def testOnetimeDispatch(self): d = EventDispatcher() msg = Element(("ns", "message")) cb = CallbackTracker() d.addOnetimeObserver("/message", cb.call) d.dispatch(msg) self.assertEquals(cb.called, 1) d.dispatch(msg) self.assertEquals(cb.called, 1)
def test_addObserverTwice(self): """ Test adding two observers for the same query. When the event is dispath both of the observers need to be called. """ d = EventDispatcher() cb1 = CallbackTracker() cb2 = CallbackTracker() d.addObserver("//event/testevent", cb1.call) d.addObserver("//event/testevent", cb2.call) d.dispatch(d, "//event/testevent") self.assertEquals(cb1.called, 1) self.assertEquals(cb1.obj, d) self.assertEquals(cb2.called, 1) self.assertEquals(cb2.obj, d)
def testAddObserverInDispatch(self): # Test for registration of events during dispatch d = EventDispatcher() msg = Element(("ns", "message")) pres = Element(("ns", "presence")) cb = CallbackTracker2(d) d.addObserver("/message", cb.call2) d.dispatch(msg) self.assertEquals(cb.called, 0) d.dispatch(pres) self.assertEquals(cb.called, 1)
def test_cleanUpRemoveEventObserver(self): """ Test observer clean-up after removeObserver for named events. """ d = EventDispatcher() cb = CallbackTracker() d.addObserver('//event/test', cb.call) d.dispatch(None, '//event/test') self.assertEqual(1, cb.called) d.removeObserver('//event/test', cb.call) self.assertEqual(0, len(d._eventObservers.pop(0)))
def test_addOnetimeObserverInDispatch(self): """ Test for registration of a onetime observer during dispatch. """ d = EventDispatcher() msg = Element(("ns", "message")) cb = CallbackTracker() def onMessage(msg): d.addOnetimeObserver("/message", cb.call) d.addOnetimeObserver("/message", onMessage) d.dispatch(msg) self.assertEquals(cb.called, 0) d.dispatch(msg) self.assertEquals(cb.called, 1) d.dispatch(msg) self.assertEquals(cb.called, 1)
def test_cleanUpRemoveXPathObserver(self): """ Test observer clean-up after removeObserver for XPath events. """ d = EventDispatcher() cb = CallbackTracker() msg = Element((None, "message")) d.addObserver('/message', cb.call) d.dispatch(msg) self.assertEqual(1, cb.called) d.removeObserver('/message', cb.call) self.assertEqual(0, len(d._xpathObservers.pop(0)))
def test_cleanUpRemoveEventObserver(self): """ Test observer clean-up after removeObserver for named events. """ d = EventDispatcher() cb = CallbackTracker() d.addObserver("//event/test", cb.call) d.dispatch(None, "//event/test") self.assertEqual(1, cb.called) d.removeObserver("//event/test", cb.call) self.assertEqual(0, len(d._eventObservers.pop(0)))
def test_cleanUpOnetimeEventObserver(self): """ Test observer clean-up after onetime named events. """ d = EventDispatcher() cb = CallbackTracker() d.addOnetimeObserver("//event/test", cb.call) d.dispatch(None, "//event/test") self.assertEqual(1, cb.called) self.assertEqual(0, len(d._eventObservers.pop(0)))
def test_observerRaisingException(self): """ Test that exceptions in observers do not bubble up to dispatch. The exceptions raised in observers should be logged and other observers should be called as if nothing happened. """ class OrderedCallbackList(utility.CallbackList): def __init__(self): self.callbacks = OrderedDict() class TestError(Exception): pass def raiseError(_): raise TestError() d = EventDispatcher() cb = CallbackTracker() originalCallbackList = utility.CallbackList try: utility.CallbackList = OrderedCallbackList d.addObserver('//event/test', raiseError) d.addObserver('//event/test', cb.call) try: d.dispatch(None, '//event/test') except TestError: self.fail("TestError raised. Should have been logged instead.") self.assertEqual(1, len(self.flushLoggedErrors(TestError))) self.assertEqual(1, cb.called) finally: utility.CallbackList = originalCallbackList
def __init__(self, *args, **kw): service.MultiService.__init__(self, *args, **kw) EventDispatcher.__init__(self, *args, **kw) self.components = {}
def testStuff(self): d = EventDispatcher() cb1 = CallbackTracker() cb2 = CallbackTracker() cb3 = CallbackTracker() d.addObserver("/message/body", cb1.call) d.addObserver("/message", cb1.call) d.addObserver("/presence", cb2.call) d.addObserver("//event/testevent", cb3.call) msg = Element(("ns", "message")) msg.addElement("body") pres = Element(("ns", "presence")) pres.addElement("presence") d.dispatch(msg) self.assertEquals(cb1.called, 2) self.assertEquals(cb1.obj, msg) self.assertEquals(cb2.called, 0) d.dispatch(pres) self.assertEquals(cb1.called, 2) self.assertEquals(cb2.called, 1) self.assertEquals(cb2.obj, pres) self.assertEquals(cb3.called, 0) d.dispatch(d, "//event/testevent") self.assertEquals(cb3.called, 1) self.assertEquals(cb3.obj, d) d.removeObserver("/presence", cb2.call) d.dispatch(pres) self.assertEquals(cb2.called, 1)
def __init__(self, **kwargs): EventDispatcher.__init__(self, **kwargs) self.listener = {}
def __init__(self): self.output = [] self.xmlstream = EventDispatcher() self.xmlstream.send = self.output.append
class Application: """ Viper application Core component, handles the loading of configuration, instances all the modules and their services. """ # # Events # eventDispatcher = EventDispatcher() kEventApplicationStart = "//event/applicationStart" kEventApplicationStop = "//event/applicationStop" def __init__(self): self.requestDispatcher = Dispatcher(self) self.config = self._getConfiguration() self._interfaces = self._getInterfaces() self._services = {} self._loadViperServices() self._modules = self._getModules() # # Configuration # def _getConfiguration(self): """ Load application configuration files. :return: <dict> """ configDirectoryPath = os.path.join("application", "config") config = Config(configDirectoryPath) configData = config.getData() # setting application parameters reactor.suggestThreadPoolSize( int(configData["performance"]["threadPoolSize"])) return configData # # Interfaces # def _getInterfaces(self): """ Load application communication interfaces. :return: <dict> """ interfaces = {} interfacesPath = os.path.join("application", "interface") interfaceList = os.listdir(interfacesPath) for file in interfaceList: interfaceDirectoryPath = os.path.join(interfacesPath, file) if not os.path.isdir(interfaceDirectoryPath) or file.startswith( "__") or file.startswith("."): continue interfaceName = ntpath.basename(interfaceDirectoryPath) interfacePath = os.path.join(interfaceDirectoryPath, interfaceName) + ".py" if not os.path.isfile(interfacePath): continue # importing interface interfaceSpec = importlib.util.spec_from_file_location( interfaceName, interfacePath) interface = importlib.util.module_from_spec(interfaceSpec) interfaceSpec.loader.exec_module(interface) # checking if there is an interface in the file if hasattr(interface, "Service"): # initializing interface interfaceInstance = interface.Service(self) interfaces[interfaceName] = interfaceInstance return interfaces def getInterfaces(self): """ Return loaded communication interfaces. :return: <dict> """ return self._interfaces # # Modules # def _getModules(self): """ Import and load application modules. :return: <dict> """ modules = {} modulesPath = os.path.join("application", "module") moduleList = os.listdir(modulesPath) for moduleName in moduleList: modulePath = os.path.join(modulesPath, moduleName, "module.py") if not os.path.isfile(modulePath): continue # importing module moduleSpec = importlib.util.spec_from_file_location( moduleName, modulePath) module = importlib.util.module_from_spec(moduleSpec) moduleSpec.loader.exec_module(module) # initializing module moduleInstance = module.Module(self) modules[moduleName] = moduleInstance return modules def isModuleLoaded(self, moduleName): """ Verify if a module is loaded. :param moduleName: <str> module name :return: <bool> True if loaded, False otherwise """ if moduleName in self._modules: return True return False # # Services # def _loadViperServices(self): """ Load application bundled services. :return: <void> """ servicesPath = os.path.join( os.path.dirname(os.path.realpath(__file__)), "service") for serviceFile in os.listdir(servicesPath): if serviceFile.startswith("__") or serviceFile.startswith("."): continue serviceName = serviceFile.replace(".py", "") servicePath = os.path.join(servicesPath, serviceFile) if not os.path.isfile(servicePath): continue # importing service serviceSpec = importlib.util.spec_from_file_location( serviceName, servicePath) service = importlib.util.module_from_spec(serviceSpec) serviceSpec.loader.exec_module(service) # initializing service serviceInstance = service.Service(self) self.addService("viper", serviceName, serviceInstance) def addService(self, moduleName, serviceName, service): """ Add a service instance to the application service pool. :param moduleName: <str> module name in which the service is located :param serviceName: <str> service name :param service: <object> service instance :return: <void> """ serviceIdentifier = "{}.{}".format(moduleName, serviceName) if serviceIdentifier not in self._services: self._services[serviceIdentifier] = service else: message = "Application - addService() - " \ "A service with the identifier {} already exists." \ .format(serviceIdentifier) raise Exception(message) def getService(self, serviceIdentifier): """ Return the requested service instance. :param serviceIdentifier: <str> service identifier :return: <object> service instance """ if serviceIdentifier in self._services: return self._services[serviceIdentifier] else: message = "Application - getService() - " \ "Service with identifier {} does not exist." \ .format(serviceIdentifier) raise Exception(message) def start(self): """ Start the application. :return: <void> """ self.eventDispatcher.dispatch(None, self.kEventApplicationStart) def stop(self): """ Stop the application. :return: <void> """ self.eventDispatcher.dispatch(None, self.kEventApplicationStop)