Python做代码生成器

浏览量:6121 | 分类:python | 发布日期:2015-01-21

在工作中一些比较常用的代码逻辑比如 像DAO层,常常copy项目代码
然后删除没有用的代码,这样做有可能会没处理干净
用python生成代码模板其它语言也可以,这样就能得到一个纯净的代码
工具开发起来很简单,能加快工作效率
下面以JS模板为例,代码如下

注意这个方法getopt.getopt(args, options[, long_options])

第二个参数是接收短命令 ":"号是接收参数

第二个参数是接收长命令 "="号是接收参数

运行命令如下:

sudo python make.py -m comp.gotest model.ui.dialog model.ui.page common.channel.shop common.channel.trans.interface

#!/usr/bin/env python
#coding=utf-8
#filaname:make.py
#生成comp model by @Fzxa-

import sys
import os
import getopt
import time
from string import Template

class MakeModel:

	def __init__(self):

		self.name = 'wangzhen5'

		argv = sys.argv
		try:
			opts, args = getopt.getopt(argv[1:], 'vm:', ["version","make="])
		except getopt.GetoptError:
			self.usage()
			sys.exit(2)

		if len(opts) == 0:
			self.usage()
			sys.exit()
		
		for opt,arg in opts:
			if opt in ('-v', '--version'):
				self.version()
				sys.exit(1)
			elif opt in ('-m', '--make'):
				self.makeModel(arg,args)
				sys.exit(0)
			else:
				print 'Unhandled options'
				sys.exit(3);


	def usage(self):
		print 'STK make model usage:'
		print '-v,--version: print script version'
		print '-m,--make: make javascript file'

	def version(self):
		print 'STK makemodel.py 0.0.1'

	def makeNotes(self):
		st = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
		notes = ''
		notes += '/**\n'
		notes += ' *\n'
		notes += ' * @author '+self.name+'@xxx.com\n'
		notes += ' * @date '+st+'\n'
		notes += ' * @example \n'
		notes += ' */\n\n'
		return notes

	def modelTpl(self,strModelName,importModelName):

		tpl = ''
		tpl += self.makeNotes()
		if importModelName:
			for m in importModelName:
				importModel =('$Import(\''+m+'\');\n')
				tpl += importModel
		
		tpl += '\nX.register("{MODELNAME}", function($){\n\n'
		tpl += '	return function(node,opts){\n\n'
		tpl += '		var that = {};\n\n' 
		tpl += '		//自定义变量区域 \n' 
		tpl += '		var nodes;\n\n' 
		tpl += '		//参数验证 \n' 
		tpl += '		var argsCheck = function(){};\n\n'
		tpl += '		//DOM获取方法 \n' 
		tpl += '		var parseDOM = function(){};\n\n'
		tpl += '		//模块初始化方法 \n' 
		tpl += '		var initPlugins = function(){};\n\n'
		tpl += '		//DOM事件绑定方法 \n' 
		tpl += '		var bindDOMFuns = {};\n\n'
		tpl += '		//自定义变量区域 \n' 
		tpl += '		var bindCustEvtFuns = {};\n\n'
		tpl += '		//广播事件方法 \n' 
		tpl += '		var bindListenerFuns = {};\n\n'
		tpl += '		//组件销毁方法 \n' 
		tpl += '		var destory = function(){};\n\n'
		tpl += '		//初始化执行 \n' 
		tpl += '		var init = function(){\n'
		tpl += '			rgsCheck();\n'
		tpl += '			parseDOM();\n'
		tpl += '			initPlugins();\n'
		tpl += '			bindDOMFuns();\n'
		tpl += '			bindCustEvtFuns();\n'
		tpl += '			bindListenerFuns();\n'
		tpl += '		};\n\n'
		tpl += '		init();\n\n'
		tpl += '		that.destory && destory;\n\n'
		tpl += '		return that;\n\n'
		tpl += '	}\n\n'
		tpl += '});'
		tpl = tpl.replace("{MODELNAME}",strModelName)
		return tpl

	def checkFileExists(self,fileName):
		fn = fileName+'.js'
		if os.path.exists(fn):
			print 'File already exists, please check file and try again.'
			sys.exit()


	def makeModel(self,strModelName, importModelName):
		arrModelName = strModelName.split(".");
		fileName = arrModelName[1]
		self.checkFileExists(fileName)
		print 'Make \''+fileName+'.js\' warting...'
		temp = self.modelTpl(strModelName,importModelName)
		f = open(fileName+'.js','w')
		f.write(temp)
		f.close()
		print 'ok'


MakeModel()

上一篇: Nginx和Tomcat网站共存

下一篇: UITableView Group

评论

过客   2018-11-08 19:23:05

过客   2018-11-08 19:22:58

过客   2017-05-10 01:00:42

过客   2016-10-04 23:51:19

过客   2016-09-23 02:57:22

过客   2016-09-07 23:09:20