"""Загрузка настроек приложения. Используется библиотека `configparser`.""" import configparser from os import path class AppConfig: """Класс для хранения настроек сервиса. """ def __init__(self, path_to_conf_file: str, section: str = "Main"): """ Parameters ---------- path_to_conf_file: str Путь к файлу конфига. Можно указывать относительный. section: str Секция в конфиге, которую нужно считывать. По-умолчанию секция `[Main]`. Raises ------ KeyError Если в конфиге не найдено указанной секции. """ cfg = configparser.ConfigParser() try: cfg.read(path.abspath(path_to_conf_file)) except FileNotFoundError: print(f"File {path.abspath(path_to_conf_file)} not found!") if section not in cfg.sections(): raise KeyError(f"Section {section} not found in config file!") conf = dict(cfg.items(section)) self.template: str = conf["template"] self.path_for_config: str = conf["path_for_config"] self.frequency_sec = int(conf["frequency_sec"]) self.central_host_url: str = conf["central_host_url"] self.requests_count: int = int(conf["requests_count"]) self.request_portion: int = int(conf["request_portion"]) @property def configs_path(self) -> str: """Возвращает абсолютный путь до папки с конфигами.""" _path = path.abspath(self.path_for_config) return _path def __repr__(self): return ( f"template = {self.template}\n" f"path_for_config = {self.path_for_config}\n" f"{self.frequency_sec}\n" f"{self.central_host_url}\n" f"{self.requests_count}\n" f"{self.request_portion}\n" )