From 141ccc06c19aee502afb0c151fabc25c3c3e1e0d Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Fri, 19 Dec 2025 16:46:30 +0000 Subject: [PATCH] Some minor type fixes --- src/openflexure_microscope_server/logging.py | 5 +++-- src/openflexure_microscope_server/server/legacy_api.py | 7 ++++++- .../things/camera/__init__.py | 6 +++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/openflexure_microscope_server/logging.py b/src/openflexure_microscope_server/logging.py index 5007c8f8..a32061b7 100644 --- a/src/openflexure_microscope_server/logging.py +++ b/src/openflexure_microscope_server/logging.py @@ -14,12 +14,13 @@ output to STDOUT/STDERR are captured by ``systemd``. These can be viewed by usin import logging import os from logging.handlers import RotatingFileHandler +from typing import Optional from fastapi import HTTPException from fastapi.responses import PlainTextResponse LOGGER = logging.getLogger(__name__) -OFM_LOG_FILE = None +OFM_LOG_FILE: Optional[str] = None def configure_logging(log_folder: str) -> None: @@ -128,7 +129,7 @@ class OFMHandler(logging.Handler): how many can be returned over HTTP. """ super().__init__(level=level) - self._log = [] + self._log: list[str] = [] self._max_logs = max_logs def append_record(self, record: logging.LogRecord) -> None: diff --git a/src/openflexure_microscope_server/server/legacy_api.py b/src/openflexure_microscope_server/server/legacy_api.py index 7d0e7265..69996f52 100644 --- a/src/openflexure_microscope_server/server/legacy_api.py +++ b/src/openflexure_microscope_server/server/legacy_api.py @@ -6,6 +6,8 @@ from fastapi import Response import labthings_fastapi as lt +from openflexure_microscope_server.things.camera import BaseCamera + FAKE_ROUTES = [ "/api/v2/", "/api/v2/streams/snapshot", @@ -38,7 +40,10 @@ def add_v2_endpoints(thing_server: lt.ThingServer) -> None: @app.head("/api/v2/streams/snapshot") async def thumbnail() -> JPEGResponse: """Return a low-resolution snapshot, for compatibility with OF connect.""" - blob = await thing_server.things["camera"].lores_mjpeg_stream.grab_frame() + camera_thing = thing_server.things["camera"] + if not isinstance(camera_thing, BaseCamera): + raise RuntimeError("Camera thing is not a BaseCamera") + blob = await camera_thing.lores_mjpeg_stream.grab_frame() return JPEGResponse(blob) @app.get("/api/v2/instrument/settings/name") diff --git a/src/openflexure_microscope_server/things/camera/__init__.py b/src/openflexure_microscope_server/things/camera/__init__.py index 455459cf..7592de1d 100644 --- a/src/openflexure_microscope_server/things/camera/__init__.py +++ b/src/openflexure_microscope_server/things/camera/__init__.py @@ -272,7 +272,7 @@ class BaseCamera(lt.Thing): @lt.action def capture_jpeg( self, - stream_name: str = "main", + stream_name: Literal["main", "lores", "full"] = "main", wait: Optional[float] = None, ) -> JPEGBlob: """Acquire one image from the camera as a JPEG. @@ -526,7 +526,7 @@ class BaseCamera(lt.Thing): nothing is returned on success """ if save_resolution is not None and image.size != save_resolution: - image = image.resize(save_resolution, Image.BOX) + image = image.resize(save_resolution, Image.Resampling.BOX) try: # Per PIL documentation, # (https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg) @@ -537,7 +537,7 @@ class BaseCamera(lt.Thing): # disabled, file size increases and quality is barely or not affected image.save(jpeg_path, quality=95, subsampling=0) try: - self._add_metadata_to_capture(jpeg_path, metadata) + self._add_metadata_to_capture(jpeg_path, dict(metadata)) except Exception: # We need to capture any exception as there are many reasons metadata # might not be added. We warn rather than log the error.