-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidateConfig.py
89 lines (79 loc) · 2.57 KB
/
ValidateConfig.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
import json
import os
import pyautogui
from tasks import *
def ValidateConfig(path):
errorCount = 0
warningCount = 0
try:
with open(path, "r") as f:
config = json.loads(f.read())
except json.JSONDecodeError as err:
print(f"Invalid Json file: {err}")
return False
except OSError as err:
print(f"Can't read file {path}: {err}")
return False
if type(config) != list:
print(
f"Type Error: expected a list in 'config.json', but found a {type(config)} instead"
)
return False
print("checking ids...")
ids = []
sameIds = []
for i, task in enumerate(config):
if type(task) != dict:
continue
if not 'id' in task.keys():
continue
if not task['id'] in ids:
ids.append(task['id'])
else:
if not task['id'] in sameIds:
sameIds.append(task['id'])
print(f"Loaded {len(ids)} id")
print("checking tasks...")
for i, task in enumerate(config):
print("=" * 20 + f"Task {i:05d}" + "=" * 20)
if type(task) != dict:
print(
f"Type Error: expected a dict, but found a {type(task)} instead"
)
errorCount += 1
continue
if not 'type' in task.keys():
print(f"Key Error: missing key 'type'")
errorCount += 1
continue
taskType = task['type']
if taskType == "command":
a, b = CommandTask.validate(task, ids, sameIds)
elif taskType == "keypress":
a, b = KeyTask.validate(task, ids, sameIds)
elif taskType == "detect":
a, b = DetectTask.validate(task, ids, sameIds)
elif taskType == "match":
a, b = MatchTask.validate(task, ids, sameIds)
elif taskType == "timeout":
a, b = TimeoutTask.validate(task, ids, sameIds)
elif taskType == "socketsend":
a, b = SocketSendTask.validate(task, ids, sameIds)
else:
print(f"Value Error: unknown task type {taskType}")
a, b = 1, 0
errorCount += a
warningCount += b
print("=" * 30)
print(f"Total: {errorCount} Errors, {warningCount} Warnings")
return errorCount == 0
if __name__ == "__main__":
data_dir = r".\data"
if not os.path.exists(data_dir):
print("folder not found")
exit(0)
if not os.path.exists(os.path.join(data_dir, "config.json")):
print("file not found")
exit(0)
os.chdir("./data")
ValidateConfig("config.json")