From 13b591a1578666b8976e8c95c4eaa97230078e3e Mon Sep 17 00:00:00 2001 From: Xarkam Date: Fri, 13 Mar 2026 11:21:11 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20configuration=20p?= =?UTF-8?q?our=20r=C3=A9duire=20les=20=C3=A9critures=20disque?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/configure.py | 57 ++++++++++++++++++++++++----------------- src/mainwindow.py | 8 ++++-- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/config/configure.py b/src/config/configure.py index 89c6a8f..f103ffe 100644 --- a/src/config/configure.py +++ b/src/config/configure.py @@ -1,14 +1,21 @@ import json -import os from pathlib import Path -from sys import argv, executable from typing import Any, Callable, NotRequired, TypedDict, cast # Configuration du chemin du fichier de configuration avec son nom. def _get_config_path() -> Path: - base_path = Path(executable if frozenset else os.path.realpath(argv[0])).parent.absolute() - #return base_path / "config.json" - return Path(os.path.realpath(argv[0])).parent.absolute() / "config.json" + from sys import argv, executable + import os + import sys + from pathlib import Path + + # Si l'app est packagée (PyInstaller) + if getattr(sys, "frozen", False): + base_path = Path(executable).parent + else: + base_path = Path(os.path.realpath(argv[0])).parent + + return base_path / "config.json" class ConfigData(TypedDict): discord_user_id: NotRequired[str] @@ -41,11 +48,13 @@ CONFIG_SCHEMA: dict[str, ConfigField] = { } class ConfigManager: - def __init__(self, path: Path = CONFIG_PATH) -> None: - self.path = path + def __init__(self, path: Path | None = None) -> None: + self.path = path or _get_config_path() + self._data: ConfigData = self._load() + self._dirty = False # Lecture du fichier de configuration - def load(self) -> ConfigData: + def _load(self) -> ConfigData: if not self.path.exists(): return {} @@ -61,10 +70,16 @@ class ConfigManager: return cast(ConfigData, data) # Sauvegarde du fichier de configuration - def save(self, data: ConfigData) -> None: + def save(self) -> None: + if not self._dirty: + return + self.path.parent.mkdir(parents=True, exist_ok=True) + with self.path.open("w", encoding="utf-8") as file: - json.dump(data, file, indent=4, ensure_ascii=False) + json.dump(self._data, file, indent=4, ensure_ascii=False) + + self._dirty = False def _get_field(self, key: str) -> ConfigField: if key not in CONFIG_SCHEMA: @@ -73,8 +88,7 @@ class ConfigManager: def get(self, key: str) -> Any: field = self._get_field(key) - data = self.load() - value = data.get(key, field["default"]) + value = self._data.get(key, field["default"]) if not field["validator"](value): return field["default"] @@ -83,20 +97,17 @@ class ConfigManager: def set(self, key: str, value: Any) -> None: field = self._get_field(key) - normalized_value = field["normalizer"](value) - if not field["validator"](normalized_value): - raise ValueError(f"Invalid value for config key: {key}") + normalized = field["normalizer"](value) - data = self.load() - data[key] = normalized_value - self.save(data) + if not field["validator"](normalized): + raise ValueError(f"Invalid value for {key}") - def reset_key(self, key: str) -> None: - field = self._get_field(key) - data = self.load() - data[key] = field["default"] - self.save(data) + if self._data.get(key) == normalized: + return + + self._data[key] = normalized + self._dirty = True def reset_all(self) -> None: defaults: ConfigData = cast( diff --git a/src/mainwindow.py b/src/mainwindow.py index 7bc546c..8411006 100644 --- a/src/mainwindow.py +++ b/src/mainwindow.py @@ -190,7 +190,7 @@ class MainWindow(QMainWindow): # On convertit en float pour QAudioOutput (0.0 à 1.0) volume = value / 100.0 self.audio_output.setVolume(volume) - config.set_volume(value) + self.config.set_volume(value) def mute_btn_link(self) -> None: if not self.is_muted: @@ -206,7 +206,7 @@ class MainWindow(QMainWindow): self.ui.mute_btn.setStyleSheet("background-color: red;") self.is_muted = True - config.set_volume(0) + self.config.set_volume(0) else: # --- RETOUR DU SON --- # On restaure le volume précédent @@ -220,6 +220,10 @@ class MainWindow(QMainWindow): self.is_muted = False self.config.set_volume(self.previous_volume) + def closeEvent(self, event): + self.config.save() + super().closeEvent(event) + if __name__ == "__main__": app = QApplication(sys.argv)