From 7209700343406f79bcbafcd1f6dea7f159342508 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 18 Feb 2020 11:40:14 +0000 Subject: [PATCH] Skeleton for autostorage extension --- .../api/default_extensions/__init__.py | 1 + .../api/default_extensions/autostorage.py | 37 +++++++++++++++++++ openflexure_microscope/config.py | 3 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 openflexure_microscope/api/default_extensions/autostorage.py diff --git a/openflexure_microscope/api/default_extensions/__init__.py b/openflexure_microscope/api/default_extensions/__init__.py index 2b321f34..577f10ba 100644 --- a/openflexure_microscope/api/default_extensions/__init__.py +++ b/openflexure_microscope/api/default_extensions/__init__.py @@ -4,6 +4,7 @@ import traceback from .autofocus import autofocus_extension_v2 from .scan import scan_extension_v2 from .zip_builder import zip_extension_v2 +from .autostorage import autostorage_extension_v2 # "Gracefully" handle cases where picamera cannot be imported (eg test server) try: diff --git a/openflexure_microscope/api/default_extensions/autostorage.py b/openflexure_microscope/api/default_extensions/autostorage.py new file mode 100644 index 00000000..1046e895 --- /dev/null +++ b/openflexure_microscope/api/default_extensions/autostorage.py @@ -0,0 +1,37 @@ +from labthings.server.extensions import BaseExtension +from openflexure_microscope.paths import settings_file_path +from openflexure_microscope.config import OpenflexureSettingsFile + +import logging + +AS_SETTINGS_PATH = settings_file_path("autostorage_settings.json") + + +class AutostorageExtension(BaseExtension): + def __init__(self): + BaseExtension.__init__( + self, + "org.openflexure.autostorage", + version="2.0.0-beta.1", + description="Handle switching capture storage devices", + ) + + self.settings = OpenflexureSettingsFile( + AS_SETTINGS_PATH, defaults={"preferred": None} + ) + + # We'll store a reference to a camera object, who's capture paths will be modified + self.camera = None + + # Register the on_microscope function to run when the microscope is attached + self.on_component("org.openflexure.microscope", self.on_microscope) + + def on_microscope(self, microscope_obj): + """Function to automatically call when the parent LabThing has a microscope attached.""" + logging.debug(f"Autostorage extension found microscope {microscope_obj}") + if hasattr(microscope_obj, "camera"): + self.camera = microscope_obj.camera + logging.debug(f"Autostorage extension bound to camera {self.camera}") + + +autostorage_extension_v2 = AutostorageExtension() diff --git a/openflexure_microscope/config.py b/openflexure_microscope/config.py index 2b2abff4..c0a971ba 100644 --- a/openflexure_microscope/config.py +++ b/openflexure_microscope/config.py @@ -32,7 +32,8 @@ class OpenflexureSettingsFile: self.path = path # Initialise basic config file with defaults if it doesn't exist - initialise_file(self.path, populate=defaults) + defaults_str = json.dumps(defaults, cls=JSONEncoder, indent=2, sort_keys=True) + initialise_file(self.path, populate=defaults_str) def load(self) -> dict: """