|
|
|
@ -5,14 +5,37 @@ Config related tools.
|
|
|
|
|
import dataclasses
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class Config:
|
|
|
|
|
username: str
|
|
|
|
|
homeserver: str
|
|
|
|
|
password: str
|
|
|
|
|
port: int
|
|
|
|
|
host: str
|
|
|
|
|
alert_rooms: list[str]
|
|
|
|
|
port: int = 8000
|
|
|
|
|
host: str = "127.0.0.1"
|
|
|
|
|
tls: bool = False
|
|
|
|
|
tls_auth: bool = False
|
|
|
|
|
tls_crt: Optional[str] = None
|
|
|
|
|
tls_key: Optional[str] = None
|
|
|
|
|
ca_crt: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
def check_integrity(self)->bool:
|
|
|
|
|
""" Check the integrity of the config.
|
|
|
|
|
Raise an error if the config is invalid,
|
|
|
|
|
should always return True (or raise an error).
|
|
|
|
|
"""
|
|
|
|
|
if self.tls_auth and not self.tls:
|
|
|
|
|
raise ValueError("tls_auth is enable, but not tls.")
|
|
|
|
|
if self.tls and tls_crt is None:
|
|
|
|
|
raise ValueError("tls is enable but tls_crt was not provided")
|
|
|
|
|
if self.tls and tls_key is None:
|
|
|
|
|
raise ValueError("tls is enable but tls_key was not provided")
|
|
|
|
|
if self.tls_auth and ca_cert is None:
|
|
|
|
|
raise ValueError("tls_auth is enable, but ca_crt was not provided")
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(file:str)->Config:
|
|
|
|
|
"""
|
|
|
|
@ -20,5 +43,7 @@ def load_config(file:str)->Config:
|
|
|
|
|
"""
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
data = yaml.load(f, Loader=yaml.loader.SafeLoader)
|
|
|
|
|
return Config(**data)
|
|
|
|
|
config = Config(**data)
|
|
|
|
|
config.check_integrity()
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|