diff options
Diffstat (limited to 'cogs/utils/settings.py')
| -rw-r--r-- | cogs/utils/settings.py | 114 |
1 files changed, 0 insertions, 114 deletions
diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py deleted file mode 100644 index b1a585c..0000000 --- a/cogs/utils/settings.py +++ /dev/null | |||
| @@ -1,114 +0,0 @@ | |||
| 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 logging | ||
| 7 | import sqlite3 | ||
| 8 | import os | ||
| 9 | from dataclasses import dataclass, field | ||
| 10 | |||
| 11 | from cogs.utils.database import get_connection, init_database | ||
| 12 | |||
| 13 | logger = logging.getLogger(__name__) | ||
| 14 | |||
| 15 | DEFAULT_BASEURL = "http://localhost:3939" | ||
| 16 | |||
| 17 | init_database() | ||
| 18 | |||
| 19 | |||
| 20 | @dataclass | ||
| 21 | class Settings: | ||
| 22 | volume: float = 1.0 | ||
| 23 | baseurl: str = DEFAULT_BASEURL | ||
| 24 | _data: dict = field(default_factory=dict, init=False) | ||
| 25 | |||
| 26 | async def load(self) -> "Settings": | ||
| 27 | default_settings = { | ||
| 28 | "volume": 1.0, | ||
| 29 | "baseurl": DEFAULT_BASEURL, | ||
| 30 | } | ||
| 31 | |||
| 32 | try: | ||
| 33 | with get_connection() as conn: | ||
| 34 | c = conn.cursor() | ||
| 35 | c.execute("SELECT key, value FROM settings") | ||
| 36 | rows = c.fetchall() | ||
| 37 | |||
| 38 | if rows: | ||
| 39 | self._data = {row[0]: row[1] for row in rows} | ||
| 40 | self._sync_from_data() | ||
| 41 | logger.info("Settings loaded from database") | ||
| 42 | else: | ||
| 43 | self._data = default_settings.copy() | ||
| 44 | self._sync_from_data() | ||
| 45 | await self.save() | ||
| 46 | logger.info("Created default settings in database") | ||
| 47 | except Exception as e: | ||
| 48 | logger.error(f"Error loading settings: {e}") | ||
| 49 | self._data = default_settings.copy() | ||
| 50 | self._sync_from_data() | ||
| 51 | |||
| 52 | return self | ||
| 53 | |||
| 54 | def _sync_from_data(self): | ||
| 55 | try: | ||
| 56 | self.volume = float(self._data.get("volume", 1.0)) | ||
| 57 | except (ValueError, TypeError): | ||
| 58 | self.volume = 1.0 | ||
| 59 | self.baseurl = self._data.get("baseurl", DEFAULT_BASEURL) | ||
| 60 | |||
| 61 | def _sync_to_data(self): | ||
| 62 | self._data["volume"] = str(self.volume) | ||
| 63 | self._data["baseurl"] = self.baseurl | ||
| 64 | |||
| 65 | async def save(self): | ||
| 66 | self._sync_to_data() | ||
| 67 | with get_connection() as conn: | ||
| 68 | c = conn.cursor() | ||
| 69 | c.execute("PRAGMA foreign_keys = ON") | ||
| 70 | for key, value in self._data.items(): | ||
| 71 | c.execute( | ||
| 72 | "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)", | ||
| 73 | (key, value), | ||
| 74 | ) | ||
| 75 | conn.commit() | ||
| 76 | |||
| 77 | |||
| 78 | _settings_instance: Settings | None = None | ||
| 79 | |||
| 80 | |||
| 81 | async def load_settings() -> Settings: | ||
| 82 | global _settings_instance | ||
| 83 | _settings_instance = Settings() | ||
| 84 | await _settings_instance.load() | ||
| 85 | return _settings_instance | ||
| 86 | |||
| 87 | |||
| 88 | async def save_settings(): | ||
| 89 | if _settings_instance: | ||
| 90 | await _settings_instance.save() | ||
| 91 | |||
| 92 | |||
| 93 | def get_settings() -> Settings | None: | ||
| 94 | return _settings_instance | ||
| 95 | |||
| 96 | |||
| 97 | def get_volume() -> float: | ||
| 98 | return _settings_instance.volume if _settings_instance else 1.0 | ||
| 99 | |||
| 100 | |||
| 101 | def get_baseurl() -> str: | ||
| 102 | url = os.getenv("COPYPARTY_URL") | ||
| 103 | if not url: | ||
| 104 | logger.warning("COPYPARTY_URL not set! Using default http://localhost:3939") | ||
| 105 | return DEFAULT_BASEURL | ||
| 106 | return url.rstrip("/") | ||
| 107 | |||
| 108 | |||
| 109 | def get_uploadurl() -> str: | ||
| 110 | url = os.getenv("COPYPARTY_URL_UPLOAD") | ||
| 111 | if not url: | ||
| 112 | logger.warning("COPYPARTY_URL_UPLOAD not set! Using default http://localhost:3939") | ||
| 113 | return DEFAULT_BASEURL | ||
| 114 | return url.rstrip("/") | ||
