Update engine.py

This commit is contained in:
2026-01-17 12:35:59 +01:00
parent 130a354d6e
commit 0edd6a264f

View File

@@ -1,19 +1,20 @@
import os import os
import shutil import shutil
import json
from PIL import Image from PIL import Image
from io import BytesIO from io import BytesIO
class SorterEngine: class SorterEngine:
CONFIG_PATH = "/app/favorites.json"
@staticmethod @staticmethod
def get_images(path): def get_images(path):
"""Returns list of image files in a directory."""
exts = ('.jpg', '.jpeg', '.png', '.webp', '.bmp', '.tiff') exts = ('.jpg', '.jpeg', '.png', '.webp', '.bmp', '.tiff')
if not path or not os.path.exists(path): return [] if not path or not os.path.exists(path): return []
return sorted([f for f in os.listdir(path) if f.lower().endswith(exts)]) return sorted([f for f in os.listdir(path) if f.lower().endswith(exts)])
@staticmethod @staticmethod
def get_id_mapping(path): def get_id_mapping(path):
"""Maps idXXX prefixes to full filenames for Tab 1."""
mapping = {} mapping = {}
for f in SorterEngine.get_images(path): for f in SorterEngine.get_images(path):
if f.startswith("id") and "_" in f: if f.startswith("id") and "_" in f:
@@ -23,68 +24,52 @@ class SorterEngine:
@staticmethod @staticmethod
def get_max_id_number(folder_path): def get_max_id_number(folder_path):
"""Finds the highest idXXX_ prefix in a folder to calculate the next ID."""
max_id = 0 max_id = 0
if not folder_path or not os.path.exists(folder_path): if not folder_path or not os.path.exists(folder_path): return 0
return 0
for f in os.listdir(folder_path): for f in os.listdir(folder_path):
if f.startswith("id") and "_" in f: if f.startswith("id") and "_" in f:
try: try:
num_part = f[2:].split('_')[0] num_part = f[2:].split('_')[0]
num = int(num_part) num = int(num_part)
if num > max_id: max_id = num if num > max_id: max_id = num
except (ValueError, IndexError): except: continue
continue
return max_id return max_id
@staticmethod @staticmethod
def compress_for_web(path, quality): def compress_for_web(path, quality):
"""Reduces image size for browser display to save bandwidth."""
try: try:
with Image.open(path) as img: with Image.open(path) as img:
buf = BytesIO() buf = BytesIO()
img.convert("RGB").save(buf, format="JPEG", quality=quality) img.convert("RGB").save(buf, format="JPEG", quality=quality)
return buf return buf
except Exception: except: return None
return None
@staticmethod @staticmethod
def execute_move(t_path, c_path, t_folder, c_folder, prefix, mode="standard"): def execute_move(t_path, c_path, t_folder, c_folder, prefix, mode="standard"):
"""
Moves/Renames files based on the script's logic:
- standard: selected_target / selected_control
- solo: selected_target_solo_woman / control_selected_solo_woman
"""
t_fname = f"{prefix}{os.path.basename(t_path)}" t_fname = f"{prefix}{os.path.basename(t_path)}"
c_fname = f"{prefix}{os.path.basename(c_path)}" c_fname = f"{prefix}{os.path.basename(c_path)}"
subdirs = { subdirs = {
"standard": ("selected_target", "selected_control"), "standard": ("selected_target", "selected_control"),
"solo": ("selected_target_solo_woman", "control_selected_solo_woman") "solo": ("selected_target_solo_woman", "control_selected_solo_woman")
} }
t_sub, c_sub = subdirs[mode] t_sub, c_sub = subdirs[mode]
t_dst = os.path.join(t_folder, t_sub, t_fname) t_dst, c_dst = os.path.join(t_folder, t_sub, t_fname), os.path.join(c_folder, c_sub, c_fname)
c_dst = os.path.join(c_folder, c_sub, c_fname)
os.makedirs(os.path.dirname(t_dst), exist_ok=True) os.makedirs(os.path.dirname(t_dst), exist_ok=True)
os.makedirs(os.path.dirname(c_dst), exist_ok=True) os.makedirs(os.path.dirname(c_dst), exist_ok=True)
shutil.move(t_path, t_dst) shutil.move(t_path, t_dst)
shutil.copy2(c_path, c_dst) # Copy control as per script logic shutil.copy2(c_path, c_dst)
return t_dst, c_dst return t_dst, c_dst
@staticmethod @staticmethod
def revert_action(action): def save_favorite(name, path_t, path_c):
"""Reverses the last recorded action from the history stack.""" favs = SorterEngine.load_favorites()
if action['type'] in ['link_standard', 'link_solo']: favs[name] = {"target": path_t, "control": path_c}
if os.path.exists(action['t_dst']): with open(SorterEngine.CONFIG_PATH, 'w') as f:
shutil.move(action['t_dst'], action['t_src']) json.dump(favs, f)
if os.path.exists(action['c_dst']):
os.remove(action['c_dst']) @staticmethod
elif action['type'] == 'unused': def load_favorites():
if os.path.exists(action['t_dst']): if os.path.exists(SorterEngine.CONFIG_PATH):
shutil.move(action['t_dst'], action['t_src']) with open(SorterEngine.CONFIG_PATH, 'r') as f:
if os.path.exists(action['c_dst']): return json.load(f)
shutil.move(action['c_dst'], action['c_src']) return {}