def yaml_load(filepath, **updates):
"""Loads a yaml file producing the corresponding Python dict (`safe_load`). Then:
1. normalizes non-absolute sqlite path values relative to `filepath`, if any
2. updates the dict values with `updqtes`
and returns the yaml dict.
:param updates: arguments which will updates the yaml dict before it is returned
"""
with open(filepath, 'r') as stream:
ret = yaml.safe_load(stream)
# convert sqlite into absolute paths, if any
configfilepath = abspath(dirname(filepath))
# convert relative sqlite path to absolute, assuming they are relative to the config:
sqlite_prefix = 'sqlite:///'
# we cannot modify a dict while in iteration, thus create a new dict of possibly
# modified sqlite paths and use later dict.update
newdict = {}
for k, v in viewitems(ret):
try:
if v.startswith(sqlite_prefix) and ":memory:" not in v:
dbpath = v[len(sqlite_prefix):]
if not isabs(dbpath):
newdict[k] = sqlite_prefix + abspath(normpath(join(configfilepath, dbpath)))
except AttributeError:
pass
newdict.update(updates)
ret.update(newdict)
return ret
评论列表
文章目录