From 570dba09a65f3f889da739da3d00461023f930b9 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 9 May 2022 15:34:00 -0400 Subject: move to using config.py parser stuff --- config.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 config.py (limited to 'config.py') diff --git a/config.py b/config.py new file mode 100644 index 0000000..958c095 --- /dev/null +++ b/config.py @@ -0,0 +1,43 @@ +from dataclasses import dataclass +from os.path import expanduser +from re import compile as re_compile +from typing import List, Pattern, Tuple + +import yaml + +@dataclass +class Config(object): + server: Tuple[str, int, bool] + nickname: str + username: str + realname: str + password: str + channel: str + + sasl: Tuple[str, str] + +def load(filepath: str): + with open(filepath) as file: + config_yaml = yaml.safe_load(file.read()) + + nickname = config_yaml["nickname"] + + server = config_yaml["server"] + hostname, port_s = server.split(":", 1) + tls = False + + if port_s.startswith("+"): + tls = True + port_s = port_s.lstrip("+") + port = int(port_s) + + return Config( + (hostname, port, tls), + nickname, + config_yaml.get("username", nickname), + config_yaml.get("realname", nickname), + config_yaml["password"], + config_yaml["channel"], + (config_yaml["sasl"]["username"], config_yaml["sasl"]["password"]), + ) + -- cgit 1.4.1