| |
1 # |
| |
2 # Borgend configuration loader |
| |
3 # |
| |
4 |
| |
5 import yaml |
| |
6 import io |
| |
7 import os |
| |
8 import xdg |
| |
9 import string |
| |
10 |
| |
11 |
| |
12 def expand_env(cfg, env): |
| |
13 if isinstance(cfg, dict): |
| |
14 out={key: expand_env(val, env) for key, val in cfg.items()} |
| |
15 elif isinstance(cfg, list): |
| |
16 out=[expand_env(val, env) for val in cfg] |
| |
17 elif isinstance(cfg, str): |
| |
18 out=string.Template(cfg).substitute(os.environ) |
| |
19 else: |
| |
20 out=cfg |
| |
21 |
| |
22 return out |
| |
23 |
| |
24 cfgfile=os.path.join(xdg.XDG_CONFIG_HOME, "borgend", "config.yaml") |
| |
25 |
| |
26 if not (os.path.exists(cfgfile) and os.path.isfile(cfgfile)): |
| |
27 raise SystemExit(f'Configuration file required: {cfgfile}') |
| |
28 |
| |
29 with io.open(cfgfile, 'r') as file: |
| |
30 settings=expand_env(yaml.load(file), os.environ); |