From dc31b0bebb23446983e7c2afc2d6b3e5fefa7f77 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:17:06 +0100 Subject: [PATCH 01/11] Update engine.py --- engine.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/engine.py b/engine.py index c43446b..55c0439 100644 --- a/engine.py +++ b/engine.py @@ -27,6 +27,11 @@ class SorterEngine: cursor.execute('''CREATE TABLE IF NOT EXISTS processed_log (source_path TEXT PRIMARY KEY, category TEXT, action_type TEXT)''') + # --- NEW: FOLDER TAGS TABLE (persists tags by folder) --- + cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags + (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, + PRIMARY KEY (folder_path, filename))''') + # Seed categories if empty cursor.execute("SELECT COUNT(*) FROM categories") if cursor.fetchone()[0] == 0: @@ -256,6 +261,11 @@ class SorterEngine: def commit_global(output_root, cleanup_mode, operation="Copy", source_root=None): """Commits ALL staged files and fixes permissions.""" data = SorterEngine.get_staged_data() + + # Save folder tags BEFORE processing (so we can restore them later) + if source_root: + SorterEngine.save_folder_tags(source_root) + conn = sqlite3.connect(SorterEngine.DB_PATH) cursor = conn.cursor() @@ -513,4 +523,112 @@ class SorterEngine: for idx, img_path in enumerate(all_images): if img_path in staged_keys: tagged_pages.add(idx // page_size) - return tagged_pages \ No newline at end of file + return tagged_pages + + # --- 7. FOLDER TAG PERSISTENCE --- + @staticmethod + def save_folder_tags(folder_path): + """ + Saves current staging data associated with a folder for later restoration. + Call this BEFORE clearing the staging area. + """ + import re + staged = SorterEngine.get_staged_data() + if not staged: + return 0 + + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + + saved_count = 0 + for orig_path, info in staged.items(): + # Only save tags for files that are in this folder (or subfolders) + if orig_path.startswith(folder_path): + filename = os.path.basename(orig_path) + category = info['cat'] + + # Extract index from the new_name (e.g., "Action_042.jpg" -> 42) + new_name = info['name'] + match = re.search(r'_(\d+)', new_name) + tag_index = int(match.group(1)) if match else 0 + + cursor.execute( + "INSERT OR REPLACE INTO folder_tags VALUES (?, ?, ?, ?)", + (folder_path, filename, category, tag_index) + ) + saved_count += 1 + + conn.commit() + conn.close() + return saved_count + + @staticmethod + def restore_folder_tags(folder_path, all_images): + """ + Restores previously saved tags for a folder back into the staging area. + Call this when loading/reloading a folder. + Returns the number of tags restored. + """ + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + + # Get saved tags for this folder + cursor.execute( + "SELECT filename, category, tag_index FROM folder_tags WHERE folder_path = ?", + (folder_path,) + ) + saved_tags = {row[0]: {"cat": row[1], "index": row[2]} for row in cursor.fetchall()} + + if not saved_tags: + conn.close() + return 0 + + # Build a map of filename -> full path from current images + filename_to_path = {} + for img_path in all_images: + fname = os.path.basename(img_path) + # Only map files that haven't been mapped yet (handles duplicates by using first occurrence) + if fname not in filename_to_path: + filename_to_path[fname] = img_path + + # Restore tags to staging area + restored = 0 + for filename, tag_info in saved_tags.items(): + if filename in filename_to_path: + full_path = filename_to_path[filename] + # Check if not already staged + cursor.execute("SELECT 1 FROM staging_area WHERE original_path = ?", (full_path,)) + if not cursor.fetchone(): + ext = os.path.splitext(filename)[1] + new_name = f"{tag_info['cat']}_{tag_info['index']:03d}{ext}" + cursor.execute( + "INSERT OR REPLACE INTO staging_area VALUES (?, ?, ?, 1)", + (full_path, tag_info['cat'], new_name) + ) + restored += 1 + + conn.commit() + conn.close() + return restored + + @staticmethod + def clear_folder_tags(folder_path): + """Clears saved tags for a specific folder.""" + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + cursor.execute("DELETE FROM folder_tags WHERE folder_path = ?", (folder_path,)) + conn.commit() + conn.close() + + @staticmethod + def get_saved_folder_tags(folder_path): + """Returns saved tags for a folder (for debugging/display).""" + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + cursor.execute( + "SELECT filename, category, tag_index FROM folder_tags WHERE folder_path = ?", + (folder_path,) + ) + result = {row[0]: {"cat": row[1], "index": row[2]} for row in cursor.fetchall()} + conn.close() + return result \ No newline at end of file From 37f6166b37a7ec07b68453978ba69c5acad8177f Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:17:22 +0100 Subject: [PATCH 02/11] Update gallery_app.py --- gallery_app.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gallery_app.py b/gallery_app.py index 1b74136..2df06b4 100644 --- a/gallery_app.py +++ b/gallery_app.py @@ -114,6 +114,11 @@ def load_images(): state.all_images = SorterEngine.get_images(state.source_dir, recursive=True) + # Restore previously saved tags for this folder + restored = SorterEngine.restore_folder_tags(state.source_dir, state.all_images) + if restored > 0: + ui.notify(f"Restored {restored} tags from previous session", type='info') + # Reset page if out of bounds if state.page >= state.total_pages: state.page = 0 From c89cecd43fa94a8dcbdea15c00823cd3b9e03823 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:19:02 +0100 Subject: [PATCH 03/11] Update engine.py --- engine.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/engine.py b/engine.py index 55c0439..02f9944 100644 --- a/engine.py +++ b/engine.py @@ -540,6 +540,11 @@ class SorterEngine: conn = sqlite3.connect(SorterEngine.DB_PATH) cursor = conn.cursor() + # Ensure table exists (for existing databases) + cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags + (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, + PRIMARY KEY (folder_path, filename))''') + saved_count = 0 for orig_path, info in staged.items(): # Only save tags for files that are in this folder (or subfolders) @@ -572,6 +577,11 @@ class SorterEngine: conn = sqlite3.connect(SorterEngine.DB_PATH) cursor = conn.cursor() + # Ensure table exists (for existing databases) + cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags + (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, + PRIMARY KEY (folder_path, filename))''') + # Get saved tags for this folder cursor.execute( "SELECT filename, category, tag_index FROM folder_tags WHERE folder_path = ?", From c56b07f9999355968a0b5034e037e41f8a0b4b3e Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:21:11 +0100 Subject: [PATCH 04/11] Update engine.py --- engine.py | 93 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/engine.py b/engine.py index 02f9944..64f43c6 100644 --- a/engine.py +++ b/engine.py @@ -574,52 +574,57 @@ class SorterEngine: Call this when loading/reloading a folder. Returns the number of tags restored. """ - conn = sqlite3.connect(SorterEngine.DB_PATH) - cursor = conn.cursor() - - # Ensure table exists (for existing databases) - cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags - (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, - PRIMARY KEY (folder_path, filename))''') - - # Get saved tags for this folder - cursor.execute( - "SELECT filename, category, tag_index FROM folder_tags WHERE folder_path = ?", - (folder_path,) - ) - saved_tags = {row[0]: {"cat": row[1], "index": row[2]} for row in cursor.fetchall()} - - if not saved_tags: + try: + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + + # Ensure table exists (for existing databases) + cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags + (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, + PRIMARY KEY (folder_path, filename))''') + conn.commit() + + # Get saved tags for this folder + cursor.execute( + "SELECT filename, category, tag_index FROM folder_tags WHERE folder_path = ?", + (folder_path,) + ) + saved_tags = {row[0]: {"cat": row[1], "index": row[2]} for row in cursor.fetchall()} + + if not saved_tags: + conn.close() + return 0 + + # Build a map of filename -> full path from current images + filename_to_path = {} + for img_path in all_images: + fname = os.path.basename(img_path) + # Only map files that haven't been mapped yet (handles duplicates by using first occurrence) + if fname not in filename_to_path: + filename_to_path[fname] = img_path + + # Restore tags to staging area + restored = 0 + for filename, tag_info in saved_tags.items(): + if filename in filename_to_path: + full_path = filename_to_path[filename] + # Check if not already staged + cursor.execute("SELECT 1 FROM staging_area WHERE original_path = ?", (full_path,)) + if not cursor.fetchone(): + ext = os.path.splitext(filename)[1] + new_name = f"{tag_info['cat']}_{tag_info['index']:03d}{ext}" + cursor.execute( + "INSERT OR REPLACE INTO staging_area VALUES (?, ?, ?, 1)", + (full_path, tag_info['cat'], new_name) + ) + restored += 1 + + conn.commit() conn.close() + return restored + except Exception as e: + print(f"Error restoring folder tags: {e}") return 0 - - # Build a map of filename -> full path from current images - filename_to_path = {} - for img_path in all_images: - fname = os.path.basename(img_path) - # Only map files that haven't been mapped yet (handles duplicates by using first occurrence) - if fname not in filename_to_path: - filename_to_path[fname] = img_path - - # Restore tags to staging area - restored = 0 - for filename, tag_info in saved_tags.items(): - if filename in filename_to_path: - full_path = filename_to_path[filename] - # Check if not already staged - cursor.execute("SELECT 1 FROM staging_area WHERE original_path = ?", (full_path,)) - if not cursor.fetchone(): - ext = os.path.splitext(filename)[1] - new_name = f"{tag_info['cat']}_{tag_info['index']:03d}{ext}" - cursor.execute( - "INSERT OR REPLACE INTO staging_area VALUES (?, ?, ?, 1)", - (full_path, tag_info['cat'], new_name) - ) - restored += 1 - - conn.commit() - conn.close() - return restored @staticmethod def clear_folder_tags(folder_path): From df12413c5d8b691178d0f3d3e101b83e6384b054 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:23:34 +0100 Subject: [PATCH 05/11] Update engine.py --- engine.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/engine.py b/engine.py index 64f43c6..813fcdc 100644 --- a/engine.py +++ b/engine.py @@ -578,7 +578,18 @@ class SorterEngine: conn = sqlite3.connect(SorterEngine.DB_PATH) cursor = conn.cursor() - # Ensure table exists (for existing databases) + # Check if table exists and has correct schema + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='folder_tags'") + if cursor.fetchone(): + # Table exists - check if it has the filename column + cursor.execute("PRAGMA table_info(folder_tags)") + columns = [row[1] for row in cursor.fetchall()] + if 'filename' not in columns: + # Wrong schema - drop and recreate + cursor.execute("DROP TABLE folder_tags") + conn.commit() + + # Create table with correct schema cursor.execute('''CREATE TABLE IF NOT EXISTS folder_tags (folder_path TEXT, filename TEXT, category TEXT, tag_index INTEGER, PRIMARY KEY (folder_path, filename))''') From ce7abd8a2903fa6ae62ba0a32d89207645eda939 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:27:30 +0100 Subject: [PATCH 06/11] Update gallery_app.py --- gallery_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gallery_app.py b/gallery_app.py index 2df06b4..dd2431e 100644 --- a/gallery_app.py +++ b/gallery_app.py @@ -28,7 +28,7 @@ class AppState: self.preview_quality = 50 # Tagging State - self.active_cat = "Default" + self.active_cat = "control" self.next_index = 1 # Batch Settings From 48417b6d733c2f7a4cc9e0c41f766de3ec569748 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:27:41 +0100 Subject: [PATCH 07/11] Update engine.py --- engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine.py b/engine.py index 813fcdc..cb0809b 100644 --- a/engine.py +++ b/engine.py @@ -35,7 +35,7 @@ class SorterEngine: # Seed categories if empty cursor.execute("SELECT COUNT(*) FROM categories") if cursor.fetchone()[0] == 0: - for cat in ["_TRASH", "Default", "Action", "Solo"]: + for cat in ["_TRASH", "control", "Default", "Action", "Solo"]: cursor.execute("INSERT OR IGNORE INTO categories VALUES (?)", (cat,)) conn.commit() From 28549073598814245da1d16a50338c4c1873c0c7 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:53:51 +0100 Subject: [PATCH 08/11] Update gallery_app.py --- gallery_app.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gallery_app.py b/gallery_app.py index dd2431e..4ccff80 100644 --- a/gallery_app.py +++ b/gallery_app.py @@ -112,6 +112,9 @@ def load_images(): ui.notify(f"Source not found: {state.source_dir}", type='warning') return + # Clear staging area when loading a new folder + SorterEngine.clear_staging_area() + state.all_images = SorterEngine.get_images(state.source_dir, recursive=True) # Restore previously saved tags for this folder From dd454ebf6f4bf31d474396c9ea7aa6e0280b8e41 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 13:54:06 +0100 Subject: [PATCH 09/11] Update engine.py --- engine.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/engine.py b/engine.py index cb0809b..2f270b2 100644 --- a/engine.py +++ b/engine.py @@ -246,6 +246,15 @@ class SorterEngine: conn.commit() conn.close() + @staticmethod + def clear_staging_area(): + """Clears all items from the staging area.""" + conn = sqlite3.connect(SorterEngine.DB_PATH) + cursor = conn.cursor() + cursor.execute("DELETE FROM staging_area") + conn.commit() + conn.close() + @staticmethod def get_staged_data(): """Retrieves current tagged/staged images.""" From fe6e55de163e17f483b451a8f77315daaf2348b8 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 16:03:03 +0100 Subject: [PATCH 10/11] Update gallery_app.py --- gallery_app.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/gallery_app.py b/gallery_app.py index 4ccff80..2a9d89e 100644 --- a/gallery_app.py +++ b/gallery_app.py @@ -49,16 +49,31 @@ class AppState: def load_active_profile(self): """Load paths from active profile.""" p_data = self.profiles.get(self.profile_name, {}) - self.source_dir = p_data.get("tab5_source", "/storage") - self.output_dir = p_data.get("tab5_out", "/storage") + self.input_base = p_data.get("tab5_source", "/storage") + self.output_base = p_data.get("tab5_out", "/storage") + self.folder_name = "" + + @property + def source_dir(self): + """Computed source path: input_base/folder_name or just input_base.""" + if self.folder_name: + return os.path.join(self.input_base, self.folder_name) + return self.input_base + + @property + def output_dir(self): + """Computed output path: output_base/folder_name or just output_base.""" + if self.folder_name: + return os.path.join(self.output_base, self.folder_name) + return self.output_base def save_current_profile(self): """Save current paths to active profile.""" if self.profile_name not in self.profiles: self.profiles[self.profile_name] = {} - self.profiles[self.profile_name]["tab5_source"] = self.source_dir - self.profiles[self.profile_name]["tab5_out"] = self.output_dir - SorterEngine.save_tab_paths(self.profile_name, t5_s=self.source_dir, t5_o=self.output_dir) + self.profiles[self.profile_name]["tab5_source"] = self.input_base + self.profiles[self.profile_name]["tab5_out"] = self.output_base + SorterEngine.save_tab_paths(self.profile_name, t5_s=self.input_base, t5_o=self.output_base) ui.notify(f"Profile '{self.profile_name}' saved!", type='positive') def get_categories(self) -> List[str]: @@ -490,10 +505,12 @@ def build_header(): # Source and output paths with ui.row().classes('flex-grow gap-2'): - ui.input('Source').bind_value(state, 'source_dir') \ - .classes('flex-grow').props('dark dense outlined') - ui.input('Output').bind_value(state, 'output_dir') \ - .classes('flex-grow').props('dark dense outlined') + ui.input('Input Base').bind_value(state, 'input_base') \ + .classes('w-48').props('dark dense outlined') + ui.input('Output Base').bind_value(state, 'output_base') \ + .classes('w-48').props('dark dense outlined') + ui.input('Folder (optional)').bind_value(state, 'folder_name') \ + .classes('w-32').props('dark dense outlined') ui.button(icon='save', on_click=state.save_current_profile) \ .props('flat round color=white') From 0f0aeed2f1fe61e8a3e72a6f6aa6350068432c8c Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 20 Jan 2026 16:08:21 +0100 Subject: [PATCH 11/11] Update gallery_app.py --- gallery_app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gallery_app.py b/gallery_app.py index 2a9d89e..ec19316 100644 --- a/gallery_app.py +++ b/gallery_app.py @@ -506,11 +506,11 @@ def build_header(): # Source and output paths with ui.row().classes('flex-grow gap-2'): ui.input('Input Base').bind_value(state, 'input_base') \ - .classes('w-48').props('dark dense outlined') + .classes('flex-grow').props('dark dense outlined') ui.input('Output Base').bind_value(state, 'output_base') \ - .classes('w-48').props('dark dense outlined') + .classes('flex-grow').props('dark dense outlined') ui.input('Folder (optional)').bind_value(state, 'folder_name') \ - .classes('w-32').props('dark dense outlined') + .classes('flex-grow').props('dark dense outlined') ui.button(icon='save', on_click=state.save_current_profile) \ .props('flat round color=white')