diff options
| author | salmonik <raisersoft@proton.me> | 2026-04-19 15:43:32 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-04-19 15:43:32 +0200 |
| commit | 1dcdbe3cf03ff16b3ff2e2e6d572e0b3c72c18c0 (patch) | |
| tree | f11dc45777d8b3c8ff0e2ca16be83f85b272bd48 /cogs/utils/settings.py | |
| parent | test: add cron tests + improve existing tests (diff) | |
| download | senator-sperma-1dcdbe3cf03ff16b3ff2e2e6d572e0b3c72c18c0.tar.gz senator-sperma-1dcdbe3cf03ff16b3ff2e2e6d572e0b3c72c18c0.zip | |
refactor: simplify cogs, reduce complexity
- Remove unused imports and dead code
- Cleaner error handling
- Reduce cognitive complexity
Diffstat (limited to '')
| -rw-r--r-- | cogs/utils/settings.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py index 984ec16..d66ce7f 100644 --- a/cogs/utils/settings.py +++ b/cogs/utils/settings.py | |||
| @@ -12,18 +12,19 @@ from dataclasses import dataclass, field | |||
| 12 | logger = logging.getLogger(__name__) | 12 | logger = logging.getLogger(__name__) |
| 13 | 13 | ||
| 14 | settings_file = "data/settings.json" | 14 | settings_file = "data/settings.json" |
| 15 | DEFAULT_BASEURL = "http://localhost:8080" | ||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | @dataclass | 18 | @dataclass |
| 18 | class Settings: | 19 | class Settings: |
| 19 | volume: float = 1.0 | 20 | volume: float = 1.0 |
| 20 | baseurl: str = "http://localhost:8080" | 21 | baseurl: str = DEFAULT_BASEURL |
| 21 | _data: dict = field(default_factory=dict, init=False) | 22 | _data: dict = field(default_factory=dict, init=False) |
| 22 | 23 | ||
| 23 | async def load(self) -> "Settings": | 24 | async def load(self) -> "Settings": |
| 24 | default_settings = { | 25 | default_settings = { |
| 25 | "volume": 1.0, | 26 | "volume": 1.0, |
| 26 | "baseurl": "http://localhost:8080", | 27 | "baseurl": DEFAULT_BASEURL, |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | try: | 30 | try: |
| @@ -44,7 +45,7 @@ class Settings: | |||
| 44 | self._sync_from_data() | 45 | self._sync_from_data() |
| 45 | await self.save() | 46 | await self.save() |
| 46 | logger.info("Created default settings.json") | 47 | logger.info("Created default settings.json") |
| 47 | except (json.JSONDecodeError, Exception) as e: | 48 | except json.JSONDecodeError as e: |
| 48 | logger.error(f"Error loading settings: {e}") | 49 | logger.error(f"Error loading settings: {e}") |
| 49 | self._data = default_settings.copy() | 50 | self._data = default_settings.copy() |
| 50 | self._sync_from_data() | 51 | self._sync_from_data() |
| @@ -53,7 +54,7 @@ class Settings: | |||
| 53 | 54 | ||
| 54 | def _sync_from_data(self): | 55 | def _sync_from_data(self): |
| 55 | self.volume = self._data.get("volume", 1.0) | 56 | self.volume = self._data.get("volume", 1.0) |
| 56 | self.baseurl = self._data.get("baseurl", "http://localhost:8080") | 57 | self.baseurl = self._data.get("baseurl", DEFAULT_BASEURL) |
| 57 | 58 | ||
| 58 | def _sync_to_data(self): | 59 | def _sync_to_data(self): |
| 59 | self._data["volume"] = self.volume | 60 | self._data["volume"] = self.volume |
| @@ -89,4 +90,4 @@ def get_volume() -> float: | |||
| 89 | 90 | ||
| 90 | 91 | ||
| 91 | def get_baseurl() -> str: | 92 | def get_baseurl() -> str: |
| 92 | return _settings_instance.baseurl.rstrip("/") if _settings_instance else "http://localhost:8080" | 93 | return _settings_instance.baseurl.rstrip("/") if _settings_instance else DEFAULT_BASEURL |
