码迷,mamicode.com
首页 > 编程语言 > 详细

[转]用Python做一个自动生成读表代码的小脚本

时间:2015-09-06 09:38:31      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

写在开始(本片文章不是写给小白的,至少你应该知道一些常识!)

大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码。非常烦。来个实用小工具,大家随便看看。

  1 #-*- coding: utf-8 -*-
  2 #----------------------------------------------------------#
  3 #   版本:python-3.5.0a3-amd64
  4 #   功能:生成读表代码文件
  5 #----------------------------------------------------------#
  6 import os
  7 import sys
  8 import re
  9 import string
 10 import codecs
 11 
 12 templateFileName = "./template.cs"
 13 outputPathName = "./"
 14 
 15 def InputFileName2RowClassName(_inputFileName):
 16     _className = ""
 17     _className = "Row_" + _inputFileName.split(".")[0]
 18     return _className
 19 
 20 def InputFileName2TableClassName(_inputFileName):
 21     _className = ""
 22     _className = "Table_" + _inputFileName.split(".")[0]
 23     return _className
 24 
 25 def InputFileName2FileName(_inputFileName):
 26     _className = ""
 27     _className = _inputFileName.split(".")[0]
 28     return _className
 29 
 30 def GenerateRowFields(_type,_name,_desc):
 31     _returnValue = ""
 32     _returnValue += "\t//" + _desc + "\n"
 33     if _type.find("FLOAT")!=-1:
 34         _returnValue += "\tprivate float m_float" + _name + ";\n"
 35         _returnValue += "\tpublic float " + _name + "{get{return m_float" + _name + ";}}\n"
 36     if _type.find("STRING")!=-1:
 37         _returnValue += "\tprivate string m_string" + _name + ";\n"
 38         _returnValue += "\tpublic string " + _name + "{get{return m_string" + _name + ";}}\n"
 39     if _type.find("INT")!=-1:
 40         _returnValue += "\tprivate int m_int" + _name + ";\n"
 41         _returnValue += "\tpublic int " + _name + "{get{return m_int" + _name + ";}}\n"
 42     return _returnValue
 43 
 44 def GenerateRowFunction(_type,_name,_desc,_i):
 45     _returnValue = ""
 46     if _type.find("FLOAT")!=-1:
 47         _returnValue = "\t\t\tm_float" + _name + " = float.Parse(strCols[i++]);\n"
 48     if _type.find("STRING")!=-1:
 49         _returnValue = "\t\t\tm_string" + _name + " = strCols[i++];\n"
 50     if _type.find("INT")!=-1:
 51         _returnValue = "\t\t\tm_int" + _name + " = Convert.ToInt32(strCols[i++]);\n"
 52     #print(_returnValue)
 53     return _returnValue
 54 
 55 def GenerateFileds(_inputfilename):
 56     outputText = "";
 57     lineNum = 0
 58     listTypes = []
 59     listNames = []
 60     listDescs = []
 61     textFile = open( _inputfilename, "r", encoding= utf-8 )
 62     for line in textFile.readlines():
 63         lineNum = lineNum + 1
 64         #只读前三行
 65         if lineNum <= 3:
 66             #第一行类型
 67             if lineNum == 1:
 68                 listTypes = line.strip().split("\t")
 69             #第二行名称
 70             if lineNum == 2:
 71                 listNames = line.strip().split("\t")
 72             #第三行注释
 73             if lineNum == 3:
 74                 listDescs = line.strip().split("\t")
 75     textFile.close()
 76     for i in range( 0, len(listTypes) ):
 77         outputText += GenerateRowFields(listTypes[i],listNames[i],listDescs[i])
 78     return outputText
 79 
 80 def GenerateFromText(_inputfilename):
 81     outputText = "";
 82     lineNum = 0
 83     listTypes = []
 84     listNames = []
 85     listDescs = []
 86     textFile = open( _inputfilename, "r", encoding= utf-8 )
 87     for line in textFile.readlines():
 88         lineNum = lineNum + 1
 89         #只读前三行
 90         if lineNum <= 3:
 91             #第一行类型
 92             if lineNum == 1:
 93                 listTypes = line.strip().split("\t")
 94             #第二行名称
 95             if lineNum == 2:
 96                 listNames = line.strip().split("\t")
 97             #第三行注释
 98             if lineNum == 3:
 99                 listDescs = line.strip().split("\t")
100     textFile.close()
101     for j in range( 0, len(listTypes) ):
102         #print(listTypes[j],listNames[j],listDescs[j],j)
103         outputText += GenerateRowFunction(listTypes[j],listNames[j],listDescs[j],j)
104     return outputText
105 
106 #根据TXT生成客户端代码文件
107 def GenarateCodeByText(_inputfilename):
108     #templateFileName = "template.cs"
109     templateFile = open( templateFileName, "r" )
110     templateText = templateFile.read()
111     templateText = templateText.replace( "#{RowClass}", InputFileName2RowClassName(_inputfilename) )
112     templateText = templateText.replace( "#{Fileds}", GenerateFileds(_inputfilename) )
113     templateText = templateText.replace( "#{FromText}", GenerateFromText(_inputfilename) )
114     templateText = templateText.replace( "#{TableClass}", InputFileName2TableClassName(_inputfilename) )
115     templateText = templateText.replace( "#{FileName}", InputFileName2FileName(_inputfilename) )
116     templateFile.close()
117     return templateText
118 
119 #自动写文件
120 def AutoWriteToFile(_inputfilename):
121     outputFileName = outputPathName + InputFileName2TableClassName(_inputfilename) + ".cs"
122     outputText = GenarateCodeByText(_inputfilename).encode("utf-8")
123     outputFile = open( outputFileName, "wb" )
124     outputFile.write(outputText)
125     outputFile.close()
126 
127 #获取文件夹下所有文件路径
128 def walk(path):
129     f1 = os.listdir(path)
130     for f in f1:
131         #如果是子目录,则递归遍历
132         if f.find(".svn") == -1 and os.path.isdir(os.path.join(path,f)):
133             #walk(os.path.join(path,f))
134             None
135         else:
136             if f.find(".txt") != -1:
137                 t_name = os.path.join(path,f)
138                 listfilepath.append(t_name)
139     return listfilepath
140 
141 
142 listfilepath = []
143 
144 if __name__ == __main__:
145     if len(sys.argv) == 1:
146         listfilepath = walk("./")
147         print(len(listfilepath))
148         for files in listfilepath:
149             files = files.split("/")[-1]
150             print(files)
151             AutoWriteToFile(files)
152     sys.exit(0)

 

伸手党请看下面,大神们误喷,请绕行。

code

[转]用Python做一个自动生成读表代码的小脚本

标签:

原文地址:http://www.cnblogs.com/PrideTse/p/4784427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!