config.py

Thu, 18 Jan 2018 21:42:00 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 18 Jan 2018 21:42:00 +0000
changeset 0
f5aecaad0bcf
child 1
4cdc9c1f6b28
permissions
-rw-r--r--

Some rough drafting

#
# 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