__init__.py 文件源码

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

项目:fensterbrief 作者: nitram2342 项目源码 文件源码
def init_templates(config_file):

    config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    config.read(config_file)
    template_dir = config.get('DEFAULT', 'TEMPLATE_DIR')

    texmf_dir =  os.path.expanduser('~/texmf/tex/latex/fensterbrief/')

    # check if template directory exists
    if not os.path.exists(template_dir):
        answer = input("+ Shall directory %s be created? " % template_dir).lower()
        if 'y' in answer:
            os.makedirs(template_dir)
        else:
            return

    # create user's 'texmf' directory
    if not os.path.exists(texmf_dir):
        answer = input("+ Shall directory %s be created? " % texmf_dir).lower()
        if 'y' in answer:
            os.makedirs(texmf_dir)
        else:
            return

    # copy templates to tempalte directory
    for res_name in resource_listdir('templates', ''):
        if res_name.endswith(".tex") or res_name.endswith(".md") or res_name.endswith(".lco") or res_name.endswith(".sty"):
            src_fd = resource_stream('templates', res_name)            

            if res_name.endswith(".tex") or res_name.endswith(".md"):
                dst_file = os.path.join(template_dir, res_name)
            else:
                dst_file = os.path.join(texmf_dir, res_name)

            print("+ Copy resource file to %s" % dst_file)

            write_file = False
            if os.path.exists(dst_file):
                answer = input("+ Shall %s be overwritten? " % dst_file).lower()
                if 'y' in answer:
                    write_file = True
            else:
                write_file = True

            if write_file:
                with open(dst_file, 'wb') as dst_fd:                
                    shutil.copyfileobj(src_fd, dst_fd)

    # update
    fensterbrief.run_program('texhash')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号