Split up ScanDirectory._find_files into seperate functions

This commit is contained in:
Julian Stirling 2025-07-01 19:42:09 +01:00
parent 254305a7a5
commit 7295b86cf6
2 changed files with 53 additions and 42 deletions

View file

@ -5,7 +5,7 @@ Currently it handles scan getting information from a information from a scan dir
eventually is should handle all the file system operation for smart_scan.
"""
from typing import Optional, Literal
from typing import Optional
import os
import re
import shutil
@ -254,40 +254,40 @@ class ScanDirectory:
return []
return os.listdir(self.images_dir)
def _find_files(
self, file_types=Literal["all", "scan_images", "stitches", "dzi"]
) -> tuple[list[str], list[str], list[str]] | list[str]:
folder_contents = self.get_scan_files()
def _extract_scan_images(self, file_list: list[str]):
"""Extract files which match the naming convention for scan images
# Return empty lists if no folder contents.
if not folder_contents:
if file_types == "all":
return [], [], []
return []
:param file_list: The list of files to search. Normally this would be
`self.get_scan_files()`
if file_types in ["all", "scan_images"]:
scan_images = [i for i in folder_contents if IMAGE_REGEX.search(i)]
if file_types == "scan_images":
return scan_images
:returns: The list of files that match the naming convention for scan images
"""
return [i for i in file_list if IMAGE_REGEX.search(i)]
if file_types in ["all", "stitches"]:
stitches = [i for i in folder_contents if STITCH_REGEX.search(i)]
if file_types == "stitches":
return stitches
def _extract_final_stitches(self, file_list: list[str]):
"""Extract files which match the naming convention for final stitches
if file_types in ["all", "dzi"]:
dzi_files = [i for i in folder_contents if i.endswith("dzi")]
if file_types == "dzi":
return dzi_files
:param file_list: The list of files to search.
return scan_images, stitches, dzi_files
:returns: The list of files that match the naming convention for final stitches
"""
return [i for i in file_list if STITCH_REGEX.search(i)]
def _extract_dzi_files(self, file_list: list[str]):
"""Extract files which match the naming convention for dzi_files
:param file_list: The list of files to search.
:returns: The list of files that match the naming convention for dzi_files
"""
return [i for i in file_list if i.endswith("dzi")]
def get_final_stitch_name(self) -> Optional[str]:
"""Return the filename for the final stitch (in the images dir)
If no final stitch is found, return None
"""
stitches = self._find_files("stitches")
stitches = self._extract_final_stitches(self.get_scan_files())
if not stitches:
return None
return stitches[0]
@ -299,7 +299,10 @@ class ScanDirectory:
def scan_info(self) -> ScanInfo:
"""Return the information for the scan directory as a ScanInfo object"""
scan_images, stitches, dzi_files = self._find_files("all")
scan_files = self.get_scan_files()
scan_images = self._extract_scan_images(scan_files)
stitches = self._extract_final_stitches(scan_files)
dzi_files = self._extract_dzi_files(scan_files)
number_of_images = len(scan_images)
stitch_available = len(stitches) > 0
dzi = None if not dzi_files else str(dzi_files[0])
@ -349,7 +352,8 @@ class ScanDirectory:
zip_files = []
# For each `filename.dzi` we need to skip the `filename_files` directory
dzi_dirs = [dzi[:-4] + "_files" for dzi in self._find_files("dzi")]
dzi_files = self._extract_dzi_files(self.get_scan_files())
dzi_dirs = [dzi[:-4] + "_files" for dzi in dzi_files]
with zipfile.ZipFile(zip_fname, mode="a") as scan_zip:
for file in self.all_files(skip_dirs=dzi_dirs):

View file

@ -401,7 +401,7 @@ def test_none_returned_for_missing_images_dir():
assert scan_dir.get_scan_files() == []
def test_find_files():
def test_extracting_files():
"""Test the private _find_files method of ScanDirectories
Add files to directory and check expected returns.
@ -410,34 +410,41 @@ def test_find_files():
os.makedirs(os.path.join(BASE_SCAN_DIR, "fake_scan_0001", "images"))
scan_dir = ScanDirectory("fake_scan_0001", BASE_SCAN_DIR)
# For an empty directory, "all" is 3 empty lists
assert scan_dir._find_files("all") == ([], [], [])
# Other options are 1 empty list.
for file_type in ["scan_images", "stitches", "dzi"]:
assert scan_dir._find_files(file_type) == []
# Starting all lists should be empty
scan_files = scan_dir.get_scan_files()
assert scan_dir._extract_scan_images(scan_files) == []
assert scan_dir._extract_final_stitches(scan_files) == []
assert scan_dir._extract_dzi_files(scan_files) == []
# Add a number of images
for i in range(2321):
_add_fake_image(scan_dir)
assert len(set(scan_dir._find_files("scan_images"))) == 2321
assert len(set(scan_dir._find_files("stitches"))) == 0
assert len(set(scan_dir._find_files("dzi"))) == 0
scan_files = scan_dir.get_scan_files()
assert len(set(scan_dir._extract_scan_images(scan_files))) == 2321
assert len(set(scan_dir._extract_final_stitches(scan_files))) == 0
assert len(set(scan_dir._extract_dzi_files(scan_files))) == 0
# Add and a stitched image
_add_fake_file(scan_dir, "fake_scan_0001_stitched.jpg", in_im_dir=True)
assert len(set(scan_dir._find_files("scan_images"))) == 2321
assert len(set(scan_dir._find_files("stitches"))) == 1
assert len(set(scan_dir._find_files("dzi"))) == 0
scan_files = scan_dir.get_scan_files()
assert len(set(scan_dir._extract_scan_images(scan_files))) == 2321
assert len(set(scan_dir._extract_final_stitches(scan_files))) == 1
assert len(set(scan_dir._extract_dzi_files(scan_files))) == 0
_make_fake_dzi(scan_dir)
assert len(set(scan_dir._find_files("scan_images"))) == 2321
assert len(set(scan_dir._find_files("stitches"))) == 1
assert len(set(scan_dir._find_files("dzi"))) == 1
# check totals are still correct after adding a dzi with lots of tiles.
scan_files = scan_dir.get_scan_files()
scan_images = scan_dir._extract_scan_images(scan_files)
stitches = scan_dir._extract_final_stitches(scan_files)
dzi_files = scan_dir._extract_dzi_files(scan_files)
assert len(set(scan_images)) == 2321
assert len(set(stitches)) == 1
assert len(set(dzi_files)) == 1
_, stitches, dzi_files = scan_dir._find_files("all")
# And check the names are as expected
assert stitches[0] == "fake_scan_0001_stitched.jpg"
assert dzi_files[0] == "fake_scan_0001.dzi"