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>
43 lines
1 KiB
Python
43 lines
1 KiB
Python
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()
|