template.py 文件源码

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

项目:gopythongo 作者: gopythongo 项目源码 文件源码
def process_to_tempfile(filepath: str, context: Dict[str, Any]) -> str:
    """
    renders the template in ``filepath`` using ``context`` through Jinja2. The result
    is saved into a temporary file, which will be garbage collected automatically when the
    program exits.

    :return: the full path of the temporary file containing the result
    """
    outfd, ofname = tempfile.mkstemp()
    with open(outfd, mode="w") as outf:
        with open(filepath) as inf:
            tplstr = inf.read()
        tpl = jinja2.Template(tplstr)
        outf.write(tpl.render(context))

    import gopythongo.main
    gopythongo.main.tempfiles.append(ofname)
    return ofname
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号