初次使用Python脚本,proto协议文件转Lua代码

来源:互联网 发布:c stl 源码 编辑:程序博客网 时间:2024/05/22 03:19

使用IntelliJ IDEA编辑器编写Lua脚本的时候,安装一个插件 “EmmyLua” 可以对有代码提示功能
想把protoBuf协议文件转成Lua代码,然后给出代码提示

第一次使用python脚本
使用到的几个功能记录一下

1.设置为文件格式备注为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#调用时,用两个变量接rtS,rtS1 = MyFunc(str)

2.替换字符串

s = s.replace('=', ' ');    #先把所有的'='替换成'空格'

3.用正则替换任意个数的’空格’换成’-‘

s = re.sub(r'\s+', '-', s); #把任意多个'空格'替换成'-'

4.把字符串分割成数组

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.把字符串分割成数组

arrSplit = s.split('-') #把"1-2-3" 分割成 [1,2,3]

7.读文件

fileRead = open("client2server.proto")             # 返回一个文件对象  line = fileRead.readline()             # 调用文件的 readline()方法while line:    print line    # 读取下一行,while循环,直到读完最后一行    line = fileRead.readline()

8.写文件

fileWrite = open('LuaProto.lua', 'w')fileWrite.write(allStr)fileWrite.close( )print "文件保存成功,当前目录下的LuaProto.lua"

以下是代码

Proto文件

// msgType = 1// 客户端 -> 服务器// 发送登录消息,包括账号和密码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 = "" #记录上一个ClassNamefileRead = open("client2server.proto")             # 返回一个文件对象  line = fileRead.readline()             # 调用文件的 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 "文件保存成功,当前目录下的LuaProto.lua"

生成后的Lua文件

---@class LoginLogin = {}Login.user= ""Login.pwd = ""---@class LoginRtLoginRt = {}LoginRt.rt = 0LoginRt.token = ""

API 参考

菜鸟教程 Python 基础教程http://www.runoob.com/python/python-tutorial.html
原创粉丝点击