Skip to content

Commit ce4bf3d

Browse files
authored
Merge pull request #1591 from tonypzy/refactor/socket-handler-base-initialization
refactor: move socket handler initialization to base handler
2 parents 5e2d902 + ea5d887 commit ce4bf3d

2 files changed

Lines changed: 36 additions & 30 deletions

File tree

py/visdom/server/handlers/base_handlers.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,47 @@
1919
import tornado.websocket
2020

2121

22+
_COMMON_APP_ATTRIBUTES = (
23+
"state",
24+
"subs",
25+
"sources",
26+
"port",
27+
"env_path",
28+
"storage",
29+
"login_enabled",
30+
)
31+
32+
_WEB_APP_ATTRIBUTES = _COMMON_APP_ATTRIBUTES + (
33+
"max_text_lines",
34+
"max_old_content",
35+
"max_image_history",
36+
)
37+
38+
_SOCKET_APP_ATTRIBUTES = _COMMON_APP_ATTRIBUTES + ("readonly",)
39+
40+
41+
def _copy_app_attributes(handler, app, attrs, store_app=False):
42+
if app is None:
43+
return
44+
45+
if store_app:
46+
handler.app = app
47+
48+
for attr in attrs:
49+
setattr(handler, attr, getattr(app, attr))
50+
51+
2252
class BaseWebSocketHandler(tornado.websocket.WebSocketHandler):
2353
"""
2454
Implements any required overriden functionality from the basic tornado
2555
websocket handler. Also contains some shared logic for all WebSocketHandler
2656
classes.
2757
"""
2858

59+
def initialize(self, app=None):
60+
"""Common initialization shared by WebSocket handlers."""
61+
_copy_app_attributes(self, app, _SOCKET_APP_ATTRIBUTES, store_app=True)
62+
2963
def get_current_user(self):
3064
"""
3165
This method determines the self.current_user
@@ -54,17 +88,7 @@ def initialize(self, app=None):
5488
The ``app`` parameter defaults to ``None`` so that handlers
5589
registered without an ``app`` dict (e.g. HealthHandler) still work.
5690
"""
57-
if app is not None:
58-
self.state = app.state
59-
self.subs = app.subs
60-
self.sources = app.sources
61-
self.port = app.port
62-
self.env_path = app.env_path
63-
self.storage = app.storage
64-
self.login_enabled = app.login_enabled
65-
self.max_text_lines = app.max_text_lines
66-
self.max_old_content = app.max_old_content
67-
self.max_image_history = app.max_image_history
91+
_copy_app_attributes(self, app, _WEB_APP_ATTRIBUTES)
6892

6993
def is_authorized(self):
7094
"""Update access time and validate authentication for protected methods."""

py/visdom/server/handlers/socket_handlers.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242

4343
# TODO move the logic that actually parses environments and layouts to
4444
# new classes in the data_model folder.
45-
# TODO move generalized initialization logic from these handlers into the
46-
# basehandler
4745
# TODO abstract out any direct references to the app where possible from
4846
# all handlers. Can instead provide accessor functions on the state?
4947
# TODO Try to standardize the code between the client-server and
@@ -71,17 +69,6 @@ def __init__(self, *args, **kwargs):
7169
self.polling = False
7270
super().__init__(*args, **kwargs)
7371

74-
def initialize(self, app):
75-
self.state = app.state
76-
self.subs = app.subs
77-
self.sources = app.sources
78-
self.port = app.port
79-
self.env_path = app.env_path
80-
self.storage = app.storage
81-
self.login_enabled = app.login_enabled
82-
self.app = app
83-
self.readonly = app.readonly
84-
8572
def open(self, register_to="sources"):
8673
self.sid = get_rand_id()
8774
register_list = self.sources if register_to == "sources" else self.subs
@@ -569,12 +556,7 @@ def to_failure_response(self, message=""):
569556
def WrapSocketWrapper(BaseWrapper):
570557
class WrappedSocketWrap(BaseHandler):
571558
def initialize(self, app):
572-
self.state = app.state
573-
self.subs = app.subs
574-
self.sources = app.sources
575-
self.port = app.port
576-
self.env_path = app.env_path
577-
self.login_enabled = app.login_enabled
559+
super().initialize(app)
578560
self.app = app
579561

580562
@check_auth

0 commit comments

Comments
 (0)