对python实现模板生成脚本的方法详解
最近项目需要,针对主项目提取一个小的基础版本,供于在新建项目时使用,所以就有这个python模板生成脚本,其作用如下:
1、通过配置文件来控制模板中的数据、格式化的过滤条件
2、执行后会把目录下所有的文件都会执行一篇
#!/usr/bin/python #encoding:utf-8 importjson importcodecs importos defget_files(root_path): fordirinos.walk(root_path): ifdir[2]: fornfindir[2]: yieldos.path.join(dir[0],nf) defexclude_filter(exclude,nfile): files_path=exclude.get('file_path') files_name=exclude.get('file_name') base_name=os.path.basename(nfile) exts_name=exclude.get('ext_name') base_ext_name=base_name.rsplit(".",1)[1] iffiles_path: fornpathinfiles_path: ifnpath==nfile: returnTrue eliffiles_name: fornameinfiles_name: printname,base_name ifname==base_name: returnTrue elifexts_name: fornameinexts_name: printname,base_ext_name ifname==base_ext_name: returnTrue definclude_filter(include,nfile): files_path=include.get('file_path') files_name=include.get('file_name') base_name=os.path.basename(nfile) iffiles_path: fornpathinfiles_path: ifnpath==nfile: returnTrue eliffiles_name: fornameinfiles_name: ifname==base_name: returnTrue defmain(): #readconfig config={} withcodecs.open("config.json","rb","UTF-8")asf: config=json.loads(f.read()) ifnotconfig: return template=config.get("template") iftemplateandtemplate.get('path'): root_path=template.get('path') ifnotos.path.exists(root_path): print"sourcepathnotexist" return root_path=os.path.abspath(root_path) old_path=os.path.dirname(root_path) else: return exclude=template.get('exclude') include=template.get('include') store=config.get("store") ifnotstoreornotos.path.exists(store.get('dir_path','')): return data=config.get("data") ifnotdata: return ifnotos.path.exists(root_path): print'rootpathnotexists' return ifos.path.isfile(root_path): files=[root_path] else: base_name=os.path.basename(root_path) store_root_path=os.path.join(store.get('dir_path'),base_name) ifnotos.path.exists(store_root_path): os.mkdir(store_root_path) files=get_files(root_path) fornfileinfiles: printnfile try: withcodecs.open(nfile,"rb","UTF-8")asf: s=f.read() ifnotexclude_filter(exclude,nfile)orinclude_filter(include,nfile): s=s%data except: withcodecs.open(nfile,"rb")asf: s=f.read() #savetofile fn=nfile.replace(old_path,store.get('dir_path')) fn_dir=os.path.dirname(fn) ifnotos.path.exists(fn_dir): os.makedirs(fn_dir) try: withcodecs.open(fn,"wb","UTF-8")asf: f.write(s) f.flush() except: withcodecs.open(fn,"wb")asf: f.write(s) f.flush() if__name__=='__main__': main()
配置文件:
{ "template":{ "path":"D:/tunicorn-web/framework-template",##模板文件主目录 "exclude":{##不进行模板格式化的文件 "file_path":[], "file_name":["config.json","make_project.py"], "ext_name":["css","woff2"], "file_type":[], "regex":[] }, "include":{##进行模板格式化的文件 "file_path":[], "file_name":[] } }, "store":{ "dir_path":"e:/test"##输出路径主目录 "data":{ "project_name":"NewJAVA",##模板数据 "project_prefix":"newjava"##模板数据 } }
执行操作:
1、安装了python环境
2、双击python脚本
3、然后在执行下README中的步骤
readme:
README
=============
脚本使用
-------------
1.打开config.json文件
2.配置相关信息[输出目录、项目名称、项目前缀]
3.执行make_project.py脚本
4.查看输出目录
以上这篇对python实现模板生成脚本的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。