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

Fixes for instrument image display endpoints #374

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/murfey/client/analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.client.rsync import RSyncerUpdate, TransferResult
from murfey.client.tui.forms import FormDependency
from murfey.util import Observer, get_machine_config
from murfey.util import Observer, get_machine_config_client
from murfey.util.models import PreprocessingParametersTomo, ProcessingParametersSPA

logger = logging.getLogger("murfey.client.analyser")
Expand Down Expand Up @@ -73,7 +73,7 @@
self._stopping = False
self._halt_thread = False
self._murfey_config = (
get_machine_config(
get_machine_config_client(
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down Expand Up @@ -145,7 +145,7 @@
and self._environment
):
created_directories = set(
get_machine_config(
get_machine_config_client(
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand All @@ -165,7 +165,7 @@
logger.info("Acquisition software: EPU")
if self._environment:
try:
cfg = get_machine_config(
cfg = get_machine_config_client(

Check warning on line 168 in src/murfey/client/analyser.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/analyser.py#L168

Added line #L168 was not covered by tests
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand Down
4 changes: 2 additions & 2 deletions src/murfey/client/contexts/clem.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from murfey.client.context import Context
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util import capture_post, get_machine_config
from murfey.util import capture_post, get_machine_config_client

# Create logger object
logger = logging.getLogger("murfey.client.contexts.clem")
Expand All @@ -26,7 +26,7 @@
"""
Returns the Path of the transferred file on the DLS file system.
"""
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 29 in src/murfey/client/contexts/clem.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/clem.py#L29

Added line #L29 was not covered by tests
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down
8 changes: 4 additions & 4 deletions src/murfey/client/contexts/spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
authorised_requests,
capture_get,
capture_post,
get_machine_config,
get_machine_config_client,
)

logger = logging.getLogger("murfey.client.contexts.spa")
Expand Down Expand Up @@ -105,7 +105,7 @@
def _file_transferred_to(
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
):
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 108 in src/murfey/client/contexts/spa.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa.py#L108

Added line #L108 was not covered by tests
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down Expand Up @@ -684,7 +684,7 @@
if transferred_file.suffix in data_suffixes:
if self._acquisition_software == "epu":
if environment:
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 687 in src/murfey/client/contexts/spa.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa.py#L687

Added line #L687 was not covered by tests
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down Expand Up @@ -872,7 +872,7 @@
parameters = parameters or {}
environment.id_tag_registry["processing_job"].append(tag)
proc_url = f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/register_processing_job"
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 875 in src/murfey/client/contexts/spa.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa.py#L875

Added line #L875 was not covered by tests
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down
4 changes: 2 additions & 2 deletions src/murfey/client/contexts/spa_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from murfey.client.context import Context
from murfey.client.contexts.spa import _get_grid_square_atlas_positions, _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment, SampleInfo
from murfey.util import authorised_requests, capture_post, get_machine_config
from murfey.util import authorised_requests, capture_post, get_machine_config_client

logger = logging.getLogger("murfey.client.contexts.spa_metadata")

Expand All @@ -18,7 +18,7 @@
def _atlas_destination(
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
) -> Path:
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 21 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L21

Added line #L21 was not covered by tests
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down
6 changes: 3 additions & 3 deletions src/murfey/client/contexts/tomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
MurfeyInstanceEnvironment,
global_env_lock,
)
from murfey.util import authorised_requests, capture_post, get_machine_config
from murfey.util import authorised_requests, capture_post, get_machine_config_client
from murfey.util.mdoc import get_block, get_global_data, get_num_blocks

logger = logging.getLogger("murfey.client.contexts.tomo")
Expand Down Expand Up @@ -341,7 +341,7 @@ def _complete_process_file(
def _file_transferred_to(
self, environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
):
machine_config = get_machine_config(
machine_config = get_machine_config_client(
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down Expand Up @@ -794,7 +794,7 @@ def post_transfer(
if transferred_file.suffix in data_suffixes:
if self._acquisition_software == "tomo":
if environment:
machine_config = get_machine_config(
machine_config = get_machine_config_client(
str(environment.url.geturl()),
instrument_name=environment.instrument_name,
demo=environment.demo,
Expand Down
10 changes: 5 additions & 5 deletions src/murfey/client/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from murfey.client.watchdir_multigrid import MultigridDirWatcher
from murfey.util import (
capture_post,
get_machine_config,
get_machine_config_client,
read_config,
set_default_acquisition_output,
)
Expand Down Expand Up @@ -104,7 +104,7 @@
self._force_mdoc_metadata = force_mdoc_metadata
self._strict = strict
self._skip_existing_processing = skip_existing_processing
self._machine_config = get_machine_config(
self._machine_config = get_machine_config_client(

Check warning on line 107 in src/murfey/client/tui/app.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/app.py#L107

Added line #L107 was not covered by tests
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand All @@ -128,7 +128,7 @@
self, source: Path, destination_overrides: Dict[Path, str] | None = None
):
log.info(f"Launching multigrid watcher for source {source}")
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 131 in src/murfey/client/tui/app.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/app.py#L131

Added line #L131 was not covered by tests
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand Down Expand Up @@ -688,7 +688,7 @@
self.log_book.write(message.renderable)

async def reset(self):
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 691 in src/murfey/client/tui/app.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/app.py#L691

Added line #L691 was not covered by tests
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand Down Expand Up @@ -743,7 +743,7 @@
exit()

async def action_clear(self) -> None:
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 746 in src/murfey/client/tui/app.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/app.py#L746

Added line #L746 was not covered by tests
str(self._environment.url.geturl()),
instrument_name=self._environment.instrument_name,
demo=self._environment.demo,
Expand Down
14 changes: 7 additions & 7 deletions src/murfey/client/tui/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
)
from murfey.client.rsync import RSyncer
from murfey.client.tui.forms import FormDependency
from murfey.util import capture_post, get_machine_config, read_config
from murfey.util import capture_post, get_machine_config_client, read_config
from murfey.util.models import PreprocessingParametersTomo, ProcessingParametersSPA

log = logging.getLogger("murfey.tui.screens")
Expand Down Expand Up @@ -262,7 +262,7 @@
super().__init__(*args, **kwargs)
self._selected_dir = basepath
self._add_basepath = add_basepath
cfg = get_machine_config(
cfg = get_machine_config_client(

Check warning on line 265 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L265

Added line #L265 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
demo=self.app._environment.demo,
Expand Down Expand Up @@ -898,7 +898,7 @@
visit_dir = Path(str(event.button.label)) / self.app._visit
visit_dir.mkdir(exist_ok=True)
self.app._set_default_acquisition_directories(visit_dir)
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 901 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L901

Added line #L901 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
demo=self.app._environment.demo,
Expand Down Expand Up @@ -940,7 +940,7 @@
)
yield RadioButton("Tomography", value=self._context is TomographyContext)
if self.app._multigrid:
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 943 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L943

Added line #L943 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
)
Expand Down Expand Up @@ -1000,7 +1000,7 @@
)
)
else:
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 1003 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L1003

Added line #L1003 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
)
Expand Down Expand Up @@ -1031,7 +1031,7 @@
i = Input(value=val, id=k.name, classes="input-destination")
params_bulk.append(i)
self._inputs[i] = k.name
machine_config = get_machine_config(
machine_config = get_machine_config_client(

Check warning on line 1034 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L1034

Added line #L1034 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
demo=self.app._environment.demo,
Expand Down Expand Up @@ -1083,7 +1083,7 @@

def on_radio_set_changed(self, event: RadioSet.Changed) -> None:
if event.index == 0:
cfg = get_machine_config(
cfg = get_machine_config_client(

Check warning on line 1086 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L1086

Added line #L1086 was not covered by tests
str(self.app._environment.url.geturl()),
instrument_name=self.app._environment.instrument_name,
demo=self.app._environment.demo,
Expand Down
6 changes: 3 additions & 3 deletions src/murfey/instrument_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ def get_possible_gain_references(
headers={"Authorization": f"Bearer {tokens[session_id]}"},
).json()
candidates = []
for gf in secure_path(Path(machine_config["gain_reference_directory"])).glob(
"**/*"
):
for gf in secure_path(
Path(machine_config["gain_reference_directory"]), keep_spaces=True
).glob("**/*"):
if gf.is_file():
candidates.append(
File(
Expand Down
4 changes: 2 additions & 2 deletions src/murfey/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
machine_config = get_machine_config(instrument_name=instrument_name)[
d-j-hatton marked this conversation as resolved.
Show resolved Hide resolved
instrument_name
]
if machine_config.get(instrument_name):
return machine_config[instrument_name].display_name
if machine_config:
return machine_config.display_name

Check warning on line 171 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L171

Added line #L171 was not covered by tests
return ""


Expand Down
8 changes: 4 additions & 4 deletions src/murfey/server/api/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
machine_config = get_machine_config()


@router.get("/microscope_image/")
def get_mic_image():
if machine_config.image_path:
return FileResponse(machine_config.image_path)
@router.get("/instruments/{instrument_name}/image/")
def get_mic_image(instrument_name: str):
if machine_config[instrument_name].image_path:
return FileResponse(machine_config[instrument_name].image_path)

Check warning on line 19 in src/murfey/server/api/display.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/display.py#L19

Added line #L19 was not covered by tests
return None


Expand Down
2 changes: 1 addition & 1 deletion src/murfey/server/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
except Exception:
await manager.broadcast(f"Client #{client_id} sent message {data}")
except WebSocketDisconnect:
log.info(f"Disconnecting Client {int(sanitise(str(client_id)))}")
log.info(f"Disconnecting Client {sanitise(str(client_id))}")

Check warning on line 140 in src/murfey/server/websocket.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/websocket.py#L140

Added line #L140 was not covered by tests
manager.disconnect(websocket, client_id, unregister_client=False)
await manager.broadcast(f"Client #{client_id} disconnected")
await manager.delete_state(f"Client {client_id}")
Expand Down
27 changes: 17 additions & 10 deletions src/murfey/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
return config


@lru_cache(maxsize=1)
def get_machine_config_client(
url: str, instrument_name: str = "", demo: bool = False
) -> dict:
_instrument_name: str | None = instrument_name or os.getenv("BEAMLINE")
if not _instrument_name:
return {}
return requests.get(f"{url}/instruments/{_instrument_name}/machine").json()

Check warning on line 50 in src/murfey/util/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/__init__.py#L50

Added line #L50 was not covered by tests


def authorised_requests() -> Tuple[Callable, Callable, Callable, Callable]:
token = read_config()["Murfey"].get("token", "")
_get = partial(requests.get, headers={"Authorization": f"Bearer {token}"})
Expand All @@ -62,19 +72,16 @@
return in_string


def secure_path(in_path: Path) -> Path:
secured_parts = [secure_filename(p) for p in in_path.parts]
def secure_path(in_path: Path, keep_spaces: bool = False) -> Path:
if keep_spaces:
secured_parts = [

Check warning on line 77 in src/murfey/util/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/__init__.py#L77

Added line #L77 was not covered by tests
secure_filename(p) if " " not in p else p for p in in_path.parts
]
else:
secured_parts = [secure_filename(p) for p in in_path.parts]

Check warning on line 81 in src/murfey/util/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/__init__.py#L81

Added line #L81 was not covered by tests
return Path("/".join(secured_parts))


@lru_cache(maxsize=1)
def get_machine_config(url: str, instrument_name: str = "", demo: bool = False) -> dict:
_instrument_name: str | None = instrument_name or os.getenv("BEAMLINE")
if not _instrument_name:
return {}
return requests.get(f"{url}/instruments/{_instrument_name}/machine").json()


def _get_visit_list(api_base: ParseResult, instrument_name: str):
get_visits_url = api_base._replace(
path=f"/instruments/{instrument_name}/visits_raw"
Expand Down