aboutsummaryrefslogtreecommitdiff
path: root/tool/modules/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'tool/modules/config.py')
-rw-r--r--tool/modules/config.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/tool/modules/config.py b/tool/modules/config.py
index 37ad996..28b09a3 100644
--- a/tool/modules/config.py
+++ b/tool/modules/config.py
@@ -1,7 +1,8 @@
-from rich.prompt import Prompt
import pwd
import grp
import os
+from rich.prompt import Prompt
+from .path import config_file_path
class ConfigVar:
@@ -73,3 +74,44 @@ def check_config_var_set(needed_config_var_set: set):
if var_name not in needed_config_var_set:
less.append(var_name)
return (True if len(more) == 0 else False, more, less)
+
+
+def config_file_exists():
+ return 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_exists():
+ 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}")