-
Notifications
You must be signed in to change notification settings - Fork 7
/
scan_levels.py
92 lines (82 loc) · 2.47 KB
/
scan_levels.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
#!/usr/bin/env python
# coding: utf-8
import os
import json
import re
import google_auth_write
Mission=[]
Level= set()
def construct(path, meta):
global Level, Mission
#print(path, meta[:20])
try:
j = json.loads(meta)
except:
print("json error: ", meta)
return
if 'type' in j:
if j["type"] == "mission":
extract = re.findall('\d+', path)
if len(extract)>1:
#print(extract[0])
j["level"]=extract[0]
Mission.append(j)
Level.add(extract[0])
def scan_readme(file):
#print('1', file)
with open(file, encoding="utf-8") as f: lines = f.read()
#print('2', lines[:50].replace('\n', '\\n'))
extract = re.findall(r"\{.*\}", lines)
if len(extract)>0:
#print('3', "extract", type(extract[0]), len(extract), extract[0])
construct(file, extract[0])
def listdir(basepath):
global Level, Mission
#print('a', basepath)
with os.scandir(basepath) as entries:
for entry in entries:
fname = basepath+'/'+entry.name
#print('b', fname)
if entry.is_dir():
if "Levels" in fname:
listdir(fname)
elif str(entry.name).lower() == "readme.md":
scan_readme(fname)
def scanit():
global Level, Mission
Mission=[]
Level= set()
# List all subdirectories using scandir()
basepath = '.'
listdir(basepath)
#print(Level)
#print(Mission)
allkeys = set()
for le in Level:
#print("Level:", le)
for m in Mission:
#if m["level"] == le: print(m)
allkeys.update(m.keys())
keys = ['order', 'type','level', 'id', 'description', 'target']
print('before', allkeys)
allkeys -= set(keys)
print('after', allkeys)
for x in allkeys: keys.append(x)
val = []
for le in Level:
for m in Mission:
val2 = []
if m["level"]==le:
for i in range(1,len(keys)):
val2.append(m.get(keys[i],""))
#print(m.get(keys[2]), m.get(keys[3]))
val2.insert(0, "%03d.%02d"%(int(m.get(keys[2])),int(m.get(keys[3]))))
val.append(val2)
val.sort()
val.insert(0, keys)
for x in val: del x[0]
#print(val)
return val
if __name__ == '__main__':
val = scanit()
google_auth_write.write_playbook(val)