refactor: add config.py

this change adds self validation in __init__, allows to make config values with different types (not just string) and stores config values in classes for easier accessibility

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2025-03-26 20:29:44 +03:00
parent 768f9abdcf
commit 5262975275
Signed by: mctaylors
GPG key ID: 4EEF4F949A266EE2
5 changed files with 57 additions and 35 deletions

43
config.py Normal file
View file

@ -0,0 +1,43 @@
import configparser
from typing import Optional
import requests
_c = configparser.ConfigParser()
_c.read("config.ini")
class _General:
token: str
sourceurl: Optional[str]
def __init__(self):
self.token = _c.get('General', 'Token')
self.sourceurl = _c.get('General', 'SourceUrl')
if not self.sourceurl.startswith("http://") and not self.sourceurl.startswith("https://"):
self.sourceurl = None
class _App:
name: str
host: str
hostname: str
def __init__(self):
self.name = _c.get('App', 'Name')
self.host = _c.get('App', 'Host')
# noinspection PyBroadException
try:
response = requests.get(f"http://{self.host}/status.json")
if response.status_code != 200:
raise
except:
print("Unable validate App.Host in config.ini.")
exit(1)
self.hostname = _c.get('App', 'HostName')
if not self.hostname:
self.hostname = self.host
general = _General()
app = _App()