python脚本转lua_初次使用Python脚本,proto协议文件转Lua代码
使用IntelliJ IDEA編輯器編寫Lua腳本的時候,安裝一個插件 "EmmyLua" 可以對有代碼提示功能
想把protoBuf協(xié)議文件轉(zhuǎn)成Lua代碼,然后給出代碼提示
第一次使用python腳本
使用到的幾個功能記錄一下
1.設(shè)置為文件格式備注為utf-8
#!/usr/bin/python
# -*- coding: UTF-8 -*-
2.定義一個方法,可以返回多個值
#--------------------------------------------
# 替換字符串方法
#--------------------------------------------
#define a function
def ReplaceStr(s):
#TODO ... (自己的邏輯)
return s
#備注:方法可以返回多個值
def MyFunc(s):
local s1 = "我是s1"
return s,s1
#調(diào)用時,用兩個變量接
rtS,rtS1 = MyFunc(str)
2.替換字符串
s = s.replace('=', ' '); #先把所有的'='替換成'空格'
3.用正則替換任意個數(shù)的'空格'換成'-'
s = re.sub(r'\s+', '-', s); #把任意多個'空格'替換成'-'
4.把字符串分割成數(shù)組
arrSplit = s.split('-') #把"1-2-3" 分割成 [1,2,3]
5.邏輯判斷 if-else 和 與或非
typeValue = ""
if s == "int32":
typeValue = "0"
elif s == "int64":
typeValue = "0"
else:
typeValue = "{}"
# 判斷符號
# 等于'=='
6.把字符串分割成數(shù)組
arrSplit = s.split('-') #把"1-2-3" 分割成 [1,2,3]
7.讀文件
fileRead = open("client2server.proto") # 返回一個文件對象
line = fileRead.readline() # 調(diào)用文件的 readline()方法
while line:
print line
# 讀取下一行,while循環(huán),直到讀完最后一行
line = fileRead.readline()
8.寫文件
fileWrite = open('LuaProto.lua', 'w')
fileWrite.write(allStr)
fileWrite.close( )
print "文件保存成功,當(dāng)前目錄下的LuaProto.lua"
以下是代碼
Proto文件
// msgType = 1
// 客戶端 -> 服務(wù)器
// 發(fā)送登錄消息,包括賬號和密碼
message Login {
required string user = 1;
required string pwd = 2;
}
message LoginRt {
required int32 rt = 1;
required string token = 2;
}
python腳本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
#--------------------------------------------
# 替換字符串方法
#--------------------------------------------
#define a function
def ReplaceStr(s):
s = s.replace('=', ' '); #先把所有的'='替換成'空格'
s = s.replace('=', ' '); #先把所有的'='替換成'空格'
s = s.replace('{', ' '); #先把所有的'{'替換成'空格'
s = s.replace('\t', ' '); #先把所有的'\t'替換成'空格'
s = re.sub(r'\s+', '-', s); #把任意多個'空格'替換成'-'
return s
#--------------------------------------------
# 檢測類型賦值
#--------------------------------------------
#define a function
def GetTypeValue(s):
typeValue = ""
if s == "int32":
typeValue = "0"
elif s == "int64":
typeValue = "0"
elif s == "string":
typeValue = "\"\""
elif s == "string":
typeValue = "\"\""
else:
typeValue = "{}"
return typeValue
#--------------------------------------------
# 檢測類型替換后的字符串
#--------------------------------------------
#define a function
def CheckType(s,clsStr):
rtStr = ""
arrSplit = s.split('-')
newCls = ""
if arrSplit[0] == "message":
newCls = arrSplit[1]
rtStr += "\n---@class " + newCls + '\n'
rtStr += newCls + " = {}"
rtStr += '\n'
elif (len(arrSplit) > 3) and (arrSplit[1] == "required" or arrSplit[1] == "repeated" or arrSplit[1] == "optional"):
rtStr += clsStr + "." + arrSplit[3] + " = " + GetTypeValue(arrSplit[2])
rtStr += '\n'
#elif arrSplit[0] == '}':
#rtStr += '}'
return rtStr, newCls
#--------------------------------------------
# 讀取文件
#--------------------------------------------
allStr = "" #拼接的字符串,用來寫文件
lastClass = "" #記錄上一個ClassName
fileRead = open("client2server.proto") # 返回一個文件對象
line = fileRead.readline() # 調(diào)用文件的 readline()方法
while line:
rptStr = ""
rptStr = ReplaceStr(line) #自己寫的替換字符串
rptStr,tmpCls = CheckType(rptStr,lastClass)
if tmpCls != "":
lastClass = tmpCls
allStr += rptStr
line = fileRead.readline()
#--------------------------------------------
# 寫文件
#--------------------------------------------
fileWrite = open('LuaProto.lua', 'w')
fileWrite.write(allStr)
fileWrite.close( )
print "文件保存成功,當(dāng)前目錄下的LuaProto.lua"
生成后的Lua文件
---@class Login
Login = {}
Login.user= ""
Login.pwd = ""
---@class LoginRt
LoginRt = {}
LoginRt.rt = 0
LoginRt.token = ""
API 參考
菜鳥教程 Python 基礎(chǔ)教程
http://www.runoob.com/python/python-tutorial.html
總結(jié)
以上是生活随笔為你收集整理的python脚本转lua_初次使用Python脚本,proto协议文件转Lua代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql主从代替技术_mysql主从复
- 下一篇: python0b1011_1011 A+