diff --git a/openflexure_microscope/api/default_extensions/zip_builder.py b/openflexure_microscope/api/default_extensions/zip_builder.py index 1b1b53d1..0ac6de82 100644 --- a/openflexure_microscope/api/default_extensions/zip_builder.py +++ b/openflexure_microscope/api/default_extensions/zip_builder.py @@ -80,7 +80,7 @@ class ZipManager: # Get array of captures from IDs capture_list = [ - microscope.camera.images.get(capture_id) for capture_id in capture_id_list + microscope.captures.images.get(capture_id) for capture_id in capture_id_list ] # Remove Nones from list (missing/invalid captures) capture_list = [capture for capture in capture_list if capture] diff --git a/openflexure_microscope/api/v2/views/captures.py b/openflexure_microscope/api/v2/views/captures.py index abe7e895..70bb03d2 100644 --- a/openflexure_microscope/api/v2/views/captures.py +++ b/openflexure_microscope/api/v2/views/captures.py @@ -100,7 +100,7 @@ class CaptureList(View): List all image captures """ microscope = find_component("org.openflexure.microscope") - image_list = microscope.camera.images.values() + image_list = microscope.captures.images.values() return image_list @@ -112,7 +112,7 @@ class CaptureView(View): Description of a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -124,7 +124,7 @@ class CaptureView(View): Delete a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -132,7 +132,7 @@ class CaptureView(View): # Delete the capture file capture_obj.delete() # Delete from capture list - del microscope.camera.images[id] + del microscope.captures.images[id] return "", 204 @@ -145,7 +145,7 @@ class CaptureDownload(View): Image data for a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -180,7 +180,7 @@ class CaptureTags(View): Get tags associated with a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -192,7 +192,7 @@ class CaptureTags(View): Add tags to a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -212,7 +212,7 @@ class CaptureTags(View): Delete tags from a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -235,7 +235,7 @@ class CaptureAnnotations(View): Get annotations associated with a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found @@ -247,7 +247,7 @@ class CaptureAnnotations(View): Update metadata for a single image capture """ microscope = find_component("org.openflexure.microscope") - capture_obj = microscope.camera.images.get(id) + capture_obj = microscope.captures.images.get(id) if not capture_obj: return abort(404) # 404 Not Found diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 8ba6ea2b..d8531a1f 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -79,12 +79,6 @@ class BaseCamera(metaclass=ABCMeta): def close(self): """Close the BaseCamera and all attached StreamObjects.""" logging.info("Closing {}".format(self)) - # Close all StreamObjects - for capture_list in [self.images.values(), self.videos.values()]: - for stream_object in capture_list: - stream_object.close() - # Empty temp directory - self.clear_tmp() # Stop worker thread self.stop_worker() logging.info("Closed {}".format(self)) diff --git a/openflexure_microscope/captures/capture_manager.py b/openflexure_microscope/captures/capture_manager.py index 0ccefc89..96599460 100644 --- a/openflexure_microscope/captures/capture_manager.py +++ b/openflexure_microscope/captures/capture_manager.py @@ -52,6 +52,23 @@ class CaptureManager: # FILE MANAGEMENT + def __enter__(self): + """Create camera on context enter.""" + return self + + def __exit__(self, exc_type, exc_value, traceback): + """Close camera stream on context exit.""" + self.close() + + def close(self): + logging.info("Closing {}".format(self)) + # Close all StreamObjects + for capture_list in [self.images.values(), self.videos.values()]: + for stream_object in capture_list: + stream_object.close() + # Empty temp directory + self.clear_tmp() + def clear_tmp(self): """ Removes all files in the temporary capture directories diff --git a/openflexure_microscope/microscope.py b/openflexure_microscope/microscope.py index f7e06d14..8a401b86 100644 --- a/openflexure_microscope/microscope.py +++ b/openflexure_microscope/microscope.py @@ -73,6 +73,7 @@ class Microscope: self.camera.close() if self.stage: self.stage.close() + self.captures.close() logging.info("Closed {}".format(self)) def setup(self, configuration):