resources.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:stream2segment 作者: rizac 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号