[上一篇链接]
https://blog.csdn.net/liuyongjie1992/article/details/117325176
这一步过程比较复杂,代码中涉及到多次数据构建,先简单说思路
本篇文章的核心代码是export_code.py文件,调用位置为:
下面贴上export_code.py文件的全部代码
#! python2
# coding=utf-8
import traceback
import os
import sys
import codecs
import unicodecsv
import setting
import Parser
import debug
m_code_template = u'''
#! python2
#coding:utf-8
import os
import traceback
import sys
sys.path.insert(0, os.path.abspath('./TableCreater/Script'))
import Parser
import setting
import debug
sys.path.insert(0, os.path.abspath(setting.dirPath_pb_pythonEnum))
sys.path.insert(0, os.path.abspath(setting.dirPath_pb_python))
import Table_{}_pb2
def export():
tableData = Table_{}_pb2.Table_{}()
BYTES_PATH = {} +"{}.bytes"
{}
bytes = tableData.SerializeToString()
NEWFILE = open(BYTES_PATH,"wb")
NEWFILE.write(bytes)
NEWFILE.close()
try:
export()
except:
debug.error(traceback.format_exc())'''
def getStructArrayContentInfo(cell):
cellDatas = []
arrayCell = ""
begin = False
for _char in cell:
if(begin):
if(_char == ']'):
begin = False
cellDatas.append(arrayCell.split('*'))
arrayCell = ""
else:
arrayCell = arrayCell + _char
if(_char == '['):
begin = True
return cellDatas
def createcode_baseTypeArray(cell,dataType,title):
code = u""
dataType = dataType[0:-2]
children = cell.split('*')
for i in range(0,len(children)):
child = children[i]
code = code + u"\tParser.Fill_Array(data.{},u\"{}\",u\"{}\",{})\n".format(title,dataType,child,i)
return code
def createcode_struct(cell,dataType,title,tableName):
#struct_testArray = Table_Test_pb2.testArray()
structInfo,structName = Parser.GetStructInfo(dataType)
code = u"" #"\tdata.{} = Table_{}_pb2.{}()\n".format(title,tableName,structName)
children = cell.split('*')
for i in range(0,len(children)):
child = children[i]
if(i < len(structInfo)):
code = code + u"\tdata.{}.{} = Parser.GetValue(u\"{}\",u\"\"\"{}\"\"\")\n".format(title,structInfo[i][0],structInfo[i][1],child)
return code
def createcode_structArray(cell,dataType,title):
code = u""
structInfo,structName = Parser.GetStructInfo(dataType[:-2])
cellInfo = getStructArrayContentInfo(cell)
for arrayCellData in cellInfo:
# cell_testArray = data.testArray.add()
code = code + u"\tcell_{} = data.{}.add()\n".format(title,title)
for i in range(0,len(arrayCellData)):
childData = arrayCellData[i]
childTitle = structInfo[i][0]
childType = structInfo[i][1]
# cell_testArray.id = Parser.GetValue(u"INT",u"102")
code = code + u"\tcell_{}.{} = Parser.GetValue(u\"{}\",u\"\"\"{}\"\"\")\n".format(title,childTitle,childType,childData)
return code
def createcode_col(cell,dataType,title,tableName):
code = u""
if(cell != u""):
if(Parser.IsArray(dataType)):
if(Parser.IsStructArray(dataType)):
#print("结构体数组")
code = code + createcode_structArray(cell,dataType,title)
else:
#print("基本数据类型数组")
code = code + createcode_baseTypeArray(cell,dataType,title)
else:
if(Parser.IsStructArray(dataType)):
code = code + createcode_struct(cell,dataType,title,tableName)
else:
if dataType == u'JSON':
dataType = u'STRING'
if dataType == u'STRING':
cell = cell.replace('\"','\\"')
#基本数据类型赋值
code = code + u"\tdata.{} = Parser.GetValue(u\"{}\",u\"\"\"{}\"\"\")\n".format(title,dataType,cell)
return code
def createcode_row(line,types,titles,userSigns,tableName):
code = u"\tdata = tableData.datas.add()\n"
for i in range(0,len(line)):
# if(i>=len(userSigns)):
# debug.error("11111")
if(Parser.CanUse(userSigns[i])):
code = code + createcode_col(line[i],types[i],Parser.GetTitle(titles[i]),tableName)
return code
#合并XML
def execute(code_csvs):
for csvName in code_csvs:
csvName_splits = csvName.split(".")
if(len(csvName_splits) > 0):
tableName = csvName_splits[0]
curCsvPath = setting.dirPath_csv + csvName + ".csv"
#print(curCsvPath)
csvfile = codecs.open(curCsvPath,"rb")
csv_reader = unicodecsv.reader(csvfile,encoding='utf-8-sig')
types = None
titles = None
userSigns = None
index = -1
parser_code = ""
user = ""
allRowStrs = []
for line in csv_reader:
index = index + 1
if(index == 0):
user = line[0]
elif(index == 1):
userSigns = line
elif(index == 2):
types = line
elif(index == 3):
titles = line
else:
code_row = createcode_row(line,types,titles,userSigns,tableName)
allRowStrs.append(code_row)
allRowStrs.append("\n")
# parser_code = parser_code + code_row + "\n"
# print(index)
#print("这里")
parser_code = ''.join(allRowStrs)
exportPath = "setting.dirPath_byte"
if user == "client":
exportPath = "setting.dirPath_byte_clientOnly"
code = m_code_template.format(tableName,tableName,tableName,exportPath,tableName,parser_code)
csvfile.close()
codefile = codecs.open(setting.dirPath_code + "Table_" + tableName + ".py","wb","utf-8-sig")
codefile.write(code)
codefile.close()
#入口函数
if __name__ == "__main__":
try:
Parser.curToolUser = "2"
execute(['IllegalData'])
except Exception,err:
debug.error(str(err))
代码有点长,直接讲核心部分:请同学们翻看excute函数,该函数先读取csv文件,然后抓取了一些数据,并把这些数据塞入m_code_template代码模板里,最后把这个代码模板写成了一个python文件,最终这个文件叫Table_LyItemDrop.py,请注意看m_code_template这个string里引用了上一篇中生成的import Table_{}_pb2文件,当然这个{}大括号就是等待我们把文件名塞入的,新生成的python文件里引用了另一个python文件,这是没有问题的。由于Table_LyItemDrop.py行数有两万多行,我就不插入代码段了,截图取而代之。
观察Table_LyItemDrop.py脚本的内容,可以看到前面import了一些脚本和模块,重点是export函数里包含了数很多重要数据,下面我们看该脚本20行-25行的data,
20行data = tableData.datas.add()添加一行数据,数据内容有哪些呢?
21行该列数据名为id,数据是reward_init
22行该列数据名为RandomType,数据是Fixed
剩下的我就不介绍了,直接上excel配表截图
通过对比Table_LyItemDrop.py的代码和excel配表数据,我们可以大胆联想!这个python脚本有大事要做!
本篇文章到此结束
Table_LyItemDrop.py脚本要干啥大事去下一篇文章解密
[下一篇链接]
https://blog.csdn.net/liuyongjie1992/article/details/117325259