def main(): while True: try: luatext = raw_input('lua_filename>') except EOFError: break if not luatext: continue a1 = PyLuaTblParser() print(luatext) a1.loadLuaTable(luatext) print(a1.dump()) try: wtext = raw_input('wirte_to_filename>') except EOFError: break if not wtext: continue a1.dumpLuaTable(wtext) try: dictext = raw_input('dict_filename>') except EOFError: break if not dictext: continue a2 = PyLuaTblParser() with open(dictext, 'r') as file: d = file.read() dic = ast.literal_eval(d) a2.loadDict(dic) print(a2.dumpDict()) print(a2.dump())
3, [45] = 11 --[[fdsaf--]], [''] = '', '' } } } ,,,}''' test_str = '''{\r\nroot = {\r\n\t"Test Pattern String",\r\n\t-- {"object with 1 member" = {"array with 1 element",},},\r\n\t{["object with 1 member"] = {"array with 1 element",},},\r\n\t{},\r\n\t[99] = -42,\r\n\t[98] = {{}},\r\n\t[97] = {{},{}},\r\n\t[96] = {{}, 1, 2, nil},\r\n\t[95] = {1, 2, {["1"] = 1}},\r\n\t[94] = { {["1"]=1, ["2"]=2}, {1, ["2"]=2}, ["3"] = 3 },\r\n\ttrue,\r\n\tfalse,\r\n\tnil,\r\n\t{\r\n\t\t["integer"]= 1234567890,\r\n\t\treal=-9876.543210,\r\n\t\te= 0.123456789e-12,\r\n\t\tE= 1.234567890E+34,\r\n\t\tzero = 0,\r\n\t\tone = 1,\r\n\t\tspace = " ",\r\n\t\tquote = "\\"",\r\n\t\tbackslash = "\\\\",\r\n\t\tcontrols = "\\b\\f\\n\\r\\t",\r\n\t\tslash = "/ & \\\\",\r\n\t\talpha= "abcdefghijklmnopqrstuvwyz",\r\n\t\tALPHA = "ABCDEFGHIJKLMNOPQRSTUVWYZ",\r\n\t\tdigit = "0123456789",\r\n\t\tspecial = "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\r\n\t\thex = "0x01230x45670x89AB0xCDEF0xabcd0xef4A",\r\n\t\t["true"] = true,\r\n\t\t["false"] = false,\r\n\t\t["nil"] = nil,\r\n\t\tarray = {nil, nil,},\r\n\t\tobject = { },\r\n\t\taddress = "50 St. James Street",\r\n\t\turl = "http://www.JSON.org/",\r\n\t\tcomment = "// /* <!-- --",\r\n\t\t["# -- --> */"] = " ",\r\n\t\t[" s p a c e d " ] = {1,2 , 3\r\n\r\n\t\t\t,\r\n\r\n\t\t\t4 , 5 , 6 ,7 },\r\n\t\t--[[[][][] Test multi-line comments\r\n\t\t\tcompact = {1,2,3,4,5,6,7},\r\n\t- -[luatext = "{\\"object with 1 member\\" = {\\"array with 1 element\\"}}",\r\n\t\tquotes = "" (0x0022) %22 0x22 034 "",\r\n\t\t["\\\\\\"\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"]\r\n\t\t= "A key can be any string"]]\r\n\t-- ]]\r\n\t\tcompact = {1,2,3,4,5,6,7},\r\n\t\tluatext = "{\\"object with 1 member\\" = {\\"array with 1 element\\"}}",\r\n\t\tquotes = "" (0x0022) %22 0x22 034 "",\r\n\t\t["\\\\\\"\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"]\r\n\t\t= "A key can be any string"\r\n\t},\r\n\t0.5 ,31415926535897932384626433832795028841971693993751058209749445923.\r\n\t,\r\n\t3.1415926535897932384626433832795028841971693993751058209749445923\r\n\t,\r\n\r\n\t1066\r\n\r\n\r\n\t,"rosebud"\r\n\r\n}} ''' a1 = PyLuaTblParser() a2 = PyLuaTblParser() a3 = PyLuaTblParser() a1.load(test_str) print a1.dump() d1 = a1.dumpDict() a2.loadDict(d1) a2.dumpLuaTable('output.txt') a3.loadLuaTable('output.txt') d3 = a3.dumpDict() print d1 == d3 print '\n' print d1 print '\n' print d3
,"rosebud" }} ''') d = parser.dumpDict() print d parser.loadDict(d) parser["root"][96] = "Hello World" parser.update({"root":1}) # parser["root"][4] = "Hello World" parser.dumpLuaTable("output2.lua") # print "------------------" parser.loadLuaTable("output2.lua") sd2 = parser.dumpDict() print "--------------------------" print sd2 # print "-------------" # print parser.dumpLuaTable("table_test.lua") # # print PythonStrToStruct('''{ # # "array": [65, 23, 5], # # "dict": { # # "mixed": { # # 1: 43, # # 2: 54.33, # # 3: False, # # 4: 9, # # "string": "value"
''' The parser interprets Lua tables that have the following syntax form:https://www.lua.org/manual/5.1/manual.html There is mainly one class in this project. PyLuaTblParser provides both the interface and the Lua table parser function. The load() method loads and parses a Lua table from a string, the dump() method dumps the string which represents the abstract syntax tree built during load() function. The eat_text() method read the table code, and decode it into an abstract syntax tree called container as well as parse the abstract syntax tree into dump string recursively. An incident stack is used to dump a dict into a readable form. This parser support hex digit representation. Basic usage is: ''' from PyLuaTblParser import * a1 = PyLuaTblParser() a2 = PyLuaTblParser() a3 = PyLuaTblParser() test_str = '{array = {0x6A,23,5,},dict = {mixed = {43,54.33,false,9,string = "value",},array = {3,6,4,},string = "value",},}' a1.load(test_str) d1 = a1.dumpDict() a2.loadDict(d1) file_path = "test.txt" a2.dumpLuaTable(file_path) a3.loadLuaTable(file_path) d3 = a3.dumpDict()
Parser1.load(lua_table_str) parsed_lua_table1 = Parser1.dumpDict() print parsed_lua_table1 Parser2 = PyLuaTblParser() Parser2.loadDict(parsed_lua_table1) parsed_lua_table2 = Parser2.dumpDict() print parsed_lua_table2 Parser1.dumpLuaTable("lua_table.lua") Parser2.dumpLuaTable("lua_table.lua") Parser1.loadLuaTable("lua_table.lua") parsed_lua_table3 = Parser1.dumpDict() print parsed_lua_table3 Parser3 = PyLuaTblParser() Parser3.loadLuaTable("rawluatable.lua") Parser3.dumpLuaTable('testcase.lua') Parser3.loadLuaTable('testcase.lua') Parser3.dumpLuaTable('testcase.lua') Parser3.loadLuaTable('testcase.lua') Parser3.dumpLuaTable('testcase.lua') Parser3.loadLuaTable('testcase.lua') parsed_lua_table3 = Parser3.dumpDict()
def test2(): print '.................... Test2 loadLuaTable and dumpLuaTable' p = PyLuaTblParser() p.loadLuaTable('test2-load.txt') print p.dump() p.dumpLuaTable('test2-dump.txt')
['"object with 1 member"'] = { "array with 1 element" } } } """ #pyt.load(test_str44) #pyt.loadDict(d) #pyt.loadDict(d2) #print d2 #res = pyt.dumpDict() #print res #pyt.loadLuaTable("py_test_input3.txt") #d = pyt.dumpDict() #print d #pyt.dumpLuaTable("dumpLuaTable.txt") a1 = PyLuaTblParser() a2 = PyLuaTblParser() a3 = PyLuaTblParser() a1.loadLuaTable("py_test_input2.txt") a1.dumpLuaTable("1.txt") #a1.loadLuaTable("1.txt") #d1 = a1.dumpDict() #print d1
from PyLuaTblParser import * if __name__ == '__main__': #基础功能能测试 a1 = PyLuaTblParser() a2 = PyLuaTblParser() a3 = PyLuaTblParser() test_str = '{array = {65,23,5,},dict = {mixed = {43,54.33,false,9,string = "value",},array = {3,6,4,},string = "value",},}' a1.load(test_str) d1 = a1.dumpDict() a2.loadDict(d1) a2.dumpLuaTable('test.txt') a3.loadLuaTable('test.txt') d3 = a3.dumpDict() print d3 #[],update测试 print a3['array'] a3['array'] = 1234 print a3['array'] print a3.dumpDict() a3.update({ 'array': { "one": 1, "two": 2, "three": 3 },