Merge pull request 'tag' (#5) from tag into main
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
157
engine.py
157
engine.py
@@ -27,10 +27,15 @@ class SorterEngine:
|
|||||||
cursor.execute('''CREATE TABLE IF NOT EXISTS processed_log
|
cursor.execute('''CREATE TABLE IF NOT EXISTS processed_log
|
||||||
(source_path TEXT PRIMARY KEY, category TEXT, action_type TEXT)''')
|
(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
|
# Seed categories if empty
|
||||||
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", "control", "Default", "Action", "Solo"]:
|
||||||
cursor.execute("INSERT OR IGNORE INTO categories VALUES (?)", (cat,))
|
cursor.execute("INSERT OR IGNORE INTO categories VALUES (?)", (cat,))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -241,6 +246,15 @@ class SorterEngine:
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
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
|
@staticmethod
|
||||||
def get_staged_data():
|
def get_staged_data():
|
||||||
"""Retrieves current tagged/staged images."""
|
"""Retrieves current tagged/staged images."""
|
||||||
@@ -256,6 +270,11 @@ class SorterEngine:
|
|||||||
def commit_global(output_root, cleanup_mode, operation="Copy", source_root=None):
|
def commit_global(output_root, cleanup_mode, operation="Copy", source_root=None):
|
||||||
"""Commits ALL staged files and fixes permissions."""
|
"""Commits ALL staged files and fixes permissions."""
|
||||||
data = SorterEngine.get_staged_data()
|
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)
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
@@ -513,4 +532,138 @@ class SorterEngine:
|
|||||||
for idx, img_path in enumerate(all_images):
|
for idx, img_path in enumerate(all_images):
|
||||||
if img_path in staged_keys:
|
if img_path in staged_keys:
|
||||||
tagged_pages.add(idx // page_size)
|
tagged_pages.add(idx // page_size)
|
||||||
return tagged_pages
|
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()
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(SorterEngine.DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 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))''')
|
||||||
|
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
|
||||||
|
|
||||||
|
@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
|
||||||
@@ -28,7 +28,7 @@ class AppState:
|
|||||||
self.preview_quality = 50
|
self.preview_quality = 50
|
||||||
|
|
||||||
# Tagging State
|
# Tagging State
|
||||||
self.active_cat = "Default"
|
self.active_cat = "control"
|
||||||
self.next_index = 1
|
self.next_index = 1
|
||||||
|
|
||||||
# Batch Settings
|
# Batch Settings
|
||||||
@@ -49,16 +49,31 @@ class AppState:
|
|||||||
def load_active_profile(self):
|
def load_active_profile(self):
|
||||||
"""Load paths from active profile."""
|
"""Load paths from active profile."""
|
||||||
p_data = self.profiles.get(self.profile_name, {})
|
p_data = self.profiles.get(self.profile_name, {})
|
||||||
self.source_dir = p_data.get("tab5_source", "/storage")
|
self.input_base = p_data.get("tab5_source", "/storage")
|
||||||
self.output_dir = p_data.get("tab5_out", "/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):
|
def save_current_profile(self):
|
||||||
"""Save current paths to active profile."""
|
"""Save current paths to active profile."""
|
||||||
if self.profile_name not in self.profiles:
|
if self.profile_name not in self.profiles:
|
||||||
self.profiles[self.profile_name] = {}
|
self.profiles[self.profile_name] = {}
|
||||||
self.profiles[self.profile_name]["tab5_source"] = self.source_dir
|
self.profiles[self.profile_name]["tab5_source"] = self.input_base
|
||||||
self.profiles[self.profile_name]["tab5_out"] = self.output_dir
|
self.profiles[self.profile_name]["tab5_out"] = self.output_base
|
||||||
SorterEngine.save_tab_paths(self.profile_name, t5_s=self.source_dir, t5_o=self.output_dir)
|
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')
|
ui.notify(f"Profile '{self.profile_name}' saved!", type='positive')
|
||||||
|
|
||||||
def get_categories(self) -> List[str]:
|
def get_categories(self) -> List[str]:
|
||||||
@@ -112,8 +127,16 @@ def load_images():
|
|||||||
ui.notify(f"Source not found: {state.source_dir}", type='warning')
|
ui.notify(f"Source not found: {state.source_dir}", type='warning')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Clear staging area when loading a new folder
|
||||||
|
SorterEngine.clear_staging_area()
|
||||||
|
|
||||||
state.all_images = SorterEngine.get_images(state.source_dir, recursive=True)
|
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
|
# Reset page if out of bounds
|
||||||
if state.page >= state.total_pages:
|
if state.page >= state.total_pages:
|
||||||
state.page = 0
|
state.page = 0
|
||||||
@@ -482,9 +505,11 @@ def build_header():
|
|||||||
|
|
||||||
# Source and output paths
|
# Source and output paths
|
||||||
with ui.row().classes('flex-grow gap-2'):
|
with ui.row().classes('flex-grow gap-2'):
|
||||||
ui.input('Source').bind_value(state, 'source_dir') \
|
ui.input('Input Base').bind_value(state, 'input_base') \
|
||||||
.classes('flex-grow').props('dark dense outlined')
|
.classes('flex-grow').props('dark dense outlined')
|
||||||
ui.input('Output').bind_value(state, 'output_dir') \
|
ui.input('Output Base').bind_value(state, 'output_base') \
|
||||||
|
.classes('flex-grow').props('dark dense outlined')
|
||||||
|
ui.input('Folder (optional)').bind_value(state, 'folder_name') \
|
||||||
.classes('flex-grow').props('dark dense outlined')
|
.classes('flex-grow').props('dark dense outlined')
|
||||||
|
|
||||||
ui.button(icon='save', on_click=state.save_current_profile) \
|
ui.button(icon='save', on_click=state.save_current_profile) \
|
||||||
|
|||||||
Reference in New Issue
Block a user