111 lines
3.3 KiB
Python
111 lines
3.3 KiB
Python
from dataclasses import dataclass
|
|
import logging
|
|
import configparser
|
|
import os
|
|
|
|
|
|
def configure_loging(main_logging: logging) -> None:
|
|
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
|
|
logging.info("configure logging")
|
|
for handler in main_logging.root.handlers[:]:
|
|
main_logging.root.removeHandler(handler)
|
|
main_logging.basicConfig(
|
|
level=cfg.log.console_lvl,
|
|
datefmt=cfg.log.dare_format,
|
|
format=cfg.log.format,
|
|
)
|
|
file_handler = main_logging.FileHandler(os.path.join(log_dir, "logfile.log"))
|
|
file_handler.setLevel(cfg.log.file_lvl)
|
|
file_handler.setFormatter(logging.Formatter(cfg.log.format))
|
|
main_logging.getLogger().addHandler(file_handler)
|
|
main_logging.info("success configure logging")
|
|
|
|
|
|
def load_config() -> configparser.ConfigParser:
|
|
cfg_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config")
|
|
|
|
logging.info("loading config")
|
|
loaded_config: configparser.ConfigParser = configparser.ConfigParser(
|
|
interpolation=None
|
|
)
|
|
user_config: str = os.path.join(cfg_dir, "config.ini")
|
|
loaded_config.read(user_config)
|
|
logging.info("success loading config")
|
|
return loaded_config
|
|
|
|
|
|
config = load_config()
|
|
|
|
|
|
@dataclass
|
|
class ConfigLog:
|
|
console_lvl: int = config["log"].getint("console_lvl")
|
|
file_lvl: int = config["log"].getint("file_lvl")
|
|
dare_format: str = config["log"].get("dare_format")
|
|
format: str = config["log"].get("format")
|
|
|
|
|
|
@dataclass
|
|
class ConfigTelegram:
|
|
send: bool = config["tg"].getboolean("send")
|
|
if send:
|
|
bot_token: str = os.getenv("TG_BOT_TOKEN")
|
|
chat_id: int = int(os.getenv("TG_CHAT_ID"))
|
|
|
|
|
|
@dataclass
|
|
class ConfigBcp:
|
|
dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "backups")
|
|
file: str = config["bcp"].get("file")
|
|
pattern: str = config["bcp"].get("pattern")
|
|
start_at: str = config["bcp"].get("start_at")
|
|
|
|
|
|
@dataclass
|
|
class ConfigNetDev:
|
|
user: str = os.getenv("NET_DEV_USER")
|
|
domain: str = os.getenv("NET_DEV_DOMAIN")
|
|
pwd: str = os.getenv("NET_DEV_PWD")
|
|
ssh_port: int = config["net_dev"].getint("ssh_port")
|
|
read_timeout: int = config["net_dev"].getint("read_timeout")
|
|
|
|
|
|
@dataclass
|
|
class ConfigGit:
|
|
push: str = config["git"].getboolean("push")
|
|
user: str = config["git"].get("user")
|
|
mail: str = config["git"].get("mail")
|
|
remote: str = config["git"].get("remote")
|
|
branch: str = config["git"].get("branch")
|
|
protocol: str = config["git"].get("protocol")
|
|
username: str = os.getenv("GIT_USERNAME")
|
|
token: str = os.getenv("GIT_TOKEN")
|
|
|
|
@dataclass
|
|
class ConfigMail:
|
|
send: bool = config["mail"].getboolean("send")
|
|
if send:
|
|
user: str = os.getenv('MAIL_USER')
|
|
pwd: str = os.getenv('MAIL_PWD')
|
|
server: str = config['mail'].get('server')
|
|
port: int = config['mail'].getint('port')
|
|
from_address : str = config['mail'].get('from_address')
|
|
to_address : str = config['mail'].get('to_address')
|
|
subject : str = config['mail'].get('subject')
|
|
|
|
|
|
|
|
@dataclass()
|
|
class ConfigAll:
|
|
log: ConfigLog = ConfigLog
|
|
tg: ConfigTelegram = ConfigTelegram
|
|
bcp: ConfigBcp = ConfigBcp
|
|
net_dev: ConfigNetDev = ConfigNetDev
|
|
git: ConfigGit = ConfigGit
|
|
mail: ConfigMail = ConfigMail
|
|
|
|
|
|
cfg = ConfigAll
|
|
|
|
configure_loging(logging)
|