From 88e3c7fbe363676de4cbf8019152d0332208006b Mon Sep 17 00:00:00 2001 From: fabcor Date: Wed, 23 Oct 2024 09:36:29 +0200 Subject: [PATCH] Enforce import order with `isort` --- .isort.cfg | 4 ++ .pre-commit-config.yaml | 5 ++ mxcubeweb-server | 1 - mxcubeweb/__init__.py | 8 ++- mxcubeweb/__version__.py | 6 +- mxcubeweb/app.py | 35 +++++------ mxcubeweb/config.py | 11 ++-- mxcubeweb/core/adapter/actuator_adapter.py | 7 +-- mxcubeweb/core/adapter/adapter_base.py | 18 +++--- mxcubeweb/core/adapter/beam_adapter.py | 2 +- .../core/adapter/beamline_action_adapter.py | 3 +- mxcubeweb/core/adapter/energy_adapter.py | 3 +- mxcubeweb/core/adapter/motor_adapter.py | 5 +- mxcubeweb/core/adapter/nstate_adapter.py | 4 +- mxcubeweb/core/adapter/wavelength_adapter.py | 3 +- mxcubeweb/core/components/harvester.py | 9 ++- mxcubeweb/core/components/lims.py | 12 ++-- mxcubeweb/core/components/queue.py | 22 ++++--- mxcubeweb/core/components/samplechanger.py | 11 ++-- mxcubeweb/core/components/sampleview.py | 32 ++++------ mxcubeweb/core/components/user/database.py | 7 ++- mxcubeweb/core/components/user/usermanager.py | 12 ++-- mxcubeweb/core/components/workflow.py | 4 +- mxcubeweb/core/models/adaptermodels.py | 12 +++- mxcubeweb/core/models/configmodels.py | 16 ++++- mxcubeweb/core/models/generic.py | 7 ++- mxcubeweb/core/models/usermodels.py | 24 +++++--- mxcubeweb/core/util/adapterutils.py | 42 ++++++------- mxcubeweb/core/util/fsutils.py | 1 + mxcubeweb/core/util/networkutils.py | 17 +++--- mxcubeweb/routes/beamline.py | 13 ++-- mxcubeweb/routes/detector.py | 6 +- mxcubeweb/routes/diffractometer.py | 8 ++- mxcubeweb/routes/harvester.py | 6 +- mxcubeweb/routes/lims.py | 16 ++--- mxcubeweb/routes/log.py | 10 +++- mxcubeweb/routes/login.py | 11 +++- mxcubeweb/routes/main.py | 19 +++--- mxcubeweb/routes/mockups.py | 8 ++- mxcubeweb/routes/queue.py | 12 +++- mxcubeweb/routes/ra.py | 8 +-- mxcubeweb/routes/samplecentring.py | 10 +++- mxcubeweb/routes/samplechanger.py | 8 ++- mxcubeweb/routes/signals.py | 27 ++++----- mxcubeweb/routes/workflow.py | 8 ++- mxcubeweb/server.py | 60 ++++++++----------- mxcubeweb/state_storage.py | 7 ++- test/fixture.py | 10 ++-- test/test_authn.py | 2 +- test/test_diffractometer_routes.py | 2 +- test/test_queue_routes.py | 18 +++--- 51 files changed, 339 insertions(+), 273 deletions(-) create mode 100644 .isort.cfg diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 000000000..4923de2c8 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,4 @@ +[settings] +profile=black +py_version=38 +force_grid_wrap=2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1a16da959..4e2cf330c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,6 +48,11 @@ repos: - --remove-duplicate-keys - --ignore-pass-after-docstring + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + - repo: https://github.com/psf/black rev: 22.8.0 hooks: diff --git a/mxcubeweb-server b/mxcubeweb-server index 7ab6cb08a..6dd7fa5fd 100755 --- a/mxcubeweb-server +++ b/mxcubeweb-server @@ -2,6 +2,5 @@ from mxcubeweb import main - if __name__ == "__main__": main() diff --git a/mxcubeweb/__init__.py b/mxcubeweb/__init__.py index 7103e8064..4e5093026 100644 --- a/mxcubeweb/__init__.py +++ b/mxcubeweb/__init__.py @@ -5,15 +5,17 @@ # Disabling E402 (module level import not at top of file) # for the lines below as we are monkey patching import argparse # noqa: E402 -import mock # noqa: E402 import os # noqa: E402 import sys # noqa: E402 import traceback # noqa: E402 from pathlib import Path # noqa: E402 -from mxcubeweb.server import Server as server # noqa: E402 + +import mock # noqa: E402 +from mxcubecore import HardwareRepository as HWR # noqa: E402 + from mxcubeweb.app import MXCUBEApplication as mxcube # noqa: E402 from mxcubeweb.config import Config # noqa: E402 -from mxcubecore import HardwareRepository as HWR # noqa: E402 +from mxcubeweb.server import Server as server # noqa: E402 sys.modules["Qub"] = mock.Mock() sys.modules["Qub.CTools"] = mock.Mock() diff --git a/mxcubeweb/__version__.py b/mxcubeweb/__version__.py index 6812af346..fc65ed007 100644 --- a/mxcubeweb/__version__.py +++ b/mxcubeweb/__version__.py @@ -6,10 +6,8 @@ ) except ImportError: # Python3.7 module backport - from importlib_metadata import ( - PackageNotFoundError, - version, # pyright: ignore[reportMissingImports] - ) + from importlib_metadata import version # pyright: ignore[reportMissingImports] + from importlib_metadata import PackageNotFoundError __version__: str try: diff --git a/mxcubeweb/app.py b/mxcubeweb/app.py index cc9b8c177..9142eb0e3 100644 --- a/mxcubeweb/app.py +++ b/mxcubeweb/app.py @@ -3,38 +3,37 @@ for accessing and manipulating those. """ +import json +import logging import os import sys -import logging -import traceback -import json import time - -from pathlib import Path +import traceback from logging import StreamHandler from logging.handlers import TimedRotatingFileHandler +from pathlib import Path +from mxcubecore import ColorFormatter from mxcubecore import HardwareRepository as HWR -from mxcubecore import removeLoggingHandlers, ColorFormatter -from mxcubecore import queue_entry +from mxcubecore import ( + queue_entry, + removeLoggingHandlers, +) from mxcubecore.utils.conversion import make_table -from mxcubeweb.logging_handler import MX3LoggingHandler -from mxcubeweb.core.util.adapterutils import ( - get_adapter_cls_from_hardware_object, -) from mxcubeweb.core.adapter.adapter_base import AdapterBase +from mxcubeweb.core.components.beamline import Beamline +from mxcubeweb.core.components.chat import Chat from mxcubeweb.core.components.component_base import import_component +from mxcubeweb.core.components.harvester import Harvester from mxcubeweb.core.components.lims import Lims -from mxcubeweb.core.components.chat import Chat +from mxcubeweb.core.components.queue import Queue from mxcubeweb.core.components.samplechanger import SampleChanger -from mxcubeweb.core.components.beamline import Beamline from mxcubeweb.core.components.sampleview import SampleView -from mxcubeweb.core.components.queue import Queue from mxcubeweb.core.components.workflow import Workflow from mxcubeweb.core.models.configmodels import UIComponentModel -from mxcubeweb.core.components.harvester import Harvester - +from mxcubeweb.core.util.adapterutils import get_adapter_cls_from_hardware_object +from mxcubeweb.logging_handler import MX3LoggingHandler removeLoggingHandlers() @@ -88,9 +87,7 @@ def init(app): :return: None """ - from mxcubeweb.core.adapter.beamline_adapter import ( - BeamlineAdapter, - ) + from mxcubeweb.core.adapter.beamline_adapter import BeamlineAdapter fname = os.path.dirname(__file__) HWR.add_hardware_objects_dirs([os.path.join(fname, "HardwareObjects")]) diff --git a/mxcubeweb/config.py b/mxcubeweb/config.py index 10efca17a..7cf0181c8 100644 --- a/mxcubeweb/config.py +++ b/mxcubeweb/config.py @@ -1,15 +1,18 @@ +import logging import os import sys -import logging -import ruamel.yaml -from pydantic.v1 import BaseModel, ValidationError +import ruamel.yaml +from pydantic.v1 import ( + BaseModel, + ValidationError, +) from mxcubeweb.core.models.configmodels import ( - UIPropertiesListModel, AppConfigModel, FlaskConfigModel, MXCUBEAppConfigModel, + UIPropertiesListModel, ) diff --git a/mxcubeweb/core/adapter/actuator_adapter.py b/mxcubeweb/core/adapter/actuator_adapter.py index be667ac36..b816cefa1 100644 --- a/mxcubeweb/core/adapter/actuator_adapter.py +++ b/mxcubeweb/core/adapter/actuator_adapter.py @@ -1,11 +1,10 @@ from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase -from mxcubeweb.core.util.adapterutils import export -from mxcubeweb.core.util.networkutils import RateLimited - from mxcubeweb.core.models.adaptermodels import ( - HOActuatorValueChangeModel, FloatValueModel, + HOActuatorValueChangeModel, ) +from mxcubeweb.core.util.adapterutils import export +from mxcubeweb.core.util.networkutils import RateLimited class ActuatorAdapter(ActuatorAdapterBase): diff --git a/mxcubeweb/core/adapter/adapter_base.py b/mxcubeweb/core/adapter/adapter_base.py index d5ca082b4..eb7925169 100644 --- a/mxcubeweb/core/adapter/adapter_base.py +++ b/mxcubeweb/core/adapter/adapter_base.py @@ -1,17 +1,21 @@ import inspect +import logging import traceback import typing -import logging +from typing import Any -from pydantic.v1 import ValidationError, Field, create_model import gevent +from pydantic.v1 import ( + Field, + ValidationError, + create_model, +) -from typing import Any - -from mxcubeweb.core.util.adapterutils import ( - get_adapter_cls_from_hardware_object, +from mxcubeweb.core.models.adaptermodels import ( + HOActuatorModel, + HOModel, ) -from mxcubeweb.core.models.adaptermodels import HOModel, HOActuatorModel +from mxcubeweb.core.util.adapterutils import get_adapter_cls_from_hardware_object class AdapterBase: diff --git a/mxcubeweb/core/adapter/beam_adapter.py b/mxcubeweb/core/adapter/beam_adapter.py index 4248b5311..247022b8a 100644 --- a/mxcubeweb/core/adapter/beam_adapter.py +++ b/mxcubeweb/core/adapter/beam_adapter.py @@ -1,9 +1,9 @@ from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase -from mxcubeweb.core.util.adapterutils import export from mxcubeweb.core.models.adaptermodels import ( HOBeamModel, HOBeamValueModel, ) +from mxcubeweb.core.util.adapterutils import export class BeamAdapter(ActuatorAdapterBase): diff --git a/mxcubeweb/core/adapter/beamline_action_adapter.py b/mxcubeweb/core/adapter/beamline_action_adapter.py index 7d1a940d7..f807e2195 100644 --- a/mxcubeweb/core/adapter/beamline_action_adapter.py +++ b/mxcubeweb/core/adapter/beamline_action_adapter.py @@ -1,11 +1,10 @@ import logging - from enum import Enum from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase from mxcubeweb.core.models.adaptermodels import ( - NStateModel, HOActuatorValueChangeModel, + NStateModel, StrValueModel, ) diff --git a/mxcubeweb/core/adapter/energy_adapter.py b/mxcubeweb/core/adapter/energy_adapter.py index b848d4298..aa9bb9ae0 100644 --- a/mxcubeweb/core/adapter/energy_adapter.py +++ b/mxcubeweb/core/adapter/energy_adapter.py @@ -1,6 +1,7 @@ +from mxcubecore import HardwareRepository as HWR + from mxcubeweb.core.adapter.actuator_adapter import ActuatorAdapter from mxcubeweb.core.adapter.wavelength_adapter import WavelengthAdapter -from mxcubecore import HardwareRepository as HWR class EnergyAdapter(ActuatorAdapter): diff --git a/mxcubeweb/core/adapter/motor_adapter.py b/mxcubeweb/core/adapter/motor_adapter.py index 129dfb5b7..817b4d9d2 100644 --- a/mxcubeweb/core/adapter/motor_adapter.py +++ b/mxcubeweb/core/adapter/motor_adapter.py @@ -1,10 +1,9 @@ from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase -from mxcubeweb.core.util.networkutils import RateLimited - from mxcubeweb.core.models.adaptermodels import ( - HOActuatorValueChangeModel, FloatValueModel, + HOActuatorValueChangeModel, ) +from mxcubeweb.core.util.networkutils import RateLimited class MotorAdapter(ActuatorAdapterBase): diff --git a/mxcubeweb/core/adapter/nstate_adapter.py b/mxcubeweb/core/adapter/nstate_adapter.py index cbc777c2d..fb719352f 100644 --- a/mxcubeweb/core/adapter/nstate_adapter.py +++ b/mxcubeweb/core/adapter/nstate_adapter.py @@ -1,10 +1,10 @@ -from enum import Enum import logging +from enum import Enum from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase from mxcubeweb.core.models.adaptermodels import ( - NStateModel, HOActuatorValueChangeModel, + NStateModel, StrValueModel, ) diff --git a/mxcubeweb/core/adapter/wavelength_adapter.py b/mxcubeweb/core/adapter/wavelength_adapter.py index 48660069e..224b20dc0 100644 --- a/mxcubeweb/core/adapter/wavelength_adapter.py +++ b/mxcubeweb/core/adapter/wavelength_adapter.py @@ -1,10 +1,9 @@ from mxcubeweb.core.adapter.adapter_base import ActuatorAdapterBase -from mxcubeweb.core.util.networkutils import RateLimited - from mxcubeweb.core.models.adaptermodels import ( FloatValueModel, HOActuatorValueChangeModel, ) +from mxcubeweb.core.util.networkutils import RateLimited class WavelengthAdapter(ActuatorAdapterBase): diff --git a/mxcubeweb/core/components/harvester.py b/mxcubeweb/core/components/harvester.py index 4ee9f706c..6b4ed48e0 100644 --- a/mxcubeweb/core/components/harvester.py +++ b/mxcubeweb/core/components/harvester.py @@ -1,13 +1,16 @@ # -*- coding: utf-8 -*- from __future__ import annotations + import logging from mxcubecore import HardwareRepository as HWR +from mxcubecore.HardwareObjects.abstract.sample_changer import Crims from mxcubeweb.core.components.component_base import ComponentBase -from mxcubeweb.core.components.queue import COLLECTED, UNCOLLECTED - -from mxcubecore.HardwareObjects.abstract.sample_changer import Crims +from mxcubeweb.core.components.queue import ( + COLLECTED, + UNCOLLECTED, +) # TO CONSIDER: diff --git a/mxcubeweb/core/components/lims.py b/mxcubeweb/core/components/lims.py index 511363c4d..04cb0d258 100644 --- a/mxcubeweb/core/components/lims.py +++ b/mxcubeweb/core/components/lims.py @@ -1,22 +1,20 @@ # -*- coding: utf-8 -*- -import sys -import logging import copy import io +import json +import logging import math import re -import json +import sys +from flask import session +from flask_login import current_user from mxcubecore import HardwareRepository as HWR from mxcubecore.model import queue_model_objects as qmo from mxcubeweb.core.components.component_base import ComponentBase from mxcubeweb.core.util import fsutils -from flask import session -from flask_login import current_user - - VALID_SAMPLE_NAME_REGEXP = re.compile("^[a-zA-Z0-9:+_-]+$") diff --git a/mxcubeweb/core/components/queue.py b/mxcubeweb/core/components/queue.py index 265c4fcb0..b87c3eb3c 100644 --- a/mxcubeweb/core/components/queue.py +++ b/mxcubeweb/core/components/queue.py @@ -1,27 +1,25 @@ # -*- coding: utf-8 -*- -import os -import json import itertools +import json import logging +import os import re +from functools import reduce from mock import Mock - from mxcubecore import HardwareRepository as HWR - -from mxcubecore.model import queue_model_objects as qmo -from mxcubecore.model import queue_model_enumerables as qme - from mxcubecore import queue_entry as qe -from mxcubecore.queue_entry.base_queue_entry import QUEUE_ENTRY_STATUS - from mxcubecore.HardwareObjects.Gphl import GphlQueueEntry +from mxcubecore.model import queue_model_enumerables as qme +from mxcubecore.model import queue_model_objects as qmo +from mxcubecore.queue_entry.base_queue_entry import QUEUE_ENTRY_STATUS from mxcubeweb.core.components.component_base import ComponentBase -from mxcubeweb.core.util.convertutils import str_to_camel, str_to_snake from mxcubeweb.core.models.generic import SimpleNameValue - -from functools import reduce +from mxcubeweb.core.util.convertutils import ( + str_to_camel, + str_to_snake, +) # Important: same constants as in constants.js QUEUE_PAUSED = "QueuePaused" diff --git a/mxcubeweb/core/components/samplechanger.py b/mxcubeweb/core/components/samplechanger.py index e7bf10908..02c688e1e 100644 --- a/mxcubeweb/core/components/samplechanger.py +++ b/mxcubeweb/core/components/samplechanger.py @@ -1,13 +1,16 @@ # -*- coding: utf-8 -*- import logging import time -import gevent -from mxcubecore import queue_entry +import gevent from mxcubecore import HardwareRepository as HWR +from mxcubecore import queue_entry from mxcubeweb.core.components.component_base import ComponentBase -from mxcubeweb.core.components.queue import COLLECTED, UNCOLLECTED +from mxcubeweb.core.components.queue import ( + COLLECTED, + UNCOLLECTED, +) # TO CONSIDER: @@ -414,8 +417,8 @@ def _gripper_changed(self): # Disabling C901 function is too complex (19) def queue_mount_sample(view, data_model, centring_done_cb, async_result): # noqa: C901 - from mxcubeweb.routes import signals from mxcubeweb.app import MXCUBEApplication as mxcube + from mxcubeweb.routes import signals HWR.beamline.sample_view.clear_all() logging.getLogger("user_level_log").info("Loading sample ...") diff --git a/mxcubeweb/core/components/sampleview.py b/mxcubeweb/core/components/sampleview.py index a5ab7c9a1..2b03c0b5a 100644 --- a/mxcubeweb/core/components/sampleview.py +++ b/mxcubeweb/core/components/sampleview.py @@ -1,31 +1,25 @@ # -*- coding: utf-8 -*- +import base64 +import inspect import logging -import types -import sys import os -import inspect +import sys +import types +from io import StringIO -import PIL import gevent.event - +import PIL from flask import Response - -from io import StringIO -import base64 - -from mxcubeweb.core.util.convertutils import to_camel, from_camel - -from mxcubecore.queue_entry.base_queue_entry import CENTRING_METHOD -from mxcubecore.BaseHardwareObjects import HardwareObjectState -from mxcubecore.HardwareObjects.abstract.AbstractNState import ( - AbstractNState, -) - - from mxcubecore import HardwareRepository as HWR +from mxcubecore.BaseHardwareObjects import HardwareObjectState +from mxcubecore.HardwareObjects.abstract.AbstractNState import AbstractNState +from mxcubecore.queue_entry.base_queue_entry import CENTRING_METHOD from mxcubeweb.core.components.component_base import ComponentBase - +from mxcubeweb.core.util.convertutils import ( + from_camel, + to_camel, +) SNAPSHOT_RECEIVED = gevent.event.Event() SNAPSHOT = None diff --git a/mxcubeweb/core/components/user/database.py b/mxcubeweb/core/components/user/database.py index 519eec4a2..9d5605f10 100644 --- a/mxcubeweb/core/components/user/database.py +++ b/mxcubeweb/core/components/user/database.py @@ -1,10 +1,13 @@ import datetime import typing +from flask_security import SQLAlchemySessionUserDatastore from sqlalchemy import create_engine -from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base -from flask_security import SQLAlchemySessionUserDatastore +from sqlalchemy.orm import ( + scoped_session, + sessionmaker, +) Base = declarative_base() diff --git a/mxcubeweb/core/components/user/usermanager.py b/mxcubeweb/core/components/user/usermanager.py index e04133f5a..aed0f567f 100644 --- a/mxcubeweb/core/components/user/usermanager.py +++ b/mxcubeweb/core/components/user/usermanager.py @@ -1,18 +1,20 @@ -import logging +import datetime import json +import logging import uuid -import datetime import flask import flask_security from flask_login import current_user +from mxcubecore import HardwareRepository as HWR from mxcubeweb.core.components.component_base import ComponentBase from mxcubeweb.core.models.usermodels import User -from mxcubeweb.core.util.networkutils import is_local_host, remote_addr from mxcubeweb.core.util.convertutils import convert_to_dict - -from mxcubecore import HardwareRepository as HWR +from mxcubeweb.core.util.networkutils import ( + is_local_host, + remote_addr, +) class BaseUserManager(ComponentBase): diff --git a/mxcubeweb/core/components/workflow.py b/mxcubeweb/core/components/workflow.py index dc03c5ec5..766a9ea5f 100644 --- a/mxcubeweb/core/components/workflow.py +++ b/mxcubeweb/core/components/workflow.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- import base64 -from mxcubeweb.core.components.component_base import ComponentBase - from mxcubecore import HardwareRepository as HWR +from mxcubeweb.core.components.component_base import ComponentBase + class Workflow(ComponentBase): def __init__(self, app, config): diff --git a/mxcubeweb/core/models/adaptermodels.py b/mxcubeweb/core/models/adaptermodels.py index 6bfb58152..eda0fa7a7 100644 --- a/mxcubeweb/core/models/adaptermodels.py +++ b/mxcubeweb/core/models/adaptermodels.py @@ -1,6 +1,14 @@ # -*- coding: utf-8 -*- -from typing import Tuple, List, Union -from pydantic.v1 import BaseModel, Field +from typing import ( + List, + Tuple, + Union, +) + +from pydantic.v1 import ( + BaseModel, + Field, +) class HOModel(BaseModel): diff --git a/mxcubeweb/core/models/configmodels.py b/mxcubeweb/core/models/configmodels.py index 88f02ccc3..ac3e566d5 100644 --- a/mxcubeweb/core/models/configmodels.py +++ b/mxcubeweb/core/models/configmodels.py @@ -1,8 +1,18 @@ +import datetime from enum import Enum +from typing import ( + Dict, + List, + Literal, + Optional, + Union, +) + from pydantic import model_validator -from pydantic.v1 import BaseModel, Field -from typing import List, Dict, Literal, Optional, Union -import datetime +from pydantic.v1 import ( + BaseModel, + Field, +) class FlaskConfigModel(BaseModel): diff --git a/mxcubeweb/core/models/generic.py b/mxcubeweb/core/models/generic.py index a4d683011..91fed77d2 100644 --- a/mxcubeweb/core/models/generic.py +++ b/mxcubeweb/core/models/generic.py @@ -1,5 +1,10 @@ from typing import Union -from pydantic.v1 import BaseModel, Field + +from pydantic.v1 import ( + BaseModel, + Field, +) + from mxcubeweb.core.models.configmodels import ModeEnum diff --git a/mxcubeweb/core/models/usermodels.py b/mxcubeweb/core/models/usermodels.py index dd43b1b25..98aa22f38 100644 --- a/mxcubeweb/core/models/usermodels.py +++ b/mxcubeweb/core/models/usermodels.py @@ -1,21 +1,27 @@ import pytz import tzlocal - -from mxcubeweb.core.components.user.database import Base -from flask_security import UserMixin, RoleMixin -from sqlalchemy.orm import relationship, backref +from flask_security import ( + RoleMixin, + UserMixin, +) from sqlalchemy import ( + JSON, Boolean, - Text, - Unicode, - DateTime, Column, + DateTime, + ForeignKey, Integer, String, - ForeignKey, - JSON, + Text, + Unicode, +) +from sqlalchemy.orm import ( + backref, + relationship, ) +from mxcubeweb.core.components.user.database import Base + class RolesUsers(Base): __tablename__ = "roles_users" diff --git a/mxcubeweb/core/util/adapterutils.py b/mxcubeweb/core/util/adapterutils.py index 4363f507f..a1ee7be42 100644 --- a/mxcubeweb/core/util/adapterutils.py +++ b/mxcubeweb/core/util/adapterutils.py @@ -6,42 +6,34 @@ def export(func): def get_adapter_cls_from_hardware_object(ho): + # This needs to be a direct import of DataPublisher otherwise the + # is instance check below fails due to different "import paths" It + # inly works because mxcubecore adds mxcubecore.HardwareObjects to + # sys path in __init__.py + import DataPublisher + from mxcubecore.HardwareObjects import ( + GenericDiffractometer, + MiniDiff, + ) from mxcubecore.HardwareObjects.abstract import ( AbstractActuator, + AbstractBeam, AbstractDetector, + AbstractEnergy, AbstractMachineInfo, - AbstractBeam, + AbstractMotor, AbstractNState, AbstractShutter, - AbstractEnergy, - AbstractMotor, ) - from mxcubecore.HardwareObjects import ( - MiniDiff, - GenericDiffractometer, - ) - - # This needs to be a direct import of DataPublisher otherwise the - # is instance check below fails due to different "import paths" It - # inly works because mxcubecore adds mxcubecore.HardwareObjects to - # sys path in __init__.py - import DataPublisher - from mxcubeweb.core.adapter.actuator_adapter import ActuatorAdapter - from mxcubeweb.core.adapter.motor_adapter import MotorAdapter - from mxcubeweb.core.adapter.detector_adapter import DetectorAdapter - from mxcubeweb.core.adapter.machine_info_adapter import ( - MachineInfoAdapter, - ) from mxcubeweb.core.adapter.beam_adapter import BeamAdapter - from mxcubeweb.core.adapter.data_publisher_adapter import ( - DataPublisherAdapter, - ) + from mxcubeweb.core.adapter.data_publisher_adapter import DataPublisherAdapter + from mxcubeweb.core.adapter.detector_adapter import DetectorAdapter + from mxcubeweb.core.adapter.diffractometer_adapter import DiffractometerAdapter from mxcubeweb.core.adapter.energy_adapter import EnergyAdapter - from mxcubeweb.core.adapter.diffractometer_adapter import ( - DiffractometerAdapter, - ) + from mxcubeweb.core.adapter.machine_info_adapter import MachineInfoAdapter + from mxcubeweb.core.adapter.motor_adapter import MotorAdapter from mxcubeweb.core.adapter.nstate_adapter import NStateAdapter if isinstance(ho, AbstractNState.AbstractNState) or isinstance( diff --git a/mxcubeweb/core/util/fsutils.py b/mxcubeweb/core/util/fsutils.py index 5fcadc9b3..a127451d7 100644 --- a/mxcubeweb/core/util/fsutils.py +++ b/mxcubeweb/core/util/fsutils.py @@ -1,4 +1,5 @@ import os + from scandir import scandir diff --git a/mxcubeweb/core/util/networkutils.py b/mxcubeweb/core/util/networkutils.py index 2fc10edf9..d02ac7c7e 100644 --- a/mxcubeweb/core/util/networkutils.py +++ b/mxcubeweb/core/util/networkutils.py @@ -1,19 +1,20 @@ -import socket -import functools import email.utils +import functools +import logging +import os import smtplib +import socket import time -import os -import logging - from email.mime.text import MIMEText from email.utils import make_msgid import flask -import flask_socketio import flask_security - -from flask_login import current_user, login_required +import flask_socketio +from flask_login import ( + current_user, + login_required, +) from mxcubecore import HardwareRepository as HWR diff --git a/mxcubeweb/routes/beamline.py b/mxcubeweb/routes/beamline.py index 2c3301e78..0af7b4558 100644 --- a/mxcubeweb/routes/beamline.py +++ b/mxcubeweb/routes/beamline.py @@ -1,11 +1,16 @@ -import sys import logging +import sys import typing -from werkzeug.exceptions import UnsupportedMediaType - -from flask import Blueprint, Response, jsonify, request, make_response +from flask import ( + Blueprint, + Response, + jsonify, + make_response, + request, +) from mxcubecore import HardwareRepository as HWR +from werkzeug.exceptions import UnsupportedMediaType def create_get_route(app, server, bp, adapter, attr): diff --git a/mxcubeweb/routes/detector.py b/mxcubeweb/routes/detector.py index fe9015584..edcd2bef0 100644 --- a/mxcubeweb/routes/detector.py +++ b/mxcubeweb/routes/detector.py @@ -1,4 +1,8 @@ -from flask import Blueprint, jsonify, request +from flask import ( + Blueprint, + jsonify, + request, +) def init_route(app, server, url_prefix): diff --git a/mxcubeweb/routes/diffractometer.py b/mxcubeweb/routes/diffractometer.py index fa3315a82..ea11e0e00 100644 --- a/mxcubeweb/routes/diffractometer.py +++ b/mxcubeweb/routes/diffractometer.py @@ -1,7 +1,11 @@ import json -from flask import Blueprint, Response, jsonify, request - +from flask import ( + Blueprint, + Response, + jsonify, + request, +) from mxcubecore import HardwareRepository as HWR diff --git a/mxcubeweb/routes/harvester.py b/mxcubeweb/routes/harvester.py index bd75a5027..346947de5 100644 --- a/mxcubeweb/routes/harvester.py +++ b/mxcubeweb/routes/harvester.py @@ -1,7 +1,11 @@ import json import logging -from flask import Blueprint, jsonify, request +from flask import ( + Blueprint, + jsonify, + request, +) from mxcubecore import HardwareRepository as HWR diff --git a/mxcubeweb/routes/lims.py b/mxcubeweb/routes/lims.py index 836d43a2f..47700213d 100644 --- a/mxcubeweb/routes/lims.py +++ b/mxcubeweb/routes/lims.py @@ -1,19 +1,21 @@ # -*- coding: utf-8 -*- -from subprocess import check_output -from os.path import isfile, join import logging +from os.path import ( + isfile, + join, +) +from subprocess import check_output from flask import ( Blueprint, - jsonify, Response, - send_file, - request, + jsonify, render_template, + request, + send_file, ) - -from mxcubecore.model import queue_model_objects as qmo from mxcubecore import HardwareRepository as HWR +from mxcubecore.model import queue_model_objects as qmo from . import signals diff --git a/mxcubeweb/routes/log.py b/mxcubeweb/routes/log.py index 3c2d3c545..478c7038a 100644 --- a/mxcubeweb/routes/log.py +++ b/mxcubeweb/routes/log.py @@ -1,7 +1,13 @@ -import logging import json +import logging + +from flask import ( + Blueprint, + jsonify, + make_response, + request, +) -from flask import Blueprint, jsonify, request, make_response from mxcubeweb import logging_handler diff --git a/mxcubeweb/routes/login.py b/mxcubeweb/routes/login.py index a9eca4bfe..98d9a0dfd 100644 --- a/mxcubeweb/routes/login.py +++ b/mxcubeweb/routes/login.py @@ -1,9 +1,16 @@ import logging -from flask import Blueprint, request, jsonify, make_response, session -from mxcubeweb.core.util import networkutils +from flask import ( + Blueprint, + jsonify, + make_response, + request, + session, +) from flask_login import current_user +from mxcubeweb.core.util import networkutils + def init_route(app, server, url_prefix): bp = Blueprint("login", __name__, url_prefix=url_prefix) diff --git a/mxcubeweb/routes/main.py b/mxcubeweb/routes/main.py index 63b8469f9..787f1b2b9 100644 --- a/mxcubeweb/routes/main.py +++ b/mxcubeweb/routes/main.py @@ -1,21 +1,20 @@ import logging -import traceback import time -import flask_login - +import traceback from datetime import datetime -from flask import Blueprint, jsonify, request +import flask_login +from flask import ( + Blueprint, + jsonify, + request, +) +from mxcubecore import HardwareRepository as HWR from spectree import Response from mxcubeweb import __version__ +from mxcubeweb.core.models.configmodels import UIPropertiesListModel from mxcubeweb.core.models.generic import AppSettingsModel -from mxcubeweb.core.models.configmodels import ( - UIPropertiesListModel, -) - - -from mxcubecore import HardwareRepository as HWR def init_route(app, server, url_prefix): diff --git a/mxcubeweb/routes/mockups.py b/mxcubeweb/routes/mockups.py index b131b2347..2d5c518f4 100644 --- a/mxcubeweb/routes/mockups.py +++ b/mxcubeweb/routes/mockups.py @@ -1,11 +1,13 @@ import logging -from flask import Blueprint, Response - +from flask import ( + Blueprint, + Response, +) from mxcubecore import HardwareRepository as HWR -from mxcubeweb.routes import signals from mxcubeweb.core.util.convertutils import to_camel +from mxcubeweb.routes import signals def init_route(app, server, url_prefix): diff --git a/mxcubeweb/routes/queue.py b/mxcubeweb/routes/queue.py index bcfef0071..b22c4e2bb 100644 --- a/mxcubeweb/routes/queue.py +++ b/mxcubeweb/routes/queue.py @@ -1,9 +1,15 @@ import json -import spectree - -from flask import Blueprint, Response, jsonify, request, session +import spectree +from flask import ( + Blueprint, + Response, + jsonify, + request, + session, +) from mxcubecore import HardwareRepository as HWR + from mxcubeweb.core.models.generic import SimpleNameValue diff --git a/mxcubeweb/routes/ra.py b/mxcubeweb/routes/ra.py index a1df3bdb5..f2d6bb22d 100644 --- a/mxcubeweb/routes/ra.py +++ b/mxcubeweb/routes/ra.py @@ -1,15 +1,13 @@ # -*- coding: utf-8 -*- import gevent - from flask import ( Blueprint, - jsonify, Response, - request, - make_response, copy_current_request_context, + jsonify, + make_response, + request, ) - from flask_login import current_user DISCONNECT_HANDLED = True diff --git a/mxcubeweb/routes/samplecentring.py b/mxcubeweb/routes/samplecentring.py index 3ed23094b..5e90bcae3 100644 --- a/mxcubeweb/routes/samplecentring.py +++ b/mxcubeweb/routes/samplecentring.py @@ -1,8 +1,12 @@ -import os import json +import os -from flask import Blueprint, Response, jsonify, request - +from flask import ( + Blueprint, + Response, + jsonify, + request, +) from mxcubecore import HardwareRepository as HWR diff --git a/mxcubeweb/routes/samplechanger.py b/mxcubeweb/routes/samplechanger.py index 1f3682ff0..917e6ded3 100644 --- a/mxcubeweb/routes/samplechanger.py +++ b/mxcubeweb/routes/samplechanger.py @@ -1,5 +1,9 @@ -from flask import Blueprint, Response, jsonify, request - +from flask import ( + Blueprint, + Response, + jsonify, + request, +) from mxcubecore import HardwareRepository as HWR diff --git a/mxcubeweb/routes/signals.py b/mxcubeweb/routes/signals.py index c7ca0381c..03c7f6af0 100644 --- a/mxcubeweb/routes/signals.py +++ b/mxcubeweb/routes/signals.py @@ -1,31 +1,24 @@ -import logging import json - -from mxcubeweb.server import Server as server -from mxcubeweb.app import MXCUBEApplication as mxcube +import logging from flask import Response +from mxcubecore import HardwareRepository as HWR +from mxcubecore import queue_entry as qe +from mxcubecore.HardwareObjects.abstract.AbstractSampleChanger import SampleChangerState +from mxcubecore.HardwareObjects.Harvester import HarvesterState +from mxcubecore.model import queue_model_objects as qmo -from mxcubecore.HardwareObjects.abstract.AbstractSampleChanger import ( - SampleChangerState, -) - +from mxcubeweb.app import MXCUBEApplication as mxcube from mxcubeweb.core.adapter.beamline_adapter import BeamlineAdapter from mxcubeweb.core.components.queue import ( + COLLECTED, + FAILED, READY, RUNNING, - FAILED, - COLLECTED, WARNING, ) -from mxcubecore.HardwareObjects.Harvester import HarvesterState - -from mxcubecore.model import queue_model_objects as qmo -from mxcubecore import queue_entry as qe - from mxcubeweb.core.util.networkutils import RateLimited - -from mxcubecore import HardwareRepository as HWR +from mxcubeweb.server import Server as server def last_queue_node(): diff --git a/mxcubeweb/routes/workflow.py b/mxcubeweb/routes/workflow.py index 2238ea7f9..2ff57ab92 100644 --- a/mxcubeweb/routes/workflow.py +++ b/mxcubeweb/routes/workflow.py @@ -1,7 +1,13 @@ # -*- coding: utf-8 -*- import io -from flask import Blueprint, Response, jsonify, request, send_file +from flask import ( + Blueprint, + Response, + jsonify, + request, + send_file, +) def init_route(app, server, url_prefix): diff --git a/mxcubeweb/server.py b/mxcubeweb/server.py index 0ce8b71d2..aa6c88958 100644 --- a/mxcubeweb/server.py +++ b/mxcubeweb/server.py @@ -1,26 +1,30 @@ -import traceback -import logging -import signal import atexit +import logging import os -import werkzeug +import signal +import traceback +import flask_security import gevent - -from werkzeug.middleware.proxy_fix import ProxyFix -from flask import Flask, request +import werkzeug +from flask import ( + Flask, + request, +) from flask_socketio import SocketIO - -import flask_security - from spectree import SpecTree +from werkzeug.middleware.proxy_fix import ProxyFix -from mxcubeweb.core.util import networkutils from mxcubeweb.core.components.user.database import ( - init_db, UserDatastore, + init_db, ) -from mxcubeweb.core.models.usermodels import User, Role, Message +from mxcubeweb.core.models.usermodels import ( + Message, + Role, + User, +) +from mxcubeweb.core.util import networkutils class Server: @@ -126,38 +130,24 @@ def register_routes(mxcube): Server.flask, Server.user_datastore, register_blueprint=False ) - from mxcubeweb.routes.beamline import ( - init_route as init_beamline_route, - ) - from mxcubeweb.routes.detector import ( - init_route as init_detector_route, - ) + from mxcubeweb.routes.beamline import init_route as init_beamline_route + from mxcubeweb.routes.detector import init_route as init_detector_route from mxcubeweb.routes.diffractometer import ( init_route as init_diffractometer_route, ) + from mxcubeweb.routes.harvester import init_route as init_harvester_route from mxcubeweb.routes.lims import init_route as init_lims_route from mxcubeweb.routes.log import init_route as init_log_route - from mxcubeweb.routes.login import ( - init_route as init_login_route, - ) + from mxcubeweb.routes.login import init_route as init_login_route from mxcubeweb.routes.main import init_route as init_main_route - from mxcubeweb.routes.mockups import ( - init_route as init_mockups_route, - ) - from mxcubeweb.routes.queue import ( - init_route as init_queue_route, - ) + from mxcubeweb.routes.mockups import init_route as init_mockups_route + from mxcubeweb.routes.queue import init_route as init_queue_route from mxcubeweb.routes.ra import init_route as init_ra_route - from mxcubeweb.routes.samplecentring import ( - init_route as init_sampleview_route, - ) + from mxcubeweb.routes.samplecentring import init_route as init_sampleview_route from mxcubeweb.routes.samplechanger import ( init_route as init_samplechanger_route, ) - from mxcubeweb.routes.workflow import ( - init_route as init_workflow_route, - ) - from mxcubeweb.routes.harvester import init_route as init_harvester_route + from mxcubeweb.routes.workflow import init_route as init_workflow_route url_root_prefix = "/mxcube/api/v0.1" diff --git a/mxcubeweb/state_storage.py b/mxcubeweb/state_storage.py index 0655aea17..5128d1d93 100644 --- a/mxcubeweb/state_storage.py +++ b/mxcubeweb/state_storage.py @@ -1,8 +1,9 @@ +import json + from flask_socketio import emit -from mxcubeweb.server import Server as server -from mxcubeweb.app import MXCUBEApplication as mxcube -import json +from mxcubeweb.app import MXCUBEApplication as mxcube +from mxcubeweb.server import Server as server def flush(): diff --git a/test/fixture.py b/test/fixture.py index 670c971f7..d684fc8e0 100644 --- a/test/fixture.py +++ b/test/fixture.py @@ -4,19 +4,18 @@ monkey.patch_all(thread=False) -import pytest -import sys -import os -import json import copy +import json +import os +import sys +import pytest from input_parameters import ( test_sample_1, test_sample_5, test_task, ) - MXCUBE_ROOT = os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") ) @@ -26,6 +25,7 @@ from mxcubecore import HardwareRepository + from mxcubeweb import build_server_and_config _SIO_TEST_CLIENT = None diff --git a/test/test_authn.py b/test/test_authn.py index f26727729..8eb9829f3 100644 --- a/test/test_authn.py +++ b/test/test_authn.py @@ -7,10 +7,10 @@ import os import time +import mxcubecore import pytest import mxcubeweb -import mxcubecore URL_BASE = "/mxcube/api/v0.1/login" URL_SIGNIN = f"{URL_BASE}/" # Trailing slash is necessary diff --git a/test/test_diffractometer_routes.py b/test/test_diffractometer_routes.py index 329c509b8..bded66a40 100644 --- a/test/test_diffractometer_routes.py +++ b/test/test_diffractometer_routes.py @@ -1,9 +1,9 @@ import json import random + from gevent.event import Event from mxcubecore import HardwareRepository as HWR - # Python 2 and 3 compatibility try: unicode diff --git a/test/test_queue_routes.py b/test/test_queue_routes.py index 81ecaf512..6e64dfc5e 100644 --- a/test/test_queue_routes.py +++ b/test/test_queue_routes.py @@ -1,22 +1,20 @@ -import time -import json import copy +import json +import time +from fixture import client from input_parameters import ( - test_sample_5, - test_sample_6, - test_task, - test_edit_task, - default_dc_params, default_char_acq_params, + default_dc_params, default_mesh_params, default_xrf_parameters, + test_edit_task, + test_sample_5, + test_sample_6, + test_task, ) -from fixture import client - - def test_queue_get(client): """Test if we can get the queue.""" resp = client.get("/mxcube/api/v0.1/queue/")