]> git.armaanb.net Git - lightcards.git/blob - lightcards/config.py
Add preliminary config file support
[lightcards.git] / lightcards / config.py
1 # Parse lightcards config file
2 # Armaan Bhojwani 2021
3
4 import sys
5 from pathlib import Path
6 import os
7
8
9 class ConfigException(BaseException):
10     def __init__(self, message):
11         self.message = message
12         print(f"lightcards: {self.message}")
13         sys.exit(4)
14
15
16 def find_file(file):
17     file = str(file)
18     local_xdg = f"{os.path.expanduser('~')}/{os.environ.get('XDG_CACHE_HOME')}/lightcards/config.py"
19     local = f"{os.path.expanduser('~')}/.config/lightcards/config.py"
20     world = "/etc/lightcards/config.py"
21     world_default = "/usr/share/doc/lightcards/config.py"
22     local_default = (
23         f"{os.path.expanduser('~')}/.local/share/doc/lightcards/config.py"
24     )
25
26     if os.path.exists(file):
27         return file
28     elif os.path.exists(local_xdg):
29         return local_xdg
30     elif os.path.exists(local):
31         return local
32     elif os.path.exists(world):
33         return world
34     elif os.path.exists(world_default):
35         return world_default
36     elif os.path.exists(local_default):
37         return local_default
38
39
40 def read_file(file):
41     config = {}
42     exec(Path(str(find_file(file))).read_text(), {}, config)
43
44     return config