Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MXCUBEApplication and Server static classes #1050

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions mxcube3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@


from gevent import monkey

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

sys.modules["Qub"] = mock.Mock()
sys.modules["Qub.CTools"] = mock.Mock()

mxcube = None
server = None


def parse_args(argv):
XML_DIR = os.path.join(
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion mxcube3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -298,7 +303,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
Expand Down
1 change: 0 additions & 1 deletion mxcube3/core/components/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions mxcube3/core/components/samplechanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}
Expand All @@ -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 ...")
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion mxcube3/core/components/sampleview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mxcube3/routes/signals.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 7 additions & 8 deletions mxcube3/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
4 changes: 2 additions & 2 deletions mxcube3/state_storage.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading