diff options
author | crupest <crupest@outlook.com> | 2022-11-19 13:00:13 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-11-19 13:00:13 +0800 |
commit | f775f4c76691fc61b2993d81d9f1b4690e76b141 (patch) | |
tree | 9fe248e5379c66cbaa740243bebc04bb7df7cd29 /tool/modules/configfile.py | |
parent | 1562a317e147954c5618ecdccbfd10c944bf81ce (diff) | |
download | crupest-f775f4c76691fc61b2993d81d9f1b4690e76b141.tar.gz crupest-f775f4c76691fc61b2993d81d9f1b4690e76b141.tar.bz2 crupest-f775f4c76691fc61b2993d81d9f1b4690e76b141.zip |
No caddy, only nginx and certbot.
Diffstat (limited to 'tool/modules/configfile.py')
-rw-r--r-- | tool/modules/configfile.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tool/modules/configfile.py b/tool/modules/configfile.py new file mode 100644 index 0000000..49f2200 --- /dev/null +++ b/tool/modules/configfile.py @@ -0,0 +1,45 @@ +import os.path +from .path import config_file_path + +config_file_exist = os.path.isfile(config_file_path) + + +def parse_config(str: str) -> dict: + config = {} + for line_number, line in enumerate(str.splitlines()): + # check if it's a comment + if line.startswith("#"): + continue + # check if there is a '=' + if line.find("=") == -1: + raise ValueError( + f"Invalid config string. Please check line {line_number + 1}. There is even no '='!") + # split at first '=' + key, value = line.split("=", 1) + key = key.strip() + value = value.strip() + config[key] = value + return config + + +def get_domain() -> str: + if not config_file_exist: + raise ValueError("Config file not found!") + with open(config_file_path) as f: + config = parse_config(f.read()) + if "CRUPEST_DOMAIN" not in config: + raise ValueError("Domain not found in config file!") + return config["CRUPEST_DOMAIN"] + + +def config_to_str(config: dict) -> str: + return "\n".join([f"{key}={value}" for key, value in config.items()]) + + +def print_config(console, config: dict) -> None: + for key, value in config.items(): + console.print(f"[magenta]{key}[/] = [cyan]{value}") + + +__all__ = ["config_file_exist", "parse_config", + "get_domain", "config_to_str", "print_config"] |