-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua_writer.py
116 lines (99 loc) · 2.27 KB
/
lua_writer.py
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
#!/usr/bin/env python
# coding=utf-8
class lua_writer():
"""excel writer"""
__file = None
__name = None
__table = 0
def __init__(self, output_file):
aname = output_file
_path = ''
idx = aname.rfind('/')
if idx != -1:
_path = aname[:idx + 1]
aname = aname[idx + 1:]
idx = aname.find('.')
if idx != -1:
aname = aname[: idx]
self.__name = aname
self.__file = open(_path + aname + '.lua', 'w', encoding = 'utf8')
self.description()
def __del__(self):
self.__file.close()
def description(self):
self.__file.write(
'''-- automake by excel2lua
-- auth: ferchiel mail: [email protected]
''')
def table_beg(self, name, _type):
s = ''
for x in range(self.__table):
s += '\t'
assert(_type == 'str' or _type == 'int')
if _type == 'str':
s += str(name) + ' = {\n'
elif _type == 'int':
if int(name) == name:
name = int(name)
name = str(name)
s += '[' + name + '] = {\n'
else:
print('MAKE ERROR! FILE: lua_writer.py LINE: 40')
exit(0)
self.__file.write(s)
self.__table += 1
def table_end(self):
s = ''
for x in range(self.__table - 1):
s += '\t'
s += '},\n'
self.__file.write(s)
self.__table -= 1
def attribute(self, key, value, _type):
s = ''
for x in range(self.__table):
s += '\t'
s += key + ' = '
if _type == 'int':
if int(value) == value:
value = int(value)
value = str(value)
s += value
elif _type == 'str':
value = str(value)
s += '\'' + value + '\''
elif _type == 'ints':
s += '{ '
tab = str(value).split(',')
for v in tab:
s += v + ', '
s = s[: -2]
s += ' }'
elif _type == 'strs':
s += '{ '
tab = value.split('`')
for v in tab:
s += '\'' + v + '\', '
s = s[: -2]
s += ' }'
elif _type == 'pos':
tab = value.split(',')
assert( len( tab ) == 2 )
s += '{ x = ' + tab[0] + ', y = ' + tab[1] + ' }'
elif _type == 'rect':
tab = value.split(',')
assert( len( tab ) == 4 )
s += '{ x = ' + tab[0] + ', y = ' + tab[1] + ', w = ' + tab[2] + ', h = ' + tab[3] + ' }'
else:
print('MAKE ERROR! FILE: lua_writer.py LINE: 62')
exit(0)
s += ',\n'
self.__file.write(s)
def write_beg(self):
self.__table += 1
s = 'return {\n'
self.__file.write(s)
def write_end(self):
self.__table -= 1
s = '}'
self.__file.write(s)