feat(wallpaper_changer): image_repository, correct number series not starting with 1

This commit is contained in:
Lucas Vivo committed 2026-11-13 18:15:19 +01:00
1 parent 03d913f251
commit f66d4c19fe
2 files changed
+11 -10

No files matched your search

+1 -1
View File
@@ -94,7 +94,7 @@ def main() -> None:
sys.exit(0)
image_repository.convert_to_jpg()
image_repository.check_consecutive_filenames(correct=True)
image_repository.check_consecutive_filenames(correct_wrong_numbers=True)
if not args.disable_encryption and (
args.check_encryption or args.mode != "default" or args.folder is not None
@@ -158,8 +158,8 @@ class ImageRepository:
return encrypted_images
def check_consecutive_filenames(
self, correct: bool = False
) -> bool:
self, correct_wrong_numbers: bool = False
) -> None:
"""Check JPG number suffixes per folder and prefix, optionally fixing gaps."""
numbered_images: dict[tuple[Path, str], list[tuple[int, str, Path]]] = {}
for path in self.img_folder_path.rglob("*"):
@@ -176,13 +176,16 @@ class ImageRepository:
renamed_files = []
is_consecutive = True
starts_with_one = True
for (folder, prefix), images in numbered_images.items():
images.sort(key=lambda image: (image[0], image[2].name))
numbers = [number for number, _, _ in images]
expected_numbers = list(
range(numbers[0], numbers[0] + len(numbers))
)
if numbers == expected_numbers:
if numbers[0] != 1:
starts_with_one = False
if numbers == expected_numbers and starts_with_one:
continue
is_consecutive = False
@@ -190,20 +193,19 @@ class ImageRepository:
f"Non-consecutive JPG filenames in {folder} for prefix "
f"{prefix!r}: found {numbers}"
)
if not correct:
if not correct_wrong_numbers:
continue
width = max(len(number_text) for _, number_text, _ in images)
for index, (_, _, path) in enumerate(images):
new_number = numbers[0] + index
new_number = index + 1
new_name = f"{prefix}{new_number:0{width}d}{path.suffix}"
renamed_files.append((path, path.with_name(new_name)))
if not correct or is_consecutive:
if not correct_wrong_numbers or (is_consecutive and starts_with_one):
logger.debug(
f"Numbered JPG images are consecutive: {is_consecutive}"
"Numbered JPG images are consecutive and start at 1:"
)
return is_consecutive
staged_files = []
for old_path, new_path in renamed_files:
@@ -218,7 +220,6 @@ class ImageRepository:
temporary_path.rename(new_path)
logger.info("Corrected gaps in JPG filename sequences")
return True
def decode_all(self, private_key: str | Path) -> list[Path]:
naming_table_path = self.img_folder_path / "naming_table.enc"