模板生成器

import os

import datetime

from string import  Template

tplFilePath = './h.java'

path = './m.java'

testObjList = ['Basic_connect',\

'Sms',\

'Phonebook',\

]

for testObj in testObjList:

testObjVarName = testObj[0].lower() + testObj[1:]

filename = 'MBIM_Service_' + testObj +'_device.java'

author = 'Liubo'

version='V0.1'

now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

tplFile = open(tplFilePath)

gFile = open(path+filename ,"w")

#method 1 with Template and substitute(safe_substitute)

lines=[]

tmp=Template(tplFile.read())

lines.append(tmp.substitute(

author = author,

now = now,

testObject = testObj,

testObjVarName = testObjVarName,

version = version))

gFile.writelines(lines)

'''

#Method 2, with replace

fileList = tplFile.readlines()

for fileLine in fileList:

line = fileLine.replace('${author}',author)\

.replace('${now}',now)\

.replace('${testObject}',testObj)\

.replace('${version}',version)\

.replace('${testObjVarName}',testObjVarName)

print line

gFile.writelines(line)

'''

tplFile.close()

gFile.close()

print 'generate %s over. ~ ~' % (path+filename)

你可能感兴趣的:(模板生成器)