Skip to content

Commit 27fafd8

Browse files
author
Avaxerrr
committed
v0.8.0
- Add persistent saving for the custom exclusions
1 parent bd5661d commit 27fafd8

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

main.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from core.exclusion_manager import ExclusionManager
1515
from utils.file_exporter import FileExporter
1616
from utils.theme_manager import ThemeManager
17+
from utils.exclusion_config import load_exclusion_config, save_exclusion_config
1718
import resources_rc
1819

1920
class FileTreeGeneratorApp:
@@ -22,8 +23,6 @@ def __init__(self):
2223
self._set_app_icon()
2324

2425
# ---- Global font setup (from qrc) ----
25-
# Make sure the font is listed in your resources.qrc, e.g.:
26-
# <file>resources/Sora-VariableFont_wght.ttf</file>
2726
font_id = QFontDatabase.addApplicationFont(":/resources/Sora-VariableFont_wght.ttf")
2827
if font_id != -1:
2928
families = QFontDatabase.applicationFontFamilies(font_id)
@@ -48,6 +47,9 @@ def __init__(self):
4847
# Initialize exclusion manager
4948
self.exclusion_manager = ExclusionManager()
5049

50+
# Load exclusion config and apply to panel/manager
51+
self._load_and_apply_exclusion_config()
52+
5153
# Connect signals
5254
self.window.address_bar.path_changed.connect(self.generate_tree)
5355
self.window.address_bar.refresh_requested.connect(self.refresh_tree)
@@ -56,7 +58,9 @@ def __init__(self):
5658
self.window.export_panel.export_requested.connect(self.export_tree)
5759
self.window.exclusion_panel.exclusions_changed.connect(self.update_exclusions)
5860

59-
# Initialize current path and tree data
61+
# Ensure exclusion config is saved on app exit
62+
self.app.aboutToQuit.connect(self._save_exclusion_config)
63+
6064
self.current_path = ""
6165
self.tree_data = None
6266

@@ -77,18 +81,44 @@ def process_command_line(self):
7781
# Set the path in the address bar and generate tree
7882
self.window.address_bar.set_path(folder_path)
7983

84+
def _load_and_apply_exclusion_config(self):
85+
"""Load exclusions and checkbox state from config.json and apply to panel/manager"""
86+
config = load_exclusion_config()
87+
patterns = config.get("patterns", [])
88+
use_common = config.get("use_common", True)
89+
# Set UI panel
90+
self.window.exclusion_panel.set_exclusion_patterns(patterns)
91+
self.window.exclusion_panel.set_use_common_exclusions(use_common)
92+
# Set backend manager
93+
self.exclusion_manager.clear_patterns()
94+
for pattern in patterns:
95+
self.exclusion_manager.add_pattern(pattern)
96+
self.exclusion_manager.set_use_common_exclusions(use_common)
97+
98+
def _save_exclusion_config(self):
99+
"""Save current exclusions and checkbox state to config.json"""
100+
patterns = self.window.exclusion_panel.get_exclusion_patterns()
101+
use_common = self.window.exclusion_panel.use_common_exclusions()
102+
print(f"[LOG] Saving exclusion config: {len(patterns)} patterns, use_common={use_common}")
103+
save_exclusion_config(patterns, use_common)
104+
print("[LOG] Exclusion config saved successfully.")
105+
80106
def update_exclusions(self):
81107
"""Update exclusion patterns from the UI and regenerate tree"""
82108
# Update exclusion manager from UI
83109
self.exclusion_manager.clear_patterns()
84110
patterns = self.window.exclusion_panel.get_exclusion_patterns()
111+
print(f"[LOG] Updating exclusions: {len(patterns)} patterns")
85112
for pattern in patterns:
86113
self.exclusion_manager.add_pattern(pattern)
87114

88115
# Set common exclusions setting
89-
self.exclusion_manager.set_use_common_exclusions(
90-
self.window.exclusion_panel.use_common_exclusions()
91-
)
116+
use_common = self.window.exclusion_panel.use_common_exclusions()
117+
print(f"[LOG] Use common exclusions: {use_common}")
118+
self.exclusion_manager.set_use_common_exclusions(use_common)
119+
120+
# Save exclusions immediately on change
121+
self._save_exclusion_config()
92122

93123
# Regenerate tree if we have a path
94124
if self.current_path:
@@ -107,7 +137,6 @@ def generate_tree(self, path: str):
107137

108138
# Format and display the tree
109139
self.update_tree_format()
110-
111140
self.window.set_status(f"Directory scanned successfully: {path}")
112141
except Exception as e:
113142
self.window.set_status(f"Error scanning directory: {str(e)}")

ui/components/exclusion_panel.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,15 @@ def get_exclusion_patterns(self) -> list:
117117
def use_common_exclusions(self) -> bool:
118118
"""Check if common exclusions should be used"""
119119
return self.common_exclusions_cb.isChecked()
120+
121+
def set_exclusion_patterns(self, patterns: list):
122+
"""Set the list of exclusion patterns in the UI."""
123+
self.exclusion_list.clear()
124+
for pattern in patterns:
125+
self.exclusion_list.addItem(pattern)
126+
self._on_exclusions_changed() # Optionally emit signal if you want
127+
128+
def set_use_common_exclusions(self, use: bool):
129+
"""Set the state of the 'Use common exclusions' checkbox."""
130+
self.common_exclusions_cb.setChecked(use)
131+
# self._on_exclusions_changed() # Not needed, as toggling the checkbox emits the signal

utils/exclusion_config.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import json
3+
import sys
4+
5+
def get_config_path():
6+
"""Return the path to config.json in the executable's directory."""
7+
if getattr(sys, 'frozen', False):
8+
# Running as a packaged executable
9+
base_dir = os.path.dirname(sys.executable)
10+
else:
11+
# Running as a script
12+
base_dir = os.path.dirname(os.path.abspath(__file__))
13+
# Go up to project root if in utils/
14+
base_dir = os.path.abspath(os.path.join(base_dir, '..'))
15+
return os.path.join(base_dir, "config.json")
16+
17+
def load_exclusion_config():
18+
"""Load exclusion patterns and use_common flag from config.json"""
19+
config_path = get_config_path()
20+
if os.path.exists(config_path):
21+
try:
22+
with open(config_path, "r", encoding="utf-8") as f:
23+
data = json.load(f)
24+
patterns = data.get("patterns", [])
25+
use_common = data.get("use_common", True)
26+
return {"patterns": patterns, "use_common": use_common}
27+
except Exception:
28+
pass
29+
# Defaults if not found or error
30+
return {"patterns": [], "use_common": True}
31+
32+
def save_exclusion_config(patterns, use_common):
33+
"""Save exclusion patterns and use_common flag to config.json"""
34+
config_path = get_config_path()
35+
data = {
36+
"patterns": patterns,
37+
"use_common": use_common
38+
}
39+
try:
40+
with open(config_path, "w", encoding="utf-8") as f:
41+
json.dump(data, f, indent=2)
42+
except Exception as e:
43+
print(f"Failed to save exclusion config: {e}")

0 commit comments

Comments
 (0)