1 #-*- coding: utf-8 -*-
2 #----------------------------------------------------------#
3 #版本:python-3.5.0a3-amd64
4 #功能:生成读表代码文件
5 #----------------------------------------------------------#
6 importos7 importsys8 importre9 importstring10 importcodecs11
12 templateFileName = "./template.cs"
13 outputPathName = "./"
14
15 defInputFileName2RowClassName(_inputFileName):16 _className = ""
17 _className = "Row_" + _inputFileName.split(".")[0]18 return_className19
20 defInputFileName2TableClassName(_inputFileName):21 _className = ""
22 _className = "Table_" + _inputFileName.split(".")[0]23 return_className24
25 defInputFileName2FileName(_inputFileName):26 _className = ""
27 _className = _inputFileName.split(".")[0]28 return_className29
30 defGenerateRowFields(_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_returnValue43
44 defGenerateRowFunction(_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_returnValue54
55 defGenerateFileds(_inputfilename):56 outputText = "";57 lineNum =058 listTypes =[]59 listNames =[]60 listDescs =[]61 textFile = open( _inputfilename, "r", encoding= 'utf-8')62 for line intextFile.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 inrange( 0, len(listTypes) ):77 outputText +=GenerateRowFields(listTypes[i],listNames[i],listDescs[i])78 returnoutputText79
80 defGenerateFromText(_inputfilename):81 outputText = "";82 lineNum =083 listTypes =[]84 listNames =[]85 listDescs =[]86 textFile = open( _inputfilename, "r", encoding= 'utf-8')87 for line intextFile.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 inrange( 0, len(listTypes) ):102 #print(listTypes[j],listNames[j],listDescs[j],j)
103 outputText +=GenerateRowFunction(listTypes[j],listNames[j],listDescs[j],j)104 returnoutputText105
106 #根据TXT生成客户端代码文件
107 defGenarateCodeByText(_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 returntemplateText118
119 #自动写文件
120 defAutoWriteToFile(_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 defwalk(path):129 f1 =os.listdir(path)130 for f inf1:131 #如果是子目录,则递归遍历
132 if f.find(".svn") == -1 andos.path.isdir(os.path.join(path,f)):133 #walk(os.path.join(path,f))
134 None135 else:136 if f.find(".txt") != -1:137 t_name =os.path.join(path,f)138 listfilepath.append(t_name)139 returnlistfilepath140
141
142 listfilepath =[]143
144 if __name__ == '__main__':145 if len(sys.argv) == 1:146 listfilepath = walk("./")147 print(len(listfilepath))148 for files inlistfilepath:149 files = files.split("/")[-1]150 print(files)151 AutoWriteToFile(files)152 sys.exit(0)