🐛 fix(config): save to config.yaml instead of default_config.yaml

- _find_config() now looks for config.yaml first, then default_config.yaml
- _load_config() sets save path to config.yaml when loading from default
- _save_config() always saves to config/config.yaml
- _on_close() saves to config.yaml on window close
- Added _get_user_config_path() helper method
This commit is contained in:
Jeremy Shen
2026-06-10 11:21:34 +08:00
parent 9de0153854
commit 1bbce6e72a
+61 -14
View File
@@ -101,39 +101,68 @@ class MainWindow(ctk.CTk):
# ── Config ─────────────────────────────────────────────────
def _find_config(self) -> Path | None:
"""Find the configuration file.
"""Find the user configuration file.
Checks common locations:
1. config/default_config.yaml (relative to app)
2. config/zynq_flasher.yaml
1. config/config.yaml (user's config)
2. config/default_config.yaml (template, not for saving)
3. User-specified path via env var
Returns:
Path to config file, or None if not found.
Path to user config file, or None if not found.
"""
candidates = [
Path(__file__).parent.parent / "config" / "default_config.yaml",
]
app_dir = Path(__file__).parent.parent
config_dir = app_dir / "config"
# 1. Check for user config first
user_config = config_dir / "config.yaml"
if user_config.exists():
return user_config
# 2. Check for default config (template)
default_config = config_dir / "default_config.yaml"
if default_config.exists():
return default_config
# 3. Check env var
env_path = os.environ.get("ZYNQ_CONFIG")
if env_path:
candidates.insert(0, Path(env_path))
for path in candidates:
if path.exists():
return path
return Path(env_path)
return None
def _get_user_config_path(self) -> Path:
"""Get the path to the user config file (config.yaml).
Creates config.yaml if it doesn't exist.
Returns:
Path to user config file.
"""
app_dir = Path(__file__).parent.parent
config_path = app_dir / "config" / "config.yaml"
config_path.parent.mkdir(parents=True, exist_ok=True)
return config_path
def _load_config(self) -> None:
"""Load configuration from file or create default."""
if self._config_path:
try:
self._config = Config.from_file(self._config_path)
# If loaded from default_config.yaml, set save path to config.yaml
if self._config_path.name == "default_config.yaml":
user_config_path = self._get_user_config_path()
self._config._config_path = user_config_path
except Exception:
self._config = Config.from_default()
# Set save path to config.yaml for new configs
user_config_path = self._get_user_config_path()
self._config._config_path = user_config_path
else:
self._config = Config.from_default()
# Set save path to config.yaml for new configs
user_config_path = self._get_user_config_path()
self._config._config_path = user_config_path
def _sync_config_from_ui(self) -> None:
"""Read current UI values and update the Config object.
@@ -156,10 +185,25 @@ class MainWindow(ctk.CTk):
self._config.firmware_bin_path = files["firmware_bin_path"]
def _save_config(self) -> None:
"""Save current configuration to file."""
"""Save current configuration to user config file (config.yaml).
Always saves to config/config.yaml, not to default_config.yaml.
"""
self._sync_config_from_ui() # Sync UI values first
if self._config and self._config.config_path:
if not self._config:
self._log_message(" No config loaded")
return
# Save to user config file (config.yaml)
user_config_path = self._get_user_config_path()
self._config._config_path = user_config_path # Set save path
try:
self._config.save()
self._log_message(f" Config saved to {user_config_path}")
self._status.set_status("Config saved", "success")
except Exception as e:
self._log_message(f" ✗ Save failed: {e}")
self._status.set_status(f"Save failed: {e}", "error")
# ── UI Construction ────────────────────────────────────────
@@ -978,6 +1022,9 @@ class MainWindow(ctk.CTk):
"""Handle window close event: save config and cleanup."""
if self._config:
self._sync_config_from_ui() # Sync UI values first
# Save to user config file (config.yaml)
user_config_path = self._get_user_config_path()
self._config._config_path = user_config_path
try:
self._config.save()
except Exception: