forked from All-Rated-Extreme-Demon-List/AREDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateData.py
302 lines (264 loc) · 9.75 KB
/
validateData.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import sys
import json
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from jsonschema import validate, exceptions
level_list_schema = {
"type": "array",
"items": {
"type": "string"
}
}
banned_schema = {
"type": "array",
"items": {
"type": "string"
}
}
tags_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"description": {"type": "string"}
}
}
}
level_schema = {
"type": "object",
"properties": {
"id": {"type": "number"},
"name": {"type": "string"},
"description": {"type": "string"},
"author": {"type": "string"},
"creators": {
"type": "array",
"items": {
"type": "string",
}
},
"tags": {
"type": "array",
"items": {
"type": "string",
}
},
"verifier": {"type": "string"},
"verification": {"type": "string"},
"records": {
"type": "array",
"items": {
"type": "object",
"properties": {
"user": {"type": "string"},
"link": {"type": "string"},
"percent": {"type": "number"},
"hz": {"type": "number"},
"mobile": {"type": "boolean"}
}
}
}
}
}
pack_tiers_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"color": {"type": "string"},
"packs": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
pack_list_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"colour": {"type": "string"},
"packs": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
def validate_data():
validator = URLValidator()
current_dir = os.path.join(os.getcwd(), "data")
list_path = os.path.join(current_dir, "_list.json")
legacy_list_path = os.path.join(current_dir, "_legacy.json")
banned_path = os.path.join(current_dir, "_leaderboard_banned.json")
pack_list_path = os.path.join(current_dir, "_packlist.json")
pack_tiers_path = os.path.join(current_dir, "_packtiers.json")
tags_path = os.path.join(current_dir, "_tags.json")
had_error = False
with open(list_path, "r", encoding='utf-8') as file:
try:
levels = json.load(file)
validate(instance=levels, schema=level_list_schema)
except ValueError as e:
print(f"Invalid json in file _list.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _list.json: {str(e)}")
sys.exit(1)
with open(banned_path, "r", encoding='utf-8') as file:
try:
banned = json.load(file)
validate(instance=banned, schema=banned_schema)
except ValueError as e:
print(f"Invalid json in file _leaderboard_banned.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _leaderboard_banned.json: {str(e)}")
sys.exit(1)
available_tags = set()
with open(tags_path, "r", encoding='utf-8') as file:
try:
tags = json.load(file)
validate(instance=tags, schema=tags_schema)
available_tags = {tag["name"] for tag in tags}
except ValueError as e:
print(f"Invalid json in file _tags.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _tags.json: {str(e)}")
sys.exit(1)
with open(legacy_list_path, "r", encoding='utf-8') as file:
try:
legacy = json.load(file)
validate(instance=legacy, schema=level_list_schema)
level_conflicts = list(set(levels) & set(legacy))
for level in level_conflicts:
print(f"Level {level} is both in legacy and main list!")
had_error = True
levels.extend(legacy)
except ValueError as e:
print(f"Invalid json in file _legacy.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _legacy.json: {str(e)}")
sys.exit(1)
with open(pack_list_path, "r", encoding='utf-8') as file:
try:
packs = json.load(file)
validate(instance=packs, schema=pack_list_schema)
except ValueError as e:
print(f"Invalid json in file _packlist.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _packlist.json: {str(e)}")
sys.exit(1)
with open(pack_tiers_path, "r", encoding='utf-8') as file:
try:
pack_tiers = json.load(file)
validate(instance=pack_tiers, schema=pack_list_schema)
except ValueError as e:
print(f"Invalid json in file _packtiers.json: {str(e)}")
sys.exit(1)
except exceptions.ValidationError as e:
print(f"Validation failed for _packtiers.json: {str(e)}")
sys.exit(1)
level_ids = {}
for filename in levels:
file_path = os.path.join(current_dir, f"{filename}.json")
try:
with open(file_path, "r", encoding='utf-8') as file:
try:
data = json.load(file)
validate(instance=data, schema=level_schema)
except ValueError as e:
print(f"Invalid json in file {filename}: {str(e)}")
had_error = True
continue
except exceptions.ValidationError as e:
print(f"Validation failed for {filename}: {str(e)}")
had_error = True
continue
level_id = str(data["id"])
if filename.endswith("2p"):
level_id += "2p"
if level_id in level_ids:
print(f"Duplicate gd level id in file {filename} with previous file {level_ids[level_id]}")
had_error = True
level_ids[level_id] = filename
records = data["records"]
names = [data["verifier"].lower()]
try:
validator(data["verification"])
except ValidationError:
had_error = True
print(f"Invalid verification Url: {filename}: {data['verification']}")
for record in records:
name = record["user"].lower()
if name in names:
had_error = True
print(f"Duplicate Record: {filename}: {name}")
names.append(name)
url = record["link"]
try:
validator(url)
except ValidationError:
had_error = True
print(f"Invalid Url: {filename} {name}: {url}")
if "" in names:
had_error = True
print(f"Empty username in {filename}")
if "tags" in data:
for tag_name in data["tags"]:
if tag_name not in available_tags:
had_error = True
print(f"Unrecognized tag: {filename}: {tag_name}")
creators = []
for creator in data["creators"]:
if creator in creators:
had_error = True
print(f"Duplicate Creator: {filename}: {creator}")
if "," in creator:
had_error = True
print(f"Invalid Creator: {filename}: {creator}")
creators.append(creator)
except FileNotFoundError:
had_error = True
print(f"Missing file {filename}")
pack_names = []
for pack in packs:
if pack["name"] in pack_names:
had_error = True
print(f"Duplicate pack name: \"{pack['name']}\"")
continue
pack_names.append(pack["name"])
for level in pack["levels"]:
if level not in levels:
had_error = True
print(f"Unkown level {level} in Pack \"{pack['name']}\"")
pack_names_used = []
for tier in pack_tiers:
for pack in tier["packs"]:
if pack not in pack_names:
had_error = True
print(f"Unkown pack \"{pack}\" in pack-tier \"{tier['name']}\"")
if pack in pack_names_used:
had_error = True
print(f"Pack \"{pack}\" was already used in another pack-tier than \"{tier['name']}\"")
pack_names_used.append(pack)
pack_names_unused = list(set(pack_names) - set(pack_names_used))
for pack in pack_names_unused:
had_error = True
print(f"Pack \"{pack}\" is not assigned to a tier")
if had_error:
sys.exit(1)
if __name__ == "__main__":
validate_data()