From fda1469e6e4de3da69694b92e9880d865aeb36de Mon Sep 17 00:00:00 2001 From: mooomooo Date: Fri, 29 Dec 2023 02:06:14 -0800 Subject: [PATCH] DRY: collect get_handler routines into base_server --- src/socketio/async_server.py | 43 +++++--------------------------- src/socketio/base_server.py | 48 ++++++++++++++++++++++++++++++++++++ src/socketio/server.py | 38 ++++++---------------------- 3 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/socketio/async_server.py b/src/socketio/async_server.py index e546bd7..b48327f 100644 --- a/src/socketio/async_server.py +++ b/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 diff --git a/src/socketio/base_server.py b/src/socketio/base_server.py index f8c9000..eafd35b 100644 --- a/src/socketio/base_server.py +++ b/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() diff --git a/src/socketio/server.py b/src/socketio/server.py index d73a244..afc1cb8 100644 --- a/src/socketio/server.py +++ b/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