Temp capture folder now handled by camera
This commit is contained in:
parent
a1965f606c
commit
7cf54240be
3 changed files with 21 additions and 39 deletions
|
|
@ -14,7 +14,7 @@ except ImportError:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from _thread import get_ident
|
from _thread import get_ident
|
||||||
|
|
||||||
from .capture import CaptureObject, capture_from_dict, BASE_CAPTURE_PATH
|
from .capture import CaptureObject, capture_from_dict, BASE_CAPTURE_PATH, TEMP_CAPTURE_PATH
|
||||||
from openflexure_microscope.config import USER_CONFIG_DIR
|
from openflexure_microscope.config import USER_CONFIG_DIR
|
||||||
from openflexure_microscope.utilities import entry_by_id
|
from openflexure_microscope.utilities import entry_by_id
|
||||||
from openflexure_microscope.lock import StrictLock
|
from openflexure_microscope.lock import StrictLock
|
||||||
|
|
@ -111,6 +111,8 @@ class BaseCamera(object):
|
||||||
self.paths = {
|
self.paths = {
|
||||||
'image': BASE_CAPTURE_PATH,
|
'image': BASE_CAPTURE_PATH,
|
||||||
'video': BASE_CAPTURE_PATH,
|
'video': BASE_CAPTURE_PATH,
|
||||||
|
'image_tmp': TEMP_CAPTURE_PATH,
|
||||||
|
'video_tpm': TEMP_CAPTURE_PATH
|
||||||
} #: dict: Dictionary of capture paths
|
} #: dict: Dictionary of capture paths
|
||||||
|
|
||||||
# Capture data
|
# Capture data
|
||||||
|
|
@ -284,7 +286,7 @@ class BaseCamera(object):
|
||||||
write_to_file: bool = False,
|
write_to_file: bool = False,
|
||||||
temporary: bool = True,
|
temporary: bool = True,
|
||||||
filename: str = None,
|
filename: str = None,
|
||||||
folder: str = None,
|
folder: str = "",
|
||||||
fmt: str = 'jpeg'):
|
fmt: str = 'jpeg'):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
@ -303,10 +305,8 @@ class BaseCamera(object):
|
||||||
logging.debug(filename)
|
logging.debug(filename)
|
||||||
|
|
||||||
# Generate folder
|
# Generate folder
|
||||||
if folder:
|
base_folder = self.paths['image_tmp'] if temporary else self.paths['image']
|
||||||
folder = os.path.join(self.paths['image'], folder)
|
folder = os.path.join(base_folder, folder)
|
||||||
else:
|
|
||||||
folder = self.paths['image']
|
|
||||||
|
|
||||||
# Create capture object
|
# Create capture object
|
||||||
output = CaptureObject(
|
output = CaptureObject(
|
||||||
|
|
@ -349,10 +349,8 @@ class BaseCamera(object):
|
||||||
logging.debug(filename)
|
logging.debug(filename)
|
||||||
|
|
||||||
# Generate folder
|
# Generate folder
|
||||||
if folder:
|
base_folder = self.paths['video_tmp'] if temporary else self.paths['video']
|
||||||
folder = os.path.join(self.paths['video'], folder)
|
folder = os.path.join(base_folder, folder)
|
||||||
else:
|
|
||||||
folder = self.paths['video']
|
|
||||||
|
|
||||||
# Create capture object
|
# Create capture object
|
||||||
output = CaptureObject(
|
output = CaptureObject(
|
||||||
|
|
|
||||||
|
|
@ -81,8 +81,8 @@ def capture_from_dict(capture_dict):
|
||||||
|
|
||||||
# Get inherent capture information from database
|
# Get inherent capture information from database
|
||||||
capture.file = capture_dict['path']
|
capture.file = capture_dict['path']
|
||||||
capture.split_file_path(capture.file)
|
|
||||||
capture.temporary = capture_dict['temporary']
|
capture.temporary = capture_dict['temporary']
|
||||||
|
capture.split_file_path(capture.file)
|
||||||
|
|
||||||
if capture.format.upper() in EXIF_FORMATS:
|
if capture.format.upper() in EXIF_FORMATS:
|
||||||
md_exif = pull_usercomment_dict(capture.file)
|
md_exif = pull_usercomment_dict(capture.file)
|
||||||
|
|
@ -137,9 +137,7 @@ class CaptureObject(object):
|
||||||
filename = self.id
|
filename = self.id
|
||||||
self.filename = "{}.{}".format(filename, fmt)
|
self.filename = "{}.{}".format(filename, fmt)
|
||||||
|
|
||||||
# Create folder path. Default to BASE_CAPTURE_PATH
|
# Create folder path
|
||||||
if not folder:
|
|
||||||
folder = BASE_CAPTURE_PATH
|
|
||||||
self.folder = folder
|
self.folder = folder
|
||||||
|
|
||||||
# Dictionary for storing custom metadata
|
# Dictionary for storing custom metadata
|
||||||
|
|
@ -185,7 +183,7 @@ class CaptureObject(object):
|
||||||
file on disk.
|
file on disk.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.build_file_path(self.filename, self.folder)
|
self.build_file_path()
|
||||||
|
|
||||||
# Byte bytestream properties
|
# Byte bytestream properties
|
||||||
self.bytestream = io.BytesIO() # Byte bytestream that data will be written to
|
self.bytestream = io.BytesIO() # Byte bytestream that data will be written to
|
||||||
|
|
@ -201,31 +199,23 @@ class CaptureObject(object):
|
||||||
# Save initial metadata file
|
# Save initial metadata file
|
||||||
self.save_metadata()
|
self.save_metadata()
|
||||||
|
|
||||||
def build_file_path(
|
def build_file_path(self):
|
||||||
self,
|
|
||||||
filename: str,
|
|
||||||
folder: str):
|
|
||||||
"""
|
"""
|
||||||
Construct a full file path, based on filename, folder, and file format.
|
Construct a full file path, based on filename, folder, and file format.
|
||||||
Defaults to UUID.
|
Defaults to UUID.
|
||||||
|
|
||||||
Args:
|
|
||||||
filename (str): Filename of capture
|
|
||||||
folder (str): Directory on disk to store capture file
|
|
||||||
"""
|
"""
|
||||||
global TEMP_CAPTURE_PATH
|
global TEMP_CAPTURE_PATH
|
||||||
|
# TODO: Combine this and split_file_path, and tidy. Let the device (camera) handle folders. This should be more basic.
|
||||||
|
# TODO: Even let the base camera manage moving captures to temp folder
|
||||||
|
# This module will clear out the temp folder, but won't MOVE anything there.
|
||||||
|
# In Base cameras new_image method, the full folder is constructed. Temp folder should be inserted there.
|
||||||
|
|
||||||
if self.temporary:
|
self.file = os.path.join(self.folder, self.filename) # Full file name by joining given folder to given name
|
||||||
# Store original file path
|
|
||||||
self.file_notmp = os.path.join(folder, filename)
|
|
||||||
# Move user-specified folder to TEMP
|
|
||||||
self.folder = os.path.join(TEMP_CAPTURE_PATH, self.folder)
|
|
||||||
|
|
||||||
self.file = os.path.join(folder, filename) # Full file name by joining given folder to given name
|
|
||||||
|
|
||||||
self.split_file_path(self.file) # Split file path into folder, filename, and basename
|
self.split_file_path(self.file) # Split file path into folder, filename, and basename
|
||||||
|
|
||||||
# Check directory is a subdirectory of BASE_CAPTURE_PATH
|
# Check directory is a subdirectory of BASE_CAPTURE_PATH
|
||||||
|
# TODO: Do we need this?
|
||||||
if not os.path.commonprefix([self.file, BASE_CAPTURE_PATH]) == BASE_CAPTURE_PATH:
|
if not os.path.commonprefix([self.file, BASE_CAPTURE_PATH]) == BASE_CAPTURE_PATH:
|
||||||
raise Exception("Captures cannot be stored in a lower-level directory than {}.".format(BASE_CAPTURE_PATH))
|
raise Exception("Captures cannot be stored in a lower-level directory than {}.".format(BASE_CAPTURE_PATH))
|
||||||
|
|
||||||
|
|
@ -448,13 +438,6 @@ class CaptureObject(object):
|
||||||
|
|
||||||
def save_file(self) -> bool:
|
def save_file(self) -> bool:
|
||||||
"""Write the StreamObjects bytestream to a file."""
|
"""Write the StreamObjects bytestream to a file."""
|
||||||
if self.temporary: # If capture is currently temporary
|
|
||||||
self.load_file() # Load data from tmp file into bytestream, if tmp file exists
|
|
||||||
self.temporary = False # Flag as kept on disk
|
|
||||||
self.file = self.file_notmp # Reset file path to non-temporary path
|
|
||||||
self.split_file_path(self.file) # Set split properties based on new path
|
|
||||||
logging.info("Moved temporary file out to {}".format(self.file))
|
|
||||||
|
|
||||||
if self.stream_exists: # If there's a bytestream to save
|
if self.stream_exists: # If there's a bytestream to save
|
||||||
with open(self.file, 'ab') as f: # Load file as bytes
|
with open(self.file, 'ab') as f: # Load file as bytes
|
||||||
logging.debug("Writing bytestream to file {}".format(self.file))
|
logging.debug("Writing bytestream to file {}".format(self.file))
|
||||||
|
|
@ -504,6 +487,7 @@ class CaptureObject(object):
|
||||||
"""Both clear the bytestream, and delete any associated on-disk data."""
|
"""Both clear the bytestream, and delete any associated on-disk data."""
|
||||||
logging.info("Closing {}".format(self.id))
|
logging.info("Closing {}".format(self.id))
|
||||||
self.delete_stream()
|
self.delete_stream()
|
||||||
|
# Delete the file from disk if temporary
|
||||||
if self.temporary:
|
if self.temporary:
|
||||||
self.delete()
|
self.delete()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,14 +62,14 @@ class ScanPlugin(MicroscopePlugin):
|
||||||
|
|
||||||
# Construct a tile filename
|
# Construct a tile filename
|
||||||
filename = "{}_{}_{}_{}".format(basename, *self.microscope.stage.position)
|
filename = "{}_{}_{}_{}".format(basename, *self.microscope.stage.position)
|
||||||
foldername = "SCAN_{}".format(basename)
|
folder = "SCAN_{}".format(basename)
|
||||||
|
|
||||||
# Create output object
|
# Create output object
|
||||||
output = self.microscope.camera.new_image(
|
output = self.microscope.camera.new_image(
|
||||||
write_to_file=True,
|
write_to_file=True,
|
||||||
temporary=temporary,
|
temporary=temporary,
|
||||||
filename=filename,
|
filename=filename,
|
||||||
folder=foldername)
|
folder=folder)
|
||||||
|
|
||||||
# Capture
|
# Capture
|
||||||
self.microscope.camera.capture(
|
self.microscope.camera.capture(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue