python 利用模板文件生成配置文件

gen.py:


__author__ = 'fuhan'

from jinja2 import Template


a={'name':'a'}

b={'name':'b'}


mode_dict = {

    'a':a,

    'b':b

}


def gen_config(tplt_file, mode='a'):

    with open(tplt_file, 'r') as r:

        tplt = Template(r.read())


    config = mode_dict[mode]

    res = tplt.render(config=config)

    save_file = tplt_file.replace('.temp','')

    with open(save_file, 'w') as w:

        w.write(res)


gen_config('setting.py.temp')



setting.py.temp:

name={{config.name}}


你可能感兴趣的:(python 利用模板文件生成配置文件)