Create and mount application level data direcory rather than just a scan directory

This commit is contained in:
Julian Stirling 2026-02-22 19:29:33 +00:00
parent 2c2a52cb97
commit 45903f92a6
12 changed files with 187 additions and 166 deletions

View file

@ -11,7 +11,8 @@ from pathlib import Path
from typing import Any, Callable, Optional
import uvicorn
from fastapi.middleware.cors import CORSMiddleware # vue3 migration
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from uvicorn.main import Server
import labthings_fastapi as lt
@ -35,6 +36,15 @@ DEVELOPER_MODE = os.getenv("OFM_SERVER_DEV_MODE", "false").lower() == "true"
_TEMPLATE_PATH = Path(__file__).with_name("fallback.html.jinja")
class OFMApplicationData(BaseModel):
"""Application data for the OpenFlexure Microscope."""
log_folder: str
"""The directory to save the logs in."""
data_folder: str
"""The directory for Things to save data in."""
def set_shutdown_function(shutdown_function: Callable[[], None]) -> None:
"""Ensure a function is called before the shutdown.
@ -62,10 +72,10 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None:
def customise_server(
server: lt.ThingServer, log_folder: str, scans_folder: Optional[str]
server: lt.ThingServer, application_config: OFMApplicationData
) -> None:
"""Customise the server with additional endpoints, etc."""
configure_logging(log_folder)
configure_logging(application_config.log_folder)
if DEVELOPER_MODE:
# Allow CORS in developer mode for easier testing with the webapp
@ -78,27 +88,13 @@ def customise_server(
)
add_v2_endpoints(server)
add_static_files(server.app, scans_folder)
add_static_files(server.app, application_config.data_folder)
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
server.app.get("/log/")(retrieve_log)
server.app.get("/logfile/")(retrieve_log_from_file)
def _get_scans_dir(config: dict) -> Optional[str]:
"""Read the config and return the scans directory.
Return is None if there is no smart_scan thing loaded.
"""
if "smart_scan" in config["things"]:
try:
return config["things"]["smart_scan"]["kwargs"]["scans_folder"]
except KeyError as e:
msg = "Configuration error: smart scan should have scans_folder kwarg set"
raise RuntimeError(msg) from e
return None
def serve_from_cli(argv: Optional[list[str]] = None) -> None:
"""Start the server from the command line."""
args = lt.cli.parse_args(argv)
@ -112,12 +108,10 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None:
lt_config = None
server = None
try:
lt_config, internal_config = _full_config_from_args(args)
lt_config, application_config = _full_config_from_args(args)
server = lt.ThingServer.from_config(lt_config)
customise_server(
server, internal_config["log_folder"], internal_config["scans_folder"]
)
customise_server(server, application_config)
def shutdown_call() -> None:
try:
@ -172,7 +166,9 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None:
raise e
def _full_config_from_args(args: Namespace) -> tuple[ThingServerConfig, dict[str, Any]]:
def _full_config_from_args(
args: Namespace,
) -> tuple[ThingServerConfig, OFMApplicationData]:
"""Load configuration from LabThings args allowing patching.
This returns the labthings ThingServerConfig model and a dictionary of the config
@ -181,16 +177,14 @@ def _full_config_from_args(args: Namespace) -> tuple[ThingServerConfig, dict[str
This provides similar functionarlity to lt.cli.config_from_args except allows the
configuration file to specify a base config, and optionally patches.
"""
internal_config = {"log_folder": "./openflexure/logs", "scans_folder": None}
# If no config file specified let LabThings handle it.
# Don't allow configuration to be set as an argument as then we cannot handle
# application_config
if not args.config:
return lt.cli.config_from_args(args), internal_config
raise RuntimeError(
"OpenFlexure Microscope Server must have a configuration file specified."
)
patched_config = load_patched_config(args.config)
log_folder = patched_config.pop("log_folder", None)
if log_folder is not None:
internal_config["log_folder"] = log_folder
scans_folder = _get_scans_dir(patched_config)
if scans_folder is not None:
internal_config["scans_folder"] = scans_folder
return ThingServerConfig(**patched_config), internal_config
application_config = OFMApplicationData(**patched_config.pop("application_config"))
return ThingServerConfig(**patched_config), application_config

View file

@ -1,7 +1,6 @@
"""Add endpoints for static files to the underlying FastAPI server."""
import os
from typing import Optional
from fastapi import FastAPI
from fastapi.responses import FileResponse, RedirectResponse
@ -36,7 +35,7 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
)
def add_static_files(app: FastAPI, scans_folder: Optional[str]) -> None:
def add_static_files(app: FastAPI, data_folder: str) -> None:
"""Add the static files responsible for the webapp app to the FastAPI app.
Note that any file in the root of the static dir will not be cached. However, the
@ -45,7 +44,7 @@ def add_static_files(app: FastAPI, scans_folder: Optional[str]) -> None:
important file not to cache is "index.html".
:param app: The FastAPI app to add to, in this case the OpenFlexure server
:param scans_folder: The directory for the scans.
:param data_folder: The directory for any data.
"""
check_static_dir()
@ -65,16 +64,17 @@ def add_static_files(app: FastAPI, scans_folder: Optional[str]) -> None:
name=f"static_{fname}",
)
# If scans folder is None, there is not smart scan thing. So nothing to mount.
if scans_folder is not None:
# Mount the scan directory to .../scans/, to allow dzi viewing
if not os.path.isdir(scans_folder):
os.makedirs(scans_folder)
app.mount(
"/scans/",
StaticFiles(directory=scans_folder),
name="scans",
)
# We need a data folder
if data_folder is None:
raise ValueError("No data folder is set, cannot start server")
# Mount the scan directory to .../data/, to allow dzi viewing
if not os.path.isdir(data_folder):
os.makedirs(data_folder)
app.mount(
"/data/",
StaticFiles(directory=data_folder),
name="data",
)
def check_static_dir() -> None:

View file

@ -3,3 +3,59 @@
The microscope can be extended to be used with other hardware by creating a package
with other Things and including them in the LabThings-FastAPI config file.
"""
import posixpath
from typing import Optional, Self
from starlette.routing import Mount
from starlette.staticfiles import StaticFiles
import labthings_fastapi as lt
class OFMThing(lt.Thing):
"""A custom LabThings Thing class for the OpenFlexure Microscope."""
_data_dir: Optional[str] = None
def __enter__(self) -> Self:
"""Set the data directory when the Thing is entered."""
self._data_dir = get_data_directory_from_server(self)
return self
@property
def data_dir(self) -> str:
"""The data directory for this thing."""
if self._data_dir is None:
raise RuntimeError(
"No data directory set. Has the LabThings server been started?"
)
return self._data_dir
def get_data_directory_from_server(thing: lt.Thing) -> str:
"""Get the data directory from the server.
:param thing: The Thing to get the data directory for.
:return: The data directory as a string:
:raise RuntimeError: If not able to get the data directory for any reason.
"""
server = thing._thing_server_interface._server()
if server is None:
raise RuntimeError("No server found to communicate with.")
routes = server.app.routes
try:
route_paths = [route.path if hasattr(route, "path") else "" for route in routes]
data_index = route_paths.index("/data")
except ValueError as e:
raise RuntimeError("Could not find data directory") from e
mount = routes[data_index]
if not isinstance(mount, Mount):
raise RuntimeError("Data directory isn't a starlette.routing.Mount.")
if not isinstance(mount.app, StaticFiles):
raise RuntimeError("Data is not mounted as static files.")
app_data_dir = mount.app.directory
if app_data_dir is None:
raise RuntimeError("Data directory is not set.")
return posixpath.join(str(app_data_dir), thing.path.strip("/"))

View file

@ -32,6 +32,7 @@ from pydantic import BaseModel, PlainSerializer
import labthings_fastapi as lt
from openflexure_microscope_server import scan_directories, stitching
from openflexure_microscope_server.things import OFMThing
from openflexure_microscope_server.utilities import coerce_thing_selector
# Things
@ -120,7 +121,7 @@ def _scan_running(
return scan_running_wrapper
class SmartScanThing(lt.Thing):
class SmartScanThing(OFMThing):
"""A Thing for scanning samples and interacting with past scans.
SmartScanThing exposes all functionality for automatically scanning samples,
@ -135,23 +136,22 @@ class SmartScanThing(lt.Thing):
def __init__(
self,
thing_server_interface: lt.ThingServerInterface,
scans_folder: str,
default_workflow: str,
) -> None:
"""Initialise a SmartScanThing saving to and loading from the input directory.
:param scans_folder: This is the path to the directory where all scans will be
saved. Any scans already in this directory will be accessible through the
HTTP interface.
:param default_workflow: The default workflows that smart scan uses if nothing
is set in settings.
"""
super().__init__(thing_server_interface)
self._scan_dir_manager = scan_directories.ScanDirectoryManager(scans_folder)
self._scan_lock = threading.Lock()
self._default_workflow = default_workflow
self._workflow_name = default_workflow
def __enter__(self) -> Self:
"""Open hardware connection when the Thing context manager is opened."""
super().__enter__()
self._scan_dir_manager = scan_directories.ScanDirectoryManager(self.data_dir)
valid_name = coerce_thing_selector(
thing_mapping=self._all_workflows,
selected=self.workflow_name,