這篇文章給大家分享的是有關(guān)如何使用python實(shí)現(xiàn)模板生成腳本的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

最近項(xiàng)目需要,針對(duì)主項(xiàng)目提取一個(gè)小的基礎(chǔ)版本,供于在新建項(xiàng)目時(shí)使用,所以就有這個(gè)python模板生成腳本,其作用如下:
1、通過配置文件來控制模板中的數(shù)據(jù)、格式化的過濾條件
2、執(zhí)行后會(huì)把目錄下所有的文件都會(huì)執(zhí)行一篇
#!/usr/bin/python
#encoding: utf-8
import json
import codecs
import os
def get_files(root_path):
for dir in os.walk(root_path):
if dir[2]:
for nf in dir[2]:
yield os.path.join(dir[0], nf)
def exclude_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]
if files_path:
for npath in files_path:
if npath==nfile:
return True
elif files_name:
for name in files_name:
print name, base_name
if name==base_name:
return True
elif exts_name:
for name in exts_name:
print name, base_ext_name
if name==base_ext_name:
return True
def include_filter(include, nfile):
files_path = include.get('file_path')
files_name = include.get('file_name')
base_name = os.path.basename(nfile)
if files_path:
for npath in files_path:
if npath==nfile:
return True
elif files_name:
for name in files_name:
if name==base_name:
return True
def main():
# read config
config = {}
with codecs.open("config.json","rb","UTF-8") as f:
config = json.loads(f.read())
if not config:
return
template = config.get("template")
if template and template.get('path'):
root_path = template.get('path')
if not os.path.exists(root_path):
print "source path not exist"
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")
if not store or not os.path.exists(store.get('dir_path', '')):
return
data = config.get("data")
if not data:
return
if not os.path.exists(root_path):
print 'root path not exists'
return
if os.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)
if not os.path.exists(store_root_path):
os.mkdir(store_root_path)
files = get_files(root_path)
for nfile in files:
print nfile
try:
with codecs.open(nfile, "rb", "UTF-8") as f:
s = f.read()
if not exclude_filter(exclude, nfile) or include_filter(include, nfile):
s = s % data
except:
with codecs.open(nfile, "rb") as f:
s = f.read()
# save to file
fn = nfile.replace(old_path, store.get('dir_path'))
fn_dir = os.path.dirname(fn)
if not os.path.exists(fn_dir):
os.makedirs(fn_dir)
try:
with codecs.open(fn, "wb", "UTF-8") as f:
f.write(s)
f.flush()
except:
with codecs.open(fn, "wb") as f:
f.write(s)
f.flush()
if __name__ == '__main__':
main()配置文件:
{
"template": {
"path" : "D:/tunicorn-web/framework-template", ##模板文件主目錄
"exclude" : { ##不進(jìn)行模板格式化的文件
"file_path" : [],
"file_name" : ["config.json", "make_project.py"],
"ext_name" : ["css", "woff2"],
"file_type" : [],
"regex" : []
},
"include" : { ##進(jìn)行模板格式化的文件
"file_path" : [],
"file_name" : []
}
},
"store":{
"dir_path" : "e:/test" ##輸出路徑主目錄
"data" : {
"project_name":"NewJAVA", ##模板數(shù)據(jù)
"project_prefix":"newjava" ##模板數(shù)據(jù)
}
}執(zhí)行操作:
1、安裝了python環(huán)境
2、雙擊python腳本
3、然后在執(zhí)行下README中的步驟
readme:
README
=============
腳本使用
-------------
1. 打開config.json文件
2. 配置相關(guān)信息[輸出目錄、項(xiàng)目名稱、項(xiàng)目前綴]
3. 執(zhí)行make_project.py腳本
4. 查看輸出目錄
感謝各位的閱讀!關(guān)于“如何使用python實(shí)現(xiàn)模板生成腳本”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文題目:如何使用python實(shí)現(xiàn)模板生成腳本-創(chuàng)新互聯(lián)
URL標(biāo)題:http://chinadenli.net/article10/cepedo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、面包屑導(dǎo)航、微信小程序、搜索引擎優(yōu)化、ChatGPT、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容