Gallery can delete all data.

This commit is contained in:
Julian Stirling 2026-06-21 18:24:57 +01:00
parent bef63769df
commit cc6caa39d7
6 changed files with 85 additions and 58 deletions

View file

@ -298,8 +298,8 @@ class BaseCamera(OFMThing, ABC):
"""The schema (BaseModel) for passing data to the gallery."""
return CaptureInfo
def get_data_for_gallery(self) -> list[CaptureInfo]:
"""Return all the information about the saved captures."""
def _all_captures(self) -> list[str]:
"""Return the full path for all captures on disk."""
files = os.listdir(self._data_dir)
captures = []
for filename in files:
@ -308,16 +308,31 @@ class BaseCamera(OFMThing, ABC):
BASE_IMAGE_FORMATS["jpeg"].path_matches(filename)
or BASE_IMAGE_FORMATS["png"].path_matches(filename)
):
captures.append(
CaptureInfo(
name=filename,
created=os.path.getctime(full_path),
modified=os.path.getmtime(full_path),
thing=self.name,
)
)
captures.append(full_path)
return captures
def get_data_for_gallery(self) -> list[CaptureInfo]:
"""Return all the information about the saved captures."""
return [
CaptureInfo(
name=os.path.basename(capture),
created=os.path.getctime(capture),
modified=os.path.getmtime(capture),
thing=self.name,
)
for capture in self._all_captures()
]
def delete_all_gallery_items(self) -> None:
"""Delete all the captures on the microscope.
Use with extreme caution.
"""
for capture in self._all_captures():
lt.raise_if_cancelled()
self.logger.info(f"Deleting: {capture}")
os.remove(capture)
@lt.endpoint(
"delete",
"capture/{name}",

View file

@ -36,6 +36,8 @@ class GalleryCompatibleThing(Protocol):
# Ignore D102: No docstrings for the protocol.
def get_data_for_gallery(self) -> list[BaseModel]: ... # noqa: D102
def delete_all_gallery_items(self) -> None: ... # noqa: D102
class GalleryThing(lt.Thing):
"""A Thing for communicating with the front end gallery."""
@ -95,5 +97,18 @@ class GalleryThing(lt.Thing):
"""
data_list = []
for thing in self.gallery_providing_things.values():
data_list += [model.model_dump() for model in thing.get_data_for_gallery()]
try:
data_list += [
model.model_dump() for model in thing.get_data_for_gallery()
]
except Exception as e:
# If any of the providers errors producing a list, log the exception
# and continue.
self.logger.exception(e)
return data_list
@lt.action
def delete_all_data(self) -> None:
"""Delete all the gallery data on this microscope."""
for thing in self.gallery_providing_things.values():
thing.delete_all_gallery_items()

View file

@ -192,6 +192,18 @@ class SmartScanThing(OFMThing):
ongoing=ongoing_name, include_ongoing=False
)
def delete_all_gallery_items(self) -> None:
"""Delete all the scans on the microscope.
**This will irreversibly remove all scanned data from the microscope!**
Use with extreme caution.
"""
for scan_name in self._scan_dir_manager.all_scans:
lt.raise_if_cancelled()
self.logger.info(f"Deleting: {scan_name}")
self._delete_scan(scan_name)
# Note that the default detector name is set at init. This is over written if
# setting is loaded from disk.
@lt.setting
@ -608,20 +620,6 @@ class SmartScanThing(OFMThing):
if not deleted_scan_success:
raise HTTPException(400, "Couldn't delete scan, check log for details")
@lt.endpoint(
"delete",
"scans",
)
def delete_all_scans(self) -> None:
"""Delete all the scans on the microscope.
**This will irreversibly remove all scanned data from the
microscope!**
Use with extreme caution.
"""
for scan_name in self._scan_dir_manager.all_scans:
self._delete_scan(scan_name)
@lt.action
def purge_empty_scans(self) -> None:
"""Delete all scan folders containing no images at the top level."""