forked from lt-tree/CompressLuaTable
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBEBase.py
More file actions
142 lines (102 loc) · 3.78 KB
/
BEBase.py
File metadata and controls
142 lines (102 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Basic method
method list
BEParseError :
BEConvert : Convert Method
BEOutput : Formatted output
"""
import json
import codecs
from slpp import slpp as lua
SPECIAL_CHAR_LIST = [
'$',
'-'
]
class BEParseError(Exception):
pass
class BEConvert(object):
def __init__(self):
pass
def convert_lua_table_dict(self, file_path):
"""Convert lua table to python dict.
This method parses lua file by intercepting the first '{' and last '}'.
Make sure there is only one table in the file.
Any other information previous the first '{' and after the last '}' will be ignored.
Args:
file_path: lua table file path.
Returns:
A dict converted by file.
"""
file_handler = codecs.open(file_path, 'r', encoding='utf-8')
file_content = file_handler.read()
file_handler.close()
file_content = file_content[file_content.find('{') : file_content.rfind('}') + 1]
py_dict = lua.decode(file_content)
return py_dict
def __have_special_char(self, check_str):
"""Check have special char
Check if have special char the char list is SPECIAL_CHAR_LIST.
lua key can't recognize special char for key like $, -
Args:
check_str: string, need check string
Returns:
int, the char in string index, return -1 if not exist
"""
for char in SPECIAL_CHAR_LIST:
if check_str.find(char) != -1:
return True
return False
def convert_dict_lua_file(self, file_handler, lua_dict, deep):
"""Convert dict to lua file
Output dict in file by lua table format.
Special process value string '__rt'
Args:
file_handler: file handler
lua_dict: dict
deep: int
"""
prefix_tab = "\t" * deep
for key, value in sorted(lua_dict.items()):
if isinstance(key, int):
file_handler.write("%s[%s] = " % (prefix_tab, key))
elif isinstance(key, str):
if self.__have_special_char(key):
file_handler.write("%s[\"%s\"] = " % (prefix_tab, key))
else:
file_handler.write("%s%s = " % (prefix_tab, key))
else:
print('ERROR! Wrong key type!')
raise SystemExit
if isinstance(value, dict):
file_handler.write("{\n")
self.convert_dict_lua_file(file_handler, value, deep + 1)
file_handler.write("%s},\n" % (prefix_tab))
elif isinstance(value, str):
if value.find("__rt") != -1:
file_handler.write("%s,\n" % (value))
else:
file_handler.write("\'%s\',\n" % (value))
elif isinstance(value, bool):
file_handler.write("%s,\n" % (str(value).lower()))
elif value is None:
file_handler.write("nil,\n")
else:
file_handler.write("%s,\n" % (value))
def convert_sqlite_dict(self):
pass
class BEOutput(object):
def __init__(self):
pass
def format_show(self, content):
"""Format show variable
"""
print(json.dumps(content, indent=4, ensure_ascii=False))
def format_show_file(self, file_path, content):
"""
"""
file_handler = codecs.open(file_path, 'w', encoding='utf-8')
file_handler.write(json.dumps(content, indent=4, ensure_ascii=False))
file_handler.close()
BEOutput = BEOutput()
BEConvert = BEConvert()
__all__ = ['BEOutput']
__all__ = ['BEConvert']