diff --git a/src/openflexure_microscope_server/scan_directories.py b/src/openflexure_microscope_server/scan_directories.py index 3ef0c946..a8850afb 100644 --- a/src/openflexure_microscope_server/scan_directories.py +++ b/src/openflexure_microscope_server/scan_directories.py @@ -276,11 +276,15 @@ class ScanDirectoryManager: return [f.name for f in os.scandir(self._base_scan_dir) if f.is_dir()] @requires_lock - def all_scans_info(self) -> list[ScanInfo]: + def all_scans_info(self, ongoing: Optional[str] = None) -> list[ScanInfo]: """Return a lists of ScanInfo objects for each scan.""" all_info: list[ScanInfo] = [] for scan_name in self.all_scans: - all_info.append(ScanDirectory(scan_name, self.base_dir).scan_info()) + # If the scan is ongoing send flag to skip reading the json data + skip_json = scan_name == ongoing + scan_dir = ScanDirectory(scan_name, self.base_dir) + info = scan_dir.scan_info(skip_json=skip_json) + all_info.append(info) return all_info @requires_lock @@ -504,7 +508,7 @@ class ScanDirectory: LOGGER.warning(f"Could not validate scan data for {self.name}.") return None - def scan_info(self) -> ScanInfo: + def scan_info(self, skip_json: bool = False) -> ScanInfo: """Return the information to be used in the UI for the scan.""" scan_files = self.get_scan_files() scan_images = self._extract_scan_images(scan_files) @@ -514,7 +518,7 @@ class ScanDirectory: stitch_available = len(stitches) > 0 dzi = None if not dzi_files else str(dzi_files[0]) - scan_data = self.get_scan_data() + scan_data = None if skip_json else self.get_scan_data() duration = ( None if scan_data is None or scan_data.duration is None diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index 0fc84f1c..c9454b11 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -588,6 +588,16 @@ class SmartScanThing(lt.Thing): ) """Whether to run a final stitch at the end of a successful scan.""" + def _get_all_scan_info(self) -> list[scan_directories.ScanInfo]: + """Return all the information from the scan directories. + + It is preferable to use the method rather than calling + _scan_dir_manager.all_scans_info() directly as it will handle stopping the json + in any ongoing scans being read. + """ + ongoing_name = None if self._ongoing_scan is None else self._ongoing_scan.name + return self._scan_dir_manager.all_scans_info(ongoing=ongoing_name) + @lt.thing_property def scans(self) -> ScanListInfo: """All the available scans. @@ -599,7 +609,7 @@ class SmartScanThing(lt.Thing): break it. """ return ScanListInfo( - scans=self._scan_dir_manager.all_scans_info(), + scans=self._get_all_scan_info(), ongoing=None if self._ongoing_scan is None else self._ongoing_scan.name, ) @@ -667,7 +677,7 @@ class SmartScanThing(lt.Thing): def purge_empty_scans(self, logger: lt.deps.InvocationLogger) -> None: """Delete all scan folders containing no images at the top level.""" # JSON is ignored as it's created before any images are captured - for scan_info in self._scan_dir_manager.all_scans_info(): + for scan_info in self._get_all_scan_info(): if scan_info.number_of_images == 0: self._delete_scan(scan_info.name, logger) @@ -791,6 +801,6 @@ class SmartScanThing(lt.Thing): """ if self._scan_logger is not None: raise RuntimeError("Can't stitch previous scans while a scan is ongoing") - for scan in self._scan_dir_manager.all_scans_info(): + for scan in self._get_all_scan_info(): if scan.dzi is None: self.stitch_scan(logger=logger, cancel=cancel, scan_name=scan.name)