From 012b86cb2b461a808f06344d1d3130a76a5cccc7 Mon Sep 17 00:00:00 2001 From: Marcus Oskarsson Date: Fri, 15 Sep 2023 10:59:46 +0200 Subject: [PATCH 1/3] MXCUBEApplication and Server are static clases and should not be instanciated --- mxcube3/__init__.py | 20 ++++++-------------- mxcube3/app.py | 4 +++- mxcube3/core/components/chat.py | 1 - mxcube3/core/components/samplechanger.py | 9 +++++---- mxcube3/core/components/sampleview.py | 2 +- mxcube3/routes/signals.py | 4 ++-- mxcube3/state_storage.py | 4 ++-- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/mxcube3/__init__.py b/mxcube3/__init__.py index dcdb05696..016174df5 100644 --- a/mxcube3/__init__.py +++ b/mxcube3/__init__.py @@ -7,6 +7,7 @@ from gevent import monkey + monkey.patch_all(thread=False) from mxcube3.server import Server @@ -18,9 +19,6 @@ sys.modules["Qub"] = mock.Mock() sys.modules["Qub.CTools"] = mock.Mock() -mxcube = None -server = None - def parse_args(argv): XML_DIR = os.path.join( @@ -103,9 +101,6 @@ def parse_args(argv): def build_server_and_config(test=False, argv=None): - global mxcube - global server - cmdline_options = parse_args(argv) try: @@ -116,9 +111,6 @@ def build_server_and_config(test=False, argv=None): return (None, None) try: - mxcube = MXCUBEApplication() - server = Server() - # This refactoring (with other bits) allows you to pass a 'path1:path2' lookup path # as the hwr_directory. I need it for sensible managing of a multi-beamline test set-up # without continuously editing teh main config files. @@ -131,9 +123,9 @@ def build_server_and_config(test=False, argv=None): if test: cfg.flask.USER_DB_PATH = "/tmp/mxcube-test-user.db" - server.init(cmdline_options, cfg, mxcube) - mxcube.init( - server, + Server.init(cmdline_options, cfg, MXCUBEApplication) + MXCUBEApplication.init( + Server, cmdline_options.allow_remote, cmdline_options.ra_timeout, cmdline_options.log_file, @@ -142,12 +134,12 @@ def build_server_and_config(test=False, argv=None): cfg, ) - server.register_routes(mxcube) + Server.register_routes(MXCUBEApplication) except Exception: traceback.print_exc() raise - return (server, cfg) + return (Server, cfg) def main(): diff --git a/mxcube3/app.py b/mxcube3/app.py index 04b19491f..0372a225b 100644 --- a/mxcube3/app.py +++ b/mxcube3/app.py @@ -298,7 +298,9 @@ def init( MXCUBEApplication.init_state_storage() # MXCUBEApplication.load_settings() - msg = "MXCuBE 3 initialized, it took %.1f seconds" % (time.time() - MXCUBEApplication.t0) + msg = "MXCuBE 3 initialized, it took %.1f seconds" % ( + time.time() - MXCUBEApplication.t0 + ) logging.getLogger("MX3.HWR").info(msg) @staticmethod diff --git a/mxcube3/core/components/chat.py b/mxcube3/core/components/chat.py index dfe595327..f7a3f2e04 100644 --- a/mxcube3/core/components/chat.py +++ b/mxcube3/core/components/chat.py @@ -19,7 +19,6 @@ def db_add_message(self, user, message): self.app.server.user_datastore.add_message_to_user(_user, _m) def append_message(self, message, user): - data = { "message": message, "username": user.username, diff --git a/mxcube3/core/components/samplechanger.py b/mxcube3/core/components/samplechanger.py index 84fe2e286..d6b525a3f 100644 --- a/mxcube3/core/components/samplechanger.py +++ b/mxcube3/core/components/samplechanger.py @@ -354,8 +354,8 @@ def get_initial_state(self): def sync_with_crims(self): """ To be use mostly when Diffractometer is in plate mode - This retun a List of crystal dict available in Crims that have been Harvested - With this user can visualize easier where the crystal are in Plate GUI + This retun a List of crystal dict available in Crims that have been Harvested + With this user can visualize easier where the crystal are in Plate GUI """ xtal_list = [] try: @@ -370,7 +370,7 @@ def sync_with_crims(self): "offset_y": x.offset_y, "image_url": x.image_url, "image_date": x.image_date, - "sample": x.sample + "sample": x.sample, } xtal_list.append(response) res = {"xtal_list": xtal_list} @@ -382,7 +382,7 @@ def sync_with_crims(self): def queue_mount_sample(view, data_model, centring_done_cb, async_result): from mxcube3.routes import signals - from mxcube3 import mxcube + from mxcube3.app import MXCUBEApplication as mxcube HWR.beamline.sample_view.clear_all() logging.getLogger("user_level_log").info("Loading sample ...") @@ -516,6 +516,7 @@ def queue_mount_sample(view, data_model, centring_done_cb, async_result): else: dm.disconnect("centringAccepted", centring_done_cb) + def patch_queue_entry_mount_sample(): # Important, patch queue_entry.mount_sample with the mount_sample defined above queue_entry.base_queue_entry.mount_sample = queue_mount_sample diff --git a/mxcube3/core/components/sampleview.py b/mxcube3/core/components/sampleview.py index 9574c3275..0c6c88090 100644 --- a/mxcube3/core/components/sampleview.py +++ b/mxcube3/core/components/sampleview.py @@ -537,7 +537,7 @@ def save_snapshot(self, filename, bw=False): # _do_take_snapshot(filename, bw) def take_snapshots(self, snapshots=None, _do_take_snapshot=_do_take_snapshot): - from mxcube3 import mxcube + from mxcube3.app import MXCUBEApplication as mxcube if snapshots is None: # called via AbstractCollect diff --git a/mxcube3/routes/signals.py b/mxcube3/routes/signals.py index 33bb3a7fa..61f30b1db 100644 --- a/mxcube3/routes/signals.py +++ b/mxcube3/routes/signals.py @@ -1,8 +1,8 @@ import logging import json -from mxcube3 import server -from mxcube3 import mxcube +from mxcube3.server import Server as server +from mxcube3.app import MXCUBEApplication as mxcube from flask import Response diff --git a/mxcube3/state_storage.py b/mxcube3/state_storage.py index 093f6b75c..d034dc528 100644 --- a/mxcube3/state_storage.py +++ b/mxcube3/state_storage.py @@ -1,6 +1,6 @@ from flask_socketio import emit -from mxcube3 import server -from mxcube3 import mxcube +from mxcube3.server import Server as server +from mxcube3.app import MXCUBEApplication as mxcube import json From 83b14bef612480fb1381b3f432cef356fef913b7 Mon Sep 17 00:00:00 2001 From: Marcus Oskarsson Date: Wed, 20 Sep 2023 13:49:48 +0200 Subject: [PATCH 2/3] Raising not implemented error on instanciation --- mxcube3/app.py | 5 +++++ mxcube3/server.py | 15 +++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mxcube3/app.py b/mxcube3/app.py index 0372a225b..2745c0a7c 100644 --- a/mxcube3/app.py +++ b/mxcube3/app.py @@ -246,6 +246,11 @@ class MXCUBEApplication: server = None + def __init__(self): + raise NotImplementedError( + "MXCUBEApplication is to be used as a pure static class, dont instanciate" + ) + @staticmethod def init( server, allow_remote, ra_timeout, log_fpath, log_level, enabled_logger_list, cfg diff --git a/mxcube3/server.py b/mxcube3/server.py index 97f98f2a8..d167526ab 100644 --- a/mxcube3/server.py +++ b/mxcube3/server.py @@ -31,6 +31,11 @@ class Server: db_session = None flask_socketio = None + def __init__(self): + raise NotImplementedError( + "Server is to be used as a pure static class, dont instanciate" + ) + @staticmethod def exception_handler(e): err_msg = "Uncaught exception while calling %s" % request.path @@ -177,10 +182,7 @@ def register_routes(mxcube): init_gphl_workflow_route, mxcube, f"{url_root_prefix}/gphl_workflow" ) - Server.security = flask_security.Security( - Server.flask, - Server.user_datastore - ) + Server.security = flask_security.Security(Server.flask, Server.user_datastore) @staticmethod def emit(*args, **kwargs): @@ -201,10 +203,7 @@ def run(cfg): if ssl_context: Server.flask_socketio.run( - Server.flask, - ssl_context=ssl_context, - host="0.0.0.0", - port=8081 + Server.flask, ssl_context=ssl_context, host="0.0.0.0", port=8081 ) else: Server.flask_socketio.run(Server.flask, host="0.0.0.0", port=8081) From e92ef2c5b05a53a819fc57ff7c1a68443106d174 Mon Sep 17 00:00:00 2001 From: Marcus Oskarsson Date: Wed, 20 Sep 2023 16:58:05 +0200 Subject: [PATCH 3/3] More consistent import of Server and MXCUBEApplication --- mxcube3/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mxcube3/__init__.py b/mxcube3/__init__.py index 016174df5..b6749db36 100644 --- a/mxcube3/__init__.py +++ b/mxcube3/__init__.py @@ -10,8 +10,8 @@ monkey.patch_all(thread=False) -from mxcube3.server import Server -from mxcube3.app import MXCUBEApplication +from mxcube3.server import Server as server +from mxcube3.app import MXCUBEApplication as mxcube from mxcube3.config import Config from mxcubecore import HardwareRepository as HWR @@ -123,9 +123,9 @@ def build_server_and_config(test=False, argv=None): if test: cfg.flask.USER_DB_PATH = "/tmp/mxcube-test-user.db" - Server.init(cmdline_options, cfg, MXCUBEApplication) - MXCUBEApplication.init( - Server, + server.init(cmdline_options, cfg, mxcube) + mxcube.init( + server, cmdline_options.allow_remote, cmdline_options.ra_timeout, cmdline_options.log_file, @@ -134,12 +134,12 @@ def build_server_and_config(test=False, argv=None): cfg, ) - Server.register_routes(MXCUBEApplication) + server.register_routes(mxcube) except Exception: traceback.print_exc() raise - return (Server, cfg) + return (server, cfg) def main():