diff options
| author | salmonik <raisersoft@proton.me> | 2026-03-23 17:43:09 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-03-23 17:43:09 +0100 |
| commit | 106789319b1de06e9e8206c3a8d1dedab84496a1 (patch) | |
| tree | 9fd916450f911c2332e4ff3e80d78cdf4f2a6998 /cogs/utils/settings.py | |
| parent | normalised times on vclogs, and improved jarvis via reply chaining (diff) | |
| download | senator-sperma-106789319b1de06e9e8206c3a8d1dedab84496a1.tar.gz senator-sperma-106789319b1de06e9e8206c3a8d1dedab84496a1.zip | |
unify settings api into one file
Diffstat (limited to 'cogs/utils/settings.py')
| -rw-r--r-- | cogs/utils/settings.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py new file mode 100644 index 0000000..0a1716f --- /dev/null +++ b/cogs/utils/settings.py | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import json | ||
| 7 | import logging | ||
| 8 | import aiofiles | ||
| 9 | import os | ||
| 10 | |||
| 11 | logger = logging.getLogger(__name__) | ||
| 12 | |||
| 13 | _settings = None | ||
| 14 | settings_file = "data/settings.json" | ||
| 15 | |||
| 16 | async def load_settings(): | ||
| 17 | global _settings | ||
| 18 | global settings_file | ||
| 19 | |||
| 20 | default_settings = { | ||
| 21 | 'volume': 1.0, | ||
| 22 | 'baseurl': 'http://localhost:8080' | ||
| 23 | } | ||
| 24 | |||
| 25 | try: | ||
| 26 | if os.path.exists(settings_file): | ||
| 27 | async with aiofiles.open(settings_file, 'r') as f: | ||
| 28 | content = await f.read() | ||
| 29 | if content.strip(): | ||
| 30 | _settings = json.loads(content) | ||
| 31 | logger.info("Settings loaded into memory") | ||
| 32 | else: | ||
| 33 | _settings = default_settings | ||
| 34 | await save_settings() | ||
| 35 | logger.warning("Empty settings.json, reset to defaults") | ||
| 36 | else: | ||
| 37 | _settings = default_settings | ||
| 38 | await save_settings() | ||
| 39 | logger.info("Created default settings.json") | ||
| 40 | except (json.JSONDecodeError, Exception) as e: | ||
| 41 | logger.error(f"Error loading settings: {e}") | ||
| 42 | _settings = default_settings | ||
| 43 | |||
| 44 | return _settings | ||
| 45 | |||
| 46 | |||
| 47 | async def save_settings(): | ||
| 48 | if _settings: | ||
| 49 | async with aiofiles.open(settings_file, 'w') as f: | ||
| 50 | await f.write(json.dumps(_settings, indent=2)) | ||
| 51 | |||
| 52 | |||
| 53 | def get_settings(): | ||
| 54 | return _settings | ||
| 55 | |||
| 56 | |||
| 57 | def get_volume(): | ||
| 58 | return _settings.get('volume', 1.0) if _settings else 1.0 | ||
| 59 | |||
| 60 | |||
| 61 | def get_baseurl(): | ||
| 62 | return _settings.get('baseurl', 'http://localhost:8080').rstrip('/') if _settings else 'http://localhost:8080' \ No newline at end of file | ||
