Browse Source

DRY: collect get_handler routines into base_server

pull/1288/head
mooomooo 1 year ago
parent
commit
fda1469e6e
  1. 43
      src/socketio/async_server.py
  2. 48
      src/socketio/base_server.py
  3. 38
      src/socketio/server.py

43
src/socketio/async_server.py

@ -615,44 +615,9 @@ class AsyncServer(base_server.BaseServer):
await self.manager.trigger_callback(sid, id, data)
async def _trigger_event(self, event, namespace, *args):
"""Invoke an application event handler.
Resolution priority:
- self.handlers[namespace][event]
- self.handlers[namespace]["*"]
- self.handlers["*"][event]
- self.handlers["*"]["*"]
- self.namespace_handlers[namespace]
- self.namespace_handlers["*"]
- self.not_handled
"""
handler = None
"""Invoke an application event handler."""
# first see if we have an explicit handler for the event
if namespace in self.handlers and \
event in self.handlers[namespace]:
handler = self.handlers[namespace][event]
elif namespace in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]:
handler = self.handlers[namespace]['*']
args = (event, *args)
elif '*' in self.handlers and \
event in self.handlers['*']:
handler = self.handlers['*'][event]
args = (namespace, *args)
elif '*' in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers['*']:
handler = self.handlers['*']['*']
args = (event, namespace, *args)
# or else, forward the event to a namepsace handler if one exists
elif namespace in self.namespace_handlers: # pragma: no branch
return await self.namespace_handlers[namespace].trigger_event(
event, *args)
elif '*' in self.namespace_handlers: # pragma: no branch
return await self.namespace_handlers['*'].trigger_event(
event, namespace, *args)
handler, args = self._get_event_handler(event, namespace, *args)
if handler:
if asyncio.iscoroutinefunction(handler):
try:
@ -662,6 +627,10 @@ class AsyncServer(base_server.BaseServer):
else:
ret = handler(*args)
return ret
# or else, forward the event to a namespace handler if one exists
handler, args = self._get_namespace_handler(namespace, *args)
if handler:
return await handler.trigger_event(event, *args)
else:
return self.not_handled

48
src/socketio/base_server.py

@ -196,6 +196,54 @@ class BaseServer:
eio_sid = self.manager.eio_sid_from_sid(sid, namespace or '/')
return self.environ.get(eio_sid)
def _get_event_handler(self, event, namespace, *args):
"""Return the appropriate application event handler.
Resolution priority:
- self.handlers[namespace][event]
- self.handlers[namespace]["*"]
- self.handlers["*"][event]
- self.handlers["*"]["*"]
"""
handler = None
if namespace in self.handlers and \
event in self.handlers[namespace]:
handler = self.handlers[namespace][event]
elif namespace in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]:
handler = self.handlers[namespace]['*']
args = (event, *args)
elif '*' in self.handlers and \
event in self.handlers['*']:
handler = self.handlers['*'][event]
args = (namespace, *args)
elif '*' in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers['*']:
handler = self.handlers['*']['*']
args = (event, namespace, *args)
else:
handler = None
return handler, args
def _get_namespace_handler(self, namespace, *args):
"""Return the appropriate application event handler.
Resolution priority:
- self.namespace_handlers[namespace]
- self.namespace_handlers["*"]
"""
handler = None
if namespace in self.namespace_handlers:
handler = self.namespace_handlers[namespace]
elif '*' in self.namespace_handlers:
handler = self.namespace_handlers['*']
args = (namespace, *args)
else:
handler = None
return handler, args
def _handle_eio_connect(self): # pragma: no cover
raise NotImplementedError()

38
src/socketio/server.py

@ -602,39 +602,15 @@ class Server(base_server.BaseServer):
self.manager.trigger_callback(sid, id, data)
def _trigger_event(self, event, namespace, *args):
"""Invoke an application event handler.
Resolution priority:
- self.handlers[namespace][event]
- self.handlers[namespace]["*"]
- self.handlers["*"][event]
- self.handlers["*"]["*"]
- self.namespace_handlers[namespace]
- self.namespace_handlers["*"]
- self.not_handled
"""
"""Invoke an application event handler."""
# first see if we have an explicit handler for the event
if namespace in self.handlers and \
event in self.handlers[namespace]:
return self.handlers[namespace][event](*args)
elif namespace in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]:
return self.handlers[namespace]['*'](event, *args)
elif '*' in self.handlers and \
event in self.handlers['*']:
return self.handlers['*'][event](namespace, *args)
elif '*' in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]:
return self.handlers['*']['*'](event, namespace, *args)
handler, args = self._get_event_handler(event, namespace, *args)
if handler:
return handler(*args)
# or else, forward the event to a namespace handler if one exists
elif namespace in self.namespace_handlers: # pragma: no branch
return self.namespace_handlers[namespace].trigger_event(
event, *args)
elif '*' in self.namespace_handlers: # pragma: no branch
return self.namespace_handlers['*'].trigger_event(
event, namespace, *args)
handler, args = self._get_namespace_handler(namespace, *args)
if handler:
return handler.trigger_event(event, *args)
else:
return self.not_handled

Loading…
Cancel
Save