]> git.armaanb.net Git - lightcards.git/blob - lightcards/config.py
Allow for config files to be incomplete
[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 read_file(file):
17     config = {}
18     file = str(file)
19     files = []
20     local_xdg = f"{os.path.expanduser('~')}/{os.environ.get('XDG_CACHE_HOME')}/lightcards/config.py"
21     local = f"{os.path.expanduser('~')}/.config/lightcards/config.py"
22     world = "/etc/lightcards/config.py"
23     world_default = "/usr/share/doc/lightcards/config.py"
24     local_default = (
25         f"{os.path.expanduser('~')}/.local/share/doc/lightcards/config.py"
26     )
27
28     if os.path.exists(world_default):
29         files.append(world_default)
30     if os.path.exists(local_default):
31         files.append(local_default)
32     if os.path.exists(world):
33         files.append(world)
34     if os.path.exists(local_xdg):
35         files.append(local_xdg)
36     if os.path.exists(local):
37         files.append(local)
38     if os.path.exists(file):
39         files.append(file)
40
41     for f in files:
42         exec(Path(str(f)).read_text(), {}, config)
43
44     return config