Commit caf553db authored by Joel Collins's avatar Joel Collins
Browse files

Moved capture management into separate object

parent 1a6088a8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ logger.setLevel(logging.INFO)
# Log server paths being used
logging.info(f"Running with data path {OPENFLEXURE_VAR_PATH}")

print("Creating app")
# Create flask app
app, labthing = create_app(
    __name__,
@@ -175,5 +176,6 @@ atexit.register(cleanup)
if __name__ == "__main__":
    from labthings.server.wsgi import Server

    print("Starting OpenFlexure Microscope Server...")
    server = Server(app)
    server.run(host="::", port=5000, debug=False, zeroconf=True)
+15 −15
Original line number Diff line number Diff line
@@ -6,8 +6,8 @@ from labthings.server.find import find_component

from openflexure_microscope.paths import settings_file_path, check_rw
from openflexure_microscope.config import OpenflexureSettingsFile
from openflexure_microscope.camera.base import BASE_CAPTURE_PATH
from openflexure_microscope.camera.capture import build_captures_from_exif
from openflexure_microscope.captures.capture_manager import BASE_CAPTURE_PATH
from openflexure_microscope.captures.capture import build_captures_from_exif

from openflexure_microscope.api.utilities.gui import build_gui

@@ -37,17 +37,17 @@ def get_permissive_locations():
    ]


def get_current_location(camera):
    return camera.paths.get("default")
def get_current_location(capture_manager):
    return capture_manager.paths.get("default")


def set_current_location(camera, location: str):
def set_current_location(capture_manager, location: str):
    if not os.path.isdir(location):
        os.makedirs(location)
    logging.debug("Updating location...")
    camera.paths.update({"default": location})
    capture_manager.paths.update({"default": location})
    logging.debug("Rebuilding captures...")
    camera.rebuild_captures()
    capture_manager.rebuild_captures()
    logging.debug("Capture location changed successfully.")


@@ -93,7 +93,7 @@ class AutostorageExtension(BaseExtension):
        )

        # We'll store a reference to a camera object, who's capture paths will be modified
        self.camera = None
        self.capture_manager = None

        self.initial_location = get_default_location()

@@ -107,9 +107,9 @@ class AutostorageExtension(BaseExtension):
            logging.debug(f"Autostorage extension bound to camera {self.camera}")

            # Store a reference to the camera
            self.camera = microscope_obj.camera
            self.capture_manager = microscope_obj.captures
            # Store the initial storage location
            self.initial_location = get_current_location(self.camera)
            self.initial_location = get_current_location(self.capture_manager)

            # If preferred path does not exist, or cannot be written to
            self.check_location(self.initial_location)
@@ -118,20 +118,20 @@ class AutostorageExtension(BaseExtension):

    def check_location(self, location=None):
        if not location:
            location = get_current_location(self.camera)
            location = get_current_location(self.capture_manager)
        # If preferred path does not exist, or cannot be written to
        if not (os.path.isdir(location) and check_rw(location)):
            logging.error(
                f"Preferred capture path {location} is missing or cannot be written to. Restoring defaults."
            )
            # Reset the storage location to default
            set_current_location(self.camera, get_default_location())
            set_current_location(self.capture_manager, get_default_location())

    def get_locations(self):
        if self.camera:
            locations = get_all_locations()

            current_location = get_current_location(self.camera)
            current_location = get_current_location(self.capture_manager)
            if current_location not in locations.values():
                locations.update({"Custom": current_location})
            # Add location from the cameras settings file
@@ -140,7 +140,7 @@ class AutostorageExtension(BaseExtension):
            return {}

    def get_preferred_key(self):
        current = get_current_location(self.camera)
        current = get_current_location(self.capture_manager)
        locations = self.get_locations()

        matches = [k for k, v in locations.items() if v == current]
@@ -157,7 +157,7 @@ class AutostorageExtension(BaseExtension):
            raise KeyError(f"No location named {new_path_key}")

        location = self.get_locations().get(new_path_key)
        set_current_location(self.camera, location)
        set_current_location(self.capture_manager, location)

    def key_to_title(self, path_key: str):
        if not path_key in self.get_locations().keys():
+9 −19
Original line number Diff line number Diff line
@@ -85,27 +85,17 @@ def capture(
    filename = "{}_{}_{}_{}".format(basename, *microscope.stage.position)
    folder = "SCAN_{}".format(basename)

    # Create output object
    output = microscope.camera.new_image(
        temporary=temporary, filename=filename, folder=folder
    )

    # Capture
    microscope.camera.capture(
        output.file, use_video_port=use_video_port, resize=resize, bayer=bayer
    # Do capture
    return microscope.capture(
        filename=filename,
        folder=folder,
        temporary=temporary,
        use_video_port=use_video_port, resize=resize, bayer=bayer,
        annotations=annotations,
        tags=tags,
        metadata=metadata
    )

    # Inject system metadata
    output.put_metadata({"instrument": microscope.metadata})

    # Insert custom metadata
    output.put_metadata(metadata)

    # Insert custom metadata
    output.put_annotations(annotations)
    # Insert custom tags
    output.put_tags(tags)


### Scanning

+0 −2
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@ from labthings.core.utilities import path_relative_to

from openflexure_microscope.paths import settings_file_path, check_rw
from openflexure_microscope.config import OpenflexureSettingsFile
from openflexure_microscope.camera.base import BASE_CAPTURE_PATH
from openflexure_microscope.camera.capture import build_captures_from_exif

from openflexure_microscope.api.utilities.gui import build_gui

+1 −1
Original line number Diff line number Diff line
@@ -7,6 +7,6 @@ default_microscope = Microscope()

# Restore loaded capture array to camera object
logging.debug("Restoring captures...")
default_microscope.camera.rebuild_captures()
default_microscope.captures.rebuild_captures()

logging.debug("Microscope successfully attached!")
Loading