Update engine.py

This commit is contained in:
2026-01-17 15:06:46 +01:00
parent 8289c6efb4
commit 933156b119

View File

@@ -12,15 +12,18 @@ class SorterEngine:
"""Initializes SQLite tables for Profiles, Folder IDs, and Categories.""" """Initializes SQLite tables for Profiles, Folder IDs, and Categories."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
# Profiles: Stores Discovery and Review paths
# Table 1: Profiles - Stores all discovery, review, and output paths
cursor.execute('''CREATE TABLE IF NOT EXISTS profiles cursor.execute('''CREATE TABLE IF NOT EXISTS profiles
(name TEXT PRIMARY KEY, disc_t TEXT, rev_t TEXT, rev_c TEXT, path_out TEXT, mode TEXT)''') (name TEXT PRIMARY KEY, disc_t TEXT, rev_t TEXT, rev_c TEXT, path_out TEXT, mode TEXT)''')
# Folder IDs: Maps source folders to unique numeric IDs
# Table 2: Folder IDs - Maps source paths to unique numeric IDs for renaming
cursor.execute('''CREATE TABLE IF NOT EXISTS folder_ids (path TEXT PRIMARY KEY, folder_id INTEGER)''') cursor.execute('''CREATE TABLE IF NOT EXISTS folder_ids (path TEXT PRIMARY KEY, folder_id INTEGER)''')
# Categories: Stores the list of sorting categories from your script
# Table 3: Categories - Stores the sorting buttons for Tab 4
cursor.execute('''CREATE TABLE IF NOT EXISTS categories (name TEXT PRIMARY KEY)''') cursor.execute('''CREATE TABLE IF NOT EXISTS categories (name TEXT PRIMARY KEY)''')
# Seed default categories if empty # Seed default categories if the table is new
cursor.execute("SELECT COUNT(*) FROM categories") cursor.execute("SELECT COUNT(*) FROM categories")
if cursor.fetchone()[0] == 0: if cursor.fetchone()[0] == 0:
for cat in ["_TRASH", "Default", "Action", "Solo"]: for cat in ["_TRASH", "Default", "Action", "Solo"]:
@@ -31,24 +34,27 @@ class SorterEngine:
@staticmethod @staticmethod
def get_folder_id(source_path): def get_folder_id(source_path):
"""Retrieves or generates a unique ID for a folder.""" """Retrieves or generates a persistent unique ID for a folder."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT folder_id FROM folder_ids WHERE path = ?", (source_path,)) cursor.execute("SELECT folder_id FROM folder_ids WHERE path = ?", (source_path,))
result = cursor.fetchone() result = cursor.fetchone()
if result: if result:
fid = result[0] fid = result[0]
else: else:
cursor.execute("SELECT MAX(folder_id) FROM folder_ids") cursor.execute("SELECT MAX(folder_id) FROM folder_ids")
row = cursor.fetchone() row = cursor.fetchone()
fid = (row[0] + 1) if row[0] else 1 fid = (row[0] + 1) if row and row[0] else 1
cursor.execute("INSERT INTO folder_ids VALUES (?, ?)", (source_path, fid)) cursor.execute("INSERT INTO folder_ids VALUES (?, ?)", (source_path, fid))
conn.commit() conn.commit()
conn.close() conn.close()
return fid return fid
@staticmethod @staticmethod
def get_categories(): def get_categories():
"""Fetches the category list for dynamic button rendering."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT name FROM categories ORDER BY name ASC") cursor.execute("SELECT name FROM categories ORDER BY name ASC")
@@ -58,6 +64,7 @@ class SorterEngine:
@staticmethod @staticmethod
def add_category(name): def add_category(name):
"""Adds a new persistent category to the database."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("INSERT OR IGNORE INTO categories VALUES (?)", (name,)) cursor.execute("INSERT OR IGNORE INTO categories VALUES (?)", (name,))
@@ -66,6 +73,7 @@ class SorterEngine:
@staticmethod @staticmethod
def save_profile(name, disc_t, rev_t, rev_c, path_out, mode): def save_profile(name, disc_t, rev_t, rev_c, path_out, mode):
"""Persists the entire path configuration to the database."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("INSERT OR REPLACE INTO profiles VALUES (?, ?, ?, ?, ?, ?)", cursor.execute("INSERT OR REPLACE INTO profiles VALUES (?, ?, ?, ?, ?, ?)",
@@ -75,6 +83,7 @@ class SorterEngine:
@staticmethod @staticmethod
def load_profiles(): def load_profiles():
"""Loads all saved path presets."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT * FROM profiles") cursor.execute("SELECT * FROM profiles")
@@ -84,21 +93,24 @@ class SorterEngine:
@staticmethod @staticmethod
def delete_profile(name): def delete_profile(name):
"""Removes a profile from the database."""
conn = sqlite3.connect(SorterEngine.DB_PATH) conn = sqlite3.connect(SorterEngine.DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("DELETE FROM profiles WHERE name = ?", (name,)) cursor.execute("DELETE FROM profiles WHERE name = ?", (name,))
conn.commit() conn.commit()
conn.close() conn.close()
# --- Standard File Operations --- # --- File Operations ---
@staticmethod @staticmethod
def get_images(path): def get_images(path):
"""Standard image scanning for directories."""
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 harmonize_names(t_p, c_p): def harmonize_names(t_p, c_p):
"""Forces the control file to match the target name to fix ID collisions."""
t_name = os.path.basename(t_p) t_name = os.path.basename(t_p)
new_c_p = os.path.join(os.path.dirname(c_p), t_name) new_c_p = os.path.join(os.path.dirname(c_p), t_name)
if os.path.exists(new_c_p) and c_p != new_c_p: if os.path.exists(new_c_p) and c_p != new_c_p:
@@ -109,9 +121,19 @@ class SorterEngine:
@staticmethod @staticmethod
def compress_for_web(path, quality): def compress_for_web(path, quality):
"""Converts raw images to compressed JPEGs for fast browser loading."""
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: return None except: return None
@staticmethod
def revert_action(action):
"""Undoes the last move operation by reversing source and destination."""
if action['type'] in ['link_standard', 'link_solo', 'unused', 'cat_move']:
if os.path.exists(action['t_dst']): shutil.move(action['t_dst'], action['t_src'])
if 'c_dst' in action and os.path.exists(action['c_dst']):
if 'link' in action['type']: os.remove(action['c_dst'])
else: shutil.move(action['c_dst'], action['c_src'])