From 214abc8d31927673d9b853cf6a901cec1f3988ea Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 23 Aug 2016 09:33:43 -0700 Subject: [PATCH] minor class-based namespace fixes --- socketio/namespace.py | 9 ++++++++- socketio/server.py | 2 +- tests/test_namespace.py | 22 +++++++++++----------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/socketio/namespace.py b/socketio/namespace.py index 4772788..6450ffe 100644 --- a/socketio/namespace.py +++ b/socketio/namespace.py @@ -14,10 +14,17 @@ class Namespace(object): self.namespace = namespace or '/' self.server = None - def set_server(self, server): + def _set_server(self, server): self.server = server def trigger_event(self, event, *args): + """Dispatch an event to the proper handler method. + + In the most common usage, this method is not overloaded by subclasses, + as it performs the routing of events to methods. However, this + method can be overriden if special dispatching rules are needed, or if + having a single method that catches all events is desired. + """ handler_name = 'on_' + event if hasattr(self, handler_name): return getattr(self, handler_name)(*args) diff --git a/socketio/server.py b/socketio/server.py index 65590ac..56152f5 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -161,7 +161,7 @@ class Server(object): """ if not isinstance(namespace_handler, namespace.Namespace): raise ValueError('Not a namespace instance') - namespace_handler.set_server(self) + namespace_handler._set_server(self) self.namespace_handlers[namespace_handler.namespace] = \ namespace_handler diff --git a/tests/test_namespace.py b/tests/test_namespace.py index f4a7bac..514fe9b 100644 --- a/tests/test_namespace.py +++ b/tests/test_namespace.py @@ -17,7 +17,7 @@ class TestNamespace(unittest.TestCase): result['result'] = (sid, environ) ns = MyNamespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.trigger_event('connect', 'sid', {'foo': 'bar'}) self.assertEqual(result['result'], ('sid', {'foo': 'bar'})) @@ -29,7 +29,7 @@ class TestNamespace(unittest.TestCase): result['result'] = sid ns = MyNamespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.trigger_event('disconnect', 'sid') self.assertEqual(result['result'], 'sid') @@ -41,7 +41,7 @@ class TestNamespace(unittest.TestCase): result['result'] = (sid, data) ns = MyNamespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.trigger_event('custom_message', 'sid', {'data': 'data'}) self.assertEqual(result['result'], ('sid', {'data': 'data'})) @@ -53,13 +53,13 @@ class TestNamespace(unittest.TestCase): result['result'] = (sid, data) ns = MyNamespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.trigger_event('another_custom_message', 'sid', {'data': 'data'}) self.assertEqual(result, {}) def test_emit(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.emit('ev', data='data', room='room', skip_sid='skip', callback='cb') ns.server.emit.assert_called_with( @@ -73,7 +73,7 @@ class TestNamespace(unittest.TestCase): def test_send(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.send(data='data', room='room', skip_sid='skip', callback='cb') ns.server.send.assert_called_with( 'data', room='room', skip_sid='skip', namespace='/foo', @@ -86,7 +86,7 @@ class TestNamespace(unittest.TestCase): def test_enter_room(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.enter_room('sid', 'room') ns.server.enter_room.assert_called_with('sid', 'room', namespace='/foo') @@ -96,7 +96,7 @@ class TestNamespace(unittest.TestCase): def test_leave_room(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.leave_room('sid', 'room') ns.server.leave_room.assert_called_with('sid', 'room', namespace='/foo') @@ -106,7 +106,7 @@ class TestNamespace(unittest.TestCase): def test_close_room(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.close_room('room') ns.server.close_room.assert_called_with('room', namespace='/foo') ns.close_room('room', namespace='/bar') @@ -114,7 +114,7 @@ class TestNamespace(unittest.TestCase): def test_rooms(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.rooms('sid') ns.server.rooms.assert_called_with('sid', namespace='/foo') ns.rooms('sid', namespace='/bar') @@ -122,7 +122,7 @@ class TestNamespace(unittest.TestCase): def test_disconnect(self): ns = namespace.Namespace('/foo') - ns.set_server(mock.MagicMock()) + ns._set_server(mock.MagicMock()) ns.disconnect('sid') ns.server.disconnect.assert_called_with('sid', namespace='/foo') ns.disconnect('sid', namespace='/bar')