Update engine.py
This commit is contained in:
138
engine.py
138
engine.py
@@ -10,22 +10,33 @@ class SorterEngine:
|
|||||||
# --- DATABASE INITIALIZATION ---
|
# --- DATABASE INITIALIZATION ---
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_db():
|
def init_db():
|
||||||
"""Initializes SQLite tables for Profiles, Folder IDs, and Categories."""
|
"""Initializes all SQLite tables for the multi-tab system."""
|
||||||
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Profile table supports 7 columns for independent paths and naming mode
|
# 1. Profiles Table: Stores independent paths for all 5 tabs
|
||||||
cursor.execute('''CREATE TABLE IF NOT EXISTS profiles
|
cursor.execute('''CREATE TABLE IF NOT EXISTS profiles
|
||||||
(name TEXT PRIMARY KEY,
|
(name TEXT PRIMARY KEY,
|
||||||
tab1_target TEXT,
|
tab1_target TEXT,
|
||||||
tab2_target TEXT, tab2_control TEXT,
|
tab2_target TEXT, tab2_control TEXT,
|
||||||
tab4_source TEXT, tab4_out TEXT,
|
tab4_source TEXT, tab4_out TEXT,
|
||||||
mode TEXT)''')
|
mode TEXT,
|
||||||
|
tab5_source TEXT, tab5_out TEXT)''')
|
||||||
|
|
||||||
|
# 2. Folder IDs Table: Maps paths to persistent numeric IDs
|
||||||
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)''')
|
||||||
|
|
||||||
|
# 3. Categories Table: Stores sorting buttons
|
||||||
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
|
# 4. Staging Area Table: Tracks pending renames for the Gallery Tab
|
||||||
|
cursor.execute('''CREATE TABLE IF NOT EXISTS staging_area
|
||||||
|
(original_path TEXT PRIMARY KEY,
|
||||||
|
target_category TEXT,
|
||||||
|
new_name TEXT,
|
||||||
|
is_marked INTEGER DEFAULT 0)''')
|
||||||
|
|
||||||
|
# Seed default categories
|
||||||
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"]:
|
||||||
@@ -36,15 +47,16 @@ class SorterEngine:
|
|||||||
|
|
||||||
# --- PROFILE & PATH MANAGEMENT ---
|
# --- PROFILE & PATH MANAGEMENT ---
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def save_tab_paths(profile_name, t1_t=None, t2_t=None, t2_c=None, t4_s=None, t4_o=None, mode=None):
|
def save_tab_paths(profile_name, t1_t=None, t2_t=None, t2_c=None, t4_s=None, t4_o=None, mode=None, t5_s=None, t5_o=None):
|
||||||
"""Updates specific tab paths while preserving others in the DB."""
|
"""Updates specific tab paths in the database while preserving others."""
|
||||||
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT * FROM profiles WHERE name = ?", (profile_name,))
|
cursor.execute("SELECT * FROM profiles WHERE name = ?", (profile_name,))
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
|
|
||||||
if not row:
|
if not row:
|
||||||
row = (profile_name, "/storage", "/storage", "/storage", "/storage", "/storage", "id")
|
# Default structure if profile is new (9 columns total)
|
||||||
|
row = (profile_name, "/storage", "/storage", "/storage", "/storage", "/storage", "id", "/storage", "/storage")
|
||||||
|
|
||||||
new_values = (
|
new_values = (
|
||||||
profile_name,
|
profile_name,
|
||||||
@@ -53,24 +65,29 @@ class SorterEngine:
|
|||||||
t2_c if t2_c is not None else row[3],
|
t2_c if t2_c is not None else row[3],
|
||||||
t4_s if t4_s is not None else row[4],
|
t4_s if t4_s is not None else row[4],
|
||||||
t4_o if t4_o is not None else row[5],
|
t4_o if t4_o is not None else row[5],
|
||||||
mode if mode is not None else row[6]
|
mode if mode is not None else row[6],
|
||||||
|
t5_s if t5_s is not None else row[7],
|
||||||
|
t5_o if t5_o is not None else row[8]
|
||||||
)
|
)
|
||||||
cursor.execute("INSERT OR REPLACE INTO profiles VALUES (?, ?, ?, ?, ?, ?, ?)", new_values)
|
cursor.execute("INSERT OR REPLACE INTO profiles VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", new_values)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_profiles():
|
def load_profiles():
|
||||||
"""Loads all workspace presets from the database."""
|
"""Loads all workspace 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")
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
conn.close()
|
conn.close()
|
||||||
return {r[0]: {"tab1_target": r[1], "tab2_target": r[2], "tab2_control": r[3],
|
return {r[0]: {
|
||||||
"tab4_source": r[4], "tab4_out": r[5], "mode": r[6]} for r in rows}
|
"tab1_target": r[1], "tab2_target": r[2], "tab2_control": r[3],
|
||||||
|
"tab4_source": r[4], "tab4_out": r[5], "mode": r[6],
|
||||||
|
"tab5_source": r[7], "tab5_out": r[8]
|
||||||
|
} for r in rows}
|
||||||
|
|
||||||
# --- CATEGORY MANAGEMENT (WITH DISK RENAMING) ---
|
# --- CATEGORY MANAGEMENT ---
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_categories():
|
def get_categories():
|
||||||
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
@@ -93,14 +110,10 @@ class SorterEngine:
|
|||||||
"""Renames category in DB and renames the physical folder on disk."""
|
"""Renames category in DB and renames the physical folder on disk."""
|
||||||
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# 1. Update Database
|
|
||||||
cursor.execute("UPDATE categories SET name = ? WHERE name = ?", (new_name, old_name))
|
cursor.execute("UPDATE categories SET name = ? WHERE name = ?", (new_name, old_name))
|
||||||
|
|
||||||
# 2. Rename on Disk
|
|
||||||
old_path = os.path.join(output_base_path, old_name)
|
old_path = os.path.join(output_base_path, old_name)
|
||||||
new_path = os.path.join(output_base_path, new_name)
|
new_path = os.path.join(output_base_path, new_name)
|
||||||
|
|
||||||
if os.path.exists(old_path) and not os.path.exists(new_path):
|
if os.path.exists(old_path) and not os.path.exists(new_path):
|
||||||
os.rename(old_path, new_path)
|
os.rename(old_path, new_path)
|
||||||
|
|
||||||
@@ -111,9 +124,7 @@ class SorterEngine:
|
|||||||
def sync_categories_from_disk(output_path):
|
def sync_categories_from_disk(output_path):
|
||||||
"""Scans output directory and adds subfolders as DB categories."""
|
"""Scans output directory and adds subfolders as DB categories."""
|
||||||
if not output_path or not os.path.exists(output_path): return 0
|
if not output_path or not os.path.exists(output_path): return 0
|
||||||
existing_folders = [d for d in os.listdir(output_path)
|
existing_folders = [d for d in os.listdir(output_path) if os.path.isdir(os.path.join(output_path, d)) and not d.startswith(".")]
|
||||||
if os.path.isdir(os.path.join(output_path, d))
|
|
||||||
and not d.startswith(".")]
|
|
||||||
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
added = 0
|
added = 0
|
||||||
@@ -124,10 +135,10 @@ class SorterEngine:
|
|||||||
conn.close()
|
conn.close()
|
||||||
return added
|
return added
|
||||||
|
|
||||||
# --- IMAGE & ID SCANNING ---
|
# --- IMAGE & ID OPERATIONS ---
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_images(path, recursive=False):
|
def get_images(path, recursive=False):
|
||||||
"""Standard image scanner with optional subfolder support."""
|
"""Image scanner with optional recursive subfolder support."""
|
||||||
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 []
|
||||||
image_list = []
|
image_list = []
|
||||||
@@ -142,7 +153,7 @@ class SorterEngine:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_id_mapping(path):
|
def get_id_mapping(path):
|
||||||
"""Groups files by idXXX_ prefix to detect collisions."""
|
"""Maps idXXX prefixes for Tab 2 collision handling."""
|
||||||
mapping = {}
|
mapping = {}
|
||||||
images = SorterEngine.get_images(path, recursive=False)
|
images = SorterEngine.get_images(path, recursive=False)
|
||||||
for f in images:
|
for f in images:
|
||||||
@@ -155,7 +166,6 @@ class SorterEngine:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_max_id_number(target_path):
|
def get_max_id_number(target_path):
|
||||||
"""Finds the highest idXXX_ prefix in a directory."""
|
|
||||||
max_id = 0
|
max_id = 0
|
||||||
if not target_path or not os.path.exists(target_path): return 0
|
if not target_path or not os.path.exists(target_path): return 0
|
||||||
for f in os.listdir(target_path):
|
for f in os.listdir(target_path):
|
||||||
@@ -184,7 +194,62 @@ class SorterEngine:
|
|||||||
conn.close()
|
conn.close()
|
||||||
return fid
|
return fid
|
||||||
|
|
||||||
# --- FILE MANIPULATION ---
|
# --- STAGING AREA (TAB 5) ---
|
||||||
|
@staticmethod
|
||||||
|
def stage_image(original_path, category, new_name):
|
||||||
|
"""Records a pending rename/move in the database."""
|
||||||
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute('''INSERT OR REPLACE INTO staging_area
|
||||||
|
(original_path, target_category, new_name, is_marked)
|
||||||
|
VALUES (?, ?, ?, 1)''', (original_path, category, new_name))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_staged_data():
|
||||||
|
"""Retrieves current tagged/staged images."""
|
||||||
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("SELECT * FROM staging_area")
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
conn.close()
|
||||||
|
return {r[0]: {"cat": r[1], "name": r[2], "marked": r[3]} for r in rows}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def commit_staging(output_root, cleanup_mode, source_root=None):
|
||||||
|
"""Commits staging: renames/moves tagged files and cleans unmarked ones."""
|
||||||
|
data = SorterEngine.get_staged_data()
|
||||||
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
staged_paths = set(data.keys())
|
||||||
|
|
||||||
|
# 1. Process Tagged Files
|
||||||
|
for old_p, info in data.items():
|
||||||
|
if info['marked'] and os.path.exists(old_p):
|
||||||
|
dest_dir = os.path.join(output_root, info['cat'])
|
||||||
|
os.makedirs(dest_dir, exist_ok=True)
|
||||||
|
final_dst = os.path.join(dest_dir, info['name'])
|
||||||
|
shutil.move(old_p, final_dst)
|
||||||
|
|
||||||
|
# 2. Cleanup Unmarked Files
|
||||||
|
if cleanup_mode != "Keep in Source" and source_root:
|
||||||
|
all_images = SorterEngine.get_images(source_root, recursive=True)
|
||||||
|
for img_p in all_images:
|
||||||
|
if img_p not in staged_paths:
|
||||||
|
if cleanup_mode == "Move to Unused":
|
||||||
|
unused_dir = os.path.join(source_root, "unused")
|
||||||
|
os.makedirs(unused_dir, exist_ok=True)
|
||||||
|
shutil.move(img_p, os.path.join(unused_dir, os.path.basename(img_p)))
|
||||||
|
elif cleanup_mode == "Delete Permanent":
|
||||||
|
os.remove(img_p)
|
||||||
|
|
||||||
|
cursor.execute("DELETE FROM staging_area")
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
# --- CORE UTILITIES ---
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def harmonize_names(t_p, c_p):
|
def harmonize_names(t_p, c_p):
|
||||||
t_name = os.path.basename(t_p)
|
t_name = os.path.basename(t_p)
|
||||||
@@ -205,30 +270,9 @@ class SorterEngine:
|
|||||||
os.rename(old_path, new_path)
|
os.rename(old_path, new_path)
|
||||||
return new_path
|
return new_path
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def move_to_unused_synced(t_p, c_p, t_root, c_root):
|
|
||||||
t_name = os.path.basename(t_p)
|
|
||||||
t_un = os.path.join(t_root, "unused", t_name)
|
|
||||||
c_un = os.path.join(c_root, "unused", t_name)
|
|
||||||
os.makedirs(os.path.dirname(t_un), exist_ok=True)
|
|
||||||
os.makedirs(os.path.dirname(c_un), exist_ok=True)
|
|
||||||
shutil.move(t_p, t_un)
|
|
||||||
shutil.move(c_p, c_un)
|
|
||||||
return t_un, c_un
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def restore_from_unused(t_p, c_p, t_root, c_root):
|
|
||||||
t_name = os.path.basename(t_p)
|
|
||||||
t_dst = os.path.join(t_root, "selected_target", t_name)
|
|
||||||
c_dst = os.path.join(c_root, "selected_control", t_name)
|
|
||||||
os.makedirs(os.path.dirname(t_dst), exist_ok=True)
|
|
||||||
os.makedirs(os.path.dirname(c_dst), exist_ok=True)
|
|
||||||
shutil.move(t_p, t_dst)
|
|
||||||
shutil.move(c_p, c_dst)
|
|
||||||
return t_dst, c_dst
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compress_for_web(path, quality):
|
def compress_for_web(path, quality):
|
||||||
|
"""Compresses images for UI performance."""
|
||||||
try:
|
try:
|
||||||
with Image.open(path) as img:
|
with Image.open(path) as img:
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
|
|||||||
Reference in New Issue
Block a user