]> git.armaanb.net Git - lightcards.git/blobdiff - lightcards/config.py
Add preliminary config file support
[lightcards.git] / lightcards / config.py
diff --git a/lightcards/config.py b/lightcards/config.py
new file mode 100644 (file)
index 0000000..56f7261
--- /dev/null
@@ -0,0 +1,44 @@
+# Parse lightcards config file
+# Armaan Bhojwani 2021
+
+import sys
+from pathlib import Path
+import os
+
+
+class ConfigException(BaseException):
+    def __init__(self, message):
+        self.message = message
+        print(f"lightcards: {self.message}")
+        sys.exit(4)
+
+
+def find_file(file):
+    file = str(file)
+    local_xdg = f"{os.path.expanduser('~')}/{os.environ.get('XDG_CACHE_HOME')}/lightcards/config.py"
+    local = f"{os.path.expanduser('~')}/.config/lightcards/config.py"
+    world = "/etc/lightcards/config.py"
+    world_default = "/usr/share/doc/lightcards/config.py"
+    local_default = (
+        f"{os.path.expanduser('~')}/.local/share/doc/lightcards/config.py"
+    )
+
+    if os.path.exists(file):
+        return file
+    elif os.path.exists(local_xdg):
+        return local_xdg
+    elif os.path.exists(local):
+        return local
+    elif os.path.exists(world):
+        return world
+    elif os.path.exists(world_default):
+        return world_default
+    elif os.path.exists(local_default):
+        return local_default
+
+
+def read_file(file):
+    config = {}
+    exec(Path(str(find_file(file))).read_text(), {}, config)
+
+    return config