Commit 5303bb12 authored by Philipp Hörist's avatar Philipp Hörist

Dispatcher: Refactor handler registration

parent 8a817d6c
Pipeline #5540 passed with stages
in 37 seconds
......@@ -183,7 +183,7 @@ class StanzaDispatcher(Observable):
for instance in self._modules.values():
for handler in instance.handlers:
self.register_handler(*handler)
self.register_handler(handler)
def reset_parser(self):
if self._parser is not None:
......@@ -253,75 +253,69 @@ class StanzaDispatcher(Observable):
tag_name, xmlns, protocol)
self._handlers[xmlns][tag_name] = {'type': protocol, 'default': []}
def register_handler(self, name, handler, typ='', ns='',
xmlns=None, priority=50):
def register_handler(self, handler):
"""
Register handler
:param name: name of top level tag, example: iq, message, presence
:param handler: callback
:param typ: value of stanza's "type" attribute.
If not specified any value will match
:param ns: Namespace of child that stanza must contain
:param xmlns: XML namespace, only needed if not jabber:client
:param priority: The priority of the handler, higher get called later
"""
if not xmlns:
xmlns = Namespace.CLIENT
xmlns = handler.xmlns or Namespace.CLIENT
if not typ and not ns:
typ = handler.typ
if not typ and not handler.ns:
typ = 'default'
self._log.debug(
'Register handler %s for "%s" type->%s ns->%s(%s) priority->%s',
handler, name, typ, ns, xmlns, priority)
handler.callback, handler.name, typ, handler.ns,
xmlns, handler.priority
)
if xmlns not in self._handlers:
self._register_namespace(xmlns)
if name not in self._handlers[xmlns]:
self._register_protocol(name, Protocol, xmlns)
if handler.name not in self._handlers[xmlns]:
self._register_protocol(handler.name, Protocol, xmlns)
specific = typ + ns
if specific not in self._handlers[xmlns][name]:
self._handlers[xmlns][name][specific] = []
specific = typ + handler.ns
if specific not in self._handlers[xmlns][handler.name]:
self._handlers[xmlns][handler.name][specific] = []
self._handlers[xmlns][name][specific].append(
{'func': handler,
'priority': priority,
self._handlers[xmlns][handler.name][specific].append(
{'func': handler.callback,
'priority': handler.priority,
'specific': specific})
def unregister_handler(self, name, handler, typ='', ns='', xmlns=None):
def unregister_handler(self, handler):
"""
Unregister handler
"""
if not xmlns:
xmlns = Namespace.CLIENT
xmlns = handler.xmlns or Namespace.CLIENT
if not typ and not ns:
typ = handler.typ
if not typ and not handler.ns:
typ = 'default'
specific = typ + ns
specific = typ + handler.ns
try:
self._handlers[xmlns][name][specific]
self._handlers[xmlns][handler.name][specific]
except KeyError:
return
for handler_dict in self._handlers[xmlns][name][specific]:
if handler_dict['func'] != handler:
for handler_dict in self._handlers[xmlns][handler.name][specific]:
if handler_dict['func'] != handler.callback:
return
try:
self._handlers[xmlns][name][specific].remove(handler_dict)
self._handlers[xmlns][handler.name][specific].remove(
handler_dict)
except ValueError:
self._log.warning(
'Unregister failed: %s for "%s" type->%s ns->%s(%s)',
handler, name, typ, ns, xmlns)
handler.callback, handler.name, typ, handler.ns, xmlns)
else:
self._log.debug(
'Unregister handler %s for "%s" type->%s ns->%s(%s)',
handler, name, typ, ns, xmlns)
handler.callback, handler.name, typ, handler.ns, xmlns)
def _default_handler(self, stanza):
"""
......
......@@ -22,6 +22,7 @@ from nbxmpp.namespaces import Namespace
from nbxmpp.simplexml import Node
from nbxmpp.const import StreamState
from nbxmpp.util import LogAdapter
from nbxmpp.structs import StanzaHandler
log = logging.getLogger('nbxmpp.smacks')
......@@ -80,14 +81,23 @@ class Smacks:
self._on_failed(None, stanza, None)
def register_handlers(self):
self._client.register_handler(
'enabled', self._on_enabled, xmlns=Namespace.STREAM_MGMT)
self._client.register_handler(
'failed', self._on_failed, xmlns=Namespace.STREAM_MGMT)
self._client.register_handler(
'r', self._send_ack, xmlns=Namespace.STREAM_MGMT)
self._client.register_handler(
'a', self._on_ack, xmlns=Namespace.STREAM_MGMT)
handlers = [
StanzaHandler(name='enabled',
callback=self._on_enabled,
xmlns=Namespace.STREAM_MGMT),
StanzaHandler(name='failed',
callback=self._on_failed,
xmlns=Namespace.STREAM_MGMT),
StanzaHandler(name='r',
callback=self._send_ack,
xmlns=Namespace.STREAM_MGMT),
StanzaHandler(name='a',
callback=self._on_ack,
xmlns=Namespace.STREAM_MGMT)
]
for handler in handlers:
self._client.register_handler(handler)
def send_enable(self):
if not self.sm_supported:
......
......@@ -46,8 +46,8 @@ class ActivityTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -51,8 +51,8 @@ class AvatarTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -62,9 +62,9 @@ class BookmarkTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -107,8 +107,8 @@ class BookmarkTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -87,8 +87,8 @@ class LocationTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -42,8 +42,8 @@ class MoodTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
......@@ -30,10 +30,10 @@ class PubsubTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
self.dispatcher.process_data(event)
......@@ -60,10 +60,10 @@ class PubsubTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
self.dispatcher.process_data(event)
......@@ -91,9 +91,9 @@ class PubsubTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT,
priority=16))
self.dispatcher.process_data(event)
......@@ -53,8 +53,8 @@ class TuneTest(StanzaHandlerTest):
'''
self.dispatcher.register_handler(
*StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
StanzaHandler(name='message',
callback=_on_message,
ns=Namespace.PUBSUB_EVENT))
self.dispatcher.process_data(event)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment