From 843389a747b12544d91f18d37a0af4f127eec640 Mon Sep 17 00:00:00 2001 From: Lucas Vivo Date: Sun, 15 Nov 2026 19:50:55 +0100 Subject: [PATCH] feat(wallpaper_changer): replace shorter filenames of same hash, add raise error raise error if naming table is missing raise error if check_encription finds no folder duplcate hashes take the longer filename from new image --- .../src/wallpaper_changer/image_repository.py | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/wallpaper_changer/src/wallpaper_changer/image_repository.py b/wallpaper_changer/src/wallpaper_changer/image_repository.py index 2b1d6d2..8c2204c 100644 --- a/wallpaper_changer/src/wallpaper_changer/image_repository.py +++ b/wallpaper_changer/src/wallpaper_changer/image_repository.py @@ -21,13 +21,13 @@ class WallpaperImage: aspect: float class ImageRepository: - def __init__(self, root: str | Path): - self.img_folder_path = Path(root) + def __init__(self, img_folder_path: str | Path): + self.img_folder_path = Path(img_folder_path) logger.info(f"Set {self.img_folder_path} as root dir") self.image_list = [] def scan(self, private_key: str | Path, img_dir: Path | None = None, - disable_encryption: bool = False): + disable_encryption: bool = False, calc_hash: bool = False): out = [] result: dict[str,dict[str, str]] = {} img_scan_path = self.img_folder_path if img_dir is None else img_dir @@ -56,7 +56,13 @@ class ImageRepository: { "file": file, "name": result[file.name].get("name") if result.get(file.name) else file.name, - "hash": result[file.name].get("hash") if result.get(file.name) else None, + "hash": ( + result[file.name].get("hash") + if result.get(file.name) + else hashlib.sha256(file.read_bytes()).hexdigest() + if calc_hash + else None + ), } ) self.image_list = out @@ -77,11 +83,12 @@ class ImageRepository: return converted_paths def check_encryption( - self, new_images_folder: Path, private_key: str | Path, replace: bool = False + self, private_key: str | Path, new_images_folder: Path | None = None, replace: bool = False ) -> list[Path]: + if not new_images_folder: + new_images_folder = self.img_folder_path if not new_images_folder.is_dir(): - logger.warning(f"New folder does not exist: {new_images_folder}") - return [] + raise ValueError(f"New folder does not exist: {new_images_folder}") new_images = sorted(new_images_folder.glob("*")) if not new_images: logger.trace(f"No new images found in {new_images_folder}") @@ -112,9 +119,13 @@ class ImageRepository: skip = False existing_entry = None for entry in naming_table: - if entry.image_hash == file_hash and not replace: - logger.warning(f"Skipping already encrypted image: {image_path.name}") - logger.warning(f"Entry: {entry.image_name}:{entry.encrypted_file}") + if entry.image_hash == file_hash and not replace and len(entry.image_name) >= len(image_path.name): + logger.warning(f"Skipping already encrypted image: {image_path.name}, Entry: {entry.image_name}:{entry.encrypted_file}") + skip = True + break + elif entry.image_hash == file_hash and not replace and len(entry.image_name) < len(image_path.name): + logger.warning(f"Already encrypted image updated with longer name {entry.image_name} -> {image_path.name}") + entry.image_name = image_path.name skip = True break elif entry.image_name == image_path.name or entry.image_hash == file_hash: @@ -228,6 +239,8 @@ class ImageRepository: naming_table_path, private_key ) encrypted_images = sorted(self.img_folder_path.rglob("*.jpg_enc")) + if encrypted_images and not naming_table: + raise FileNotFoundError("No naming table found") decoded_paths = [] sources_to_remove = [] @@ -236,12 +249,10 @@ class ImageRepository: continue image_name,image_hash = next( - ( - (item.image_name,item.image_hash) - for item in naming_table - if item.encrypted_file == encrypted_path.name - ) - ) + (item.image_name,item.image_hash) + for item in naming_table + if item.encrypted_file == encrypted_path.name + ) if image_name is None: image_name = self._find_name_in_legacy_tables(encrypted_path.name) logger.warning(