config.py

changeset 0
f5aecaad0bcf
child 1
4cdc9c1f6b28
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.py	Thu Jan 18 21:42:00 2018 +0000
@@ -0,0 +1,30 @@
+#
+# Borgend configuration loader
+#
+
+import yaml
+import io
+import os
+import xdg
+import string
+
+
+def expand_env(cfg, env):
+    if isinstance(cfg, dict):
+        out={key: expand_env(val, env) for key, val in cfg.items()}
+    elif isinstance(cfg, list):
+        out=[expand_env(val, env) for val in cfg]
+    elif isinstance(cfg, str):
+        out=string.Template(cfg).substitute(os.environ)
+    else:
+        out=cfg    
+        
+    return out
+
+cfgfile=os.path.join(xdg.XDG_CONFIG_HOME, "borgend", "config.yaml")
+
+if not (os.path.exists(cfgfile) and os.path.isfile(cfgfile)):
+    raise SystemExit(f'Configuration file required: {cfgfile}')
+
+with io.open(cfgfile, 'r') as file:
+    settings=expand_env(yaml.load(file), os.environ);

mercurial