from __future__ import annotations import logging from configparser import ConfigParser from typing import Dict, Mapping, Union class Config(ConfigParser): DEFAULTS: Mapping[str, Mapping[str, Union[str, int]]] = { "sampy": { "host": "0.0.0.0", "port": 7777, "hostname": "Python > C", "password": "changeme", "rcon_password": "changeme too", "max_players": 50, "mode": "Unknown", "language": "python", }, "sampy.rules": { "weburl": "https://git.osufx.com/Sunpy/sampy", }, "logging": { "filename": "", "level": "INFO", "datefmt": r"%%d-%%b-%%y %%H:%%M:%%S", }, } def __init__(self, *filenames): super().__init__() self.read_dict(self.DEFAULTS) found = self.read(filenames) missing = set(filenames) - set(found) if len(missing): logging.warn("Config files not found: %s" % missing) def save(self, path: str): with open(path, "w") as f: self.write(f) @property def host(self) -> str: return self.get("sampy", "host") @property def port(self) -> int: return self.getint("sampy", "port") @property def hostname(self) -> str: return self.get("sampy", "hostname") @property def password(self) -> str: return self.get("sampy", "password") @property def rcon_password(self) -> str: return self.get("sampy", "rcon_password") @property def max_players(self) -> int: return self.getint("sampy", "max_players") @property def mode(self) -> str: return self.get("sampy", "mode") @property def language(self) -> str: return self.get("sampy", "language") @property def rules(self) -> Dict[str, str]: return self["sampy.rules"]