forked from devilesk/dota-map-coordinates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_console.py
76 lines (74 loc) · 2.31 KB
/
process_console.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
import json
import os
def all_int(l):
try:
for k in l:
int(k)
return True
except:
return False
with open('data/map_data.log', 'r') as f:
lines = f.readlines()
started = False
next_filename = False
data = {}
curr_tbl = data
curr_key = None
stack = []
level = -1
for line in lines:
line = line.rstrip('\n')
if line == '[start]':
started = True
next_filename = True
data = {}
curr_tbl = data
curr_key = None
stack = []
level = -1
print(line)
elif next_filename:
print(line)
filename = line
next_filename = False
elif line == '[end]':
print(line)
started = False
while len(stack):
## print(all_int(curr_tbl.keys()))
tbl, key = stack.pop()
if all_int(curr_tbl.keys()):
tbl[key] = list(curr_tbl.values())
## print(tbl[key])
curr_tbl = tbl
with open(os.path.join('data', filename), 'w') as g:
g.write(json.dumps(data, separators=(',', ':')))
elif started:
line_level = (len(line) - len(line.lstrip())) / 2
k, v = [x.strip() for x in line.split(':')]
## print(line_level, k, v)
while level > line_level or (line_level == level and not len(curr_tbl)):
## print(all_int(curr_tbl.keys()))
# if not len(stack):
# print(stack)
tbl, key = stack.pop()
if all_int(curr_tbl.keys()):
tbl[key] = list(curr_tbl.values())
## print(tbl[key])
curr_tbl = tbl
level -= 1
if v == '':
# if not k.isdigit():
# print ('New', k)
curr_tbl[k] = {}
stack.append((curr_tbl, k))
curr_tbl = curr_tbl[k]
else:
try:
curr_tbl[k] = int(v)
except:
try:
curr_tbl[k] = float(v)
except:
curr_tbl[k] = v
level = line_level