forked from fafranco82/swdestinydb-json-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
executable file
·389 lines (310 loc) · 15.3 KB
/
validate.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
import argparse
import json
import jsonschema
import os
import sys
import re
SET_PATH_STRING = os.path.sep + 'set' + os.path.sep
def pluralize(word):
if word[-1] == 'y':
return word[:-1]+'ies'
elif word[-1] == 's' or word[-2:]=='sh':
return word+'es'
else:
return word+'s'
class Logger:
def __init__(self, verbose, indent=0, prefix=None):
self.verbose = verbose
self.indent = indent
self.prefix = prefix or ""
self.togglePrefix = True
def verbose_print(self, text, minimum_verbosity=0):
if self.verbose >= minimum_verbosity:
if self.togglePrefix:
sys.stdout.write((" "*self.indent))
sys.stdout.write(self.prefix)
self.togglePrefix = False
sys.stdout.write(text)
if "\n" in text:
self.togglePrefix = True
class ValidatorBase:
def __init__(self, base_path, logger, fix_formatting):
self.formatting_errors = 0
self.validation_errors = 0
self.logger = logger
self.fix_formatting = fix_formatting
self.collections = {}
self.base_path = base_path
self.data_path = base_path
self.schema_path = os.path.join(base_path, "schema")
self.re_side_a = re.compile(r'[0-9A]$')
def validate(self):
check_dir_access(self.data_path)
check_dir_access(self.schema_path)
self.logger.verbose_print("Validating data...\n", 0)
for thing in ['affiliation', 'faction', 'rarity', 'type', 'subtype', 'sideType', 'cycle', 'set']:
collection = self.load_collection(thing)
if collection:
self.load_collections(thing, collection)
else:
self.load_collections(thing, [])
self.load_sets_collection()
for thing in ['format']:
collection = self.load_collection(thing)
if collection:
self.load_collections(thing, collection)
else:
self.load_collections(thing, [])
def show_results(self):
self.logger.verbose_print("Found %s formatting and %s validation errors\n" % (self.formatting_errors, self.validation_errors), 0)
if self.formatting_errors == 0 and self.validation_errors == 0:
sys.exit(0)
else:
sys.exit(1)
def load_collection(self, thing):
plural_thing = pluralize(thing)
self.logger.verbose_print("Loading collection of %s\n" % plural_thing, 1)
json_path = os.path.join(self.data_path, "%s.json" % plural_thing)
check_file_access(json_path)
things_data = self.load_json_file(json_path)
if not self.validate_collection(thing, things_data):
return None
return things_data
def load_sets_collection(self):
self.logger.verbose_print("Loading collection of cards\n", 1)
json_dir = os.path.join(self.data_path, "%s" % 'set')
check_dir_access(json_dir)
for setcode in [s.get('code') for s in sorted(self.collections['set'].values(), key=lambda s: s.get('position'))]:
self.logger.verbose_print("Loading cards from set '%s'...\n" % setcode, 1)
json_path = os.path.join(json_dir, "%s.json" % setcode)
exists = not test_file_access(json_path)
if exists:
cards = self.load_json_file(json_path)
if self.validate_collection('card', cards):
self.load_collections('card', cards)
def load_collections(self, thing, collection):
if not thing in self.collections:
self.collections[thing] = {}
for item in collection:
#if not item.get("code") in self.collections[thing]:
# self.collections[thing][item.get("code")] = []
#self.collections[thing][item.get("code")].append(item)
self.collections[thing][item.get("code")] = item
def validate_collection(self, thing, things_data):
plural_thing = pluralize(thing)
self.logger.verbose_print("Validating collection of %s\n" % plural_thing, 1)
schema_path = os.path.join(self.schema_path, "%s_schema.json" % thing)
check_file_access(schema_path)
schema_data = self.load_json_file(schema_path)
if not isinstance(things_data, list):
self.logger.verbose_print("Insides of %s collection file are not a list!\n", 0)
return False
if not schema_data:
return False
if not self.check_json_schema(schema_data, schema_path):
return False
retval = True
for thing_data in things_data:
retval = self.validate_schema(thing, thing_data, schema_data) and retval
return retval
def validate_schema(self, thing, thing_data, schema_data):
self.logger.verbose_print("Validating %s %s..." % (thing, thing_data.get("code")), 2)
try:
jsonschema.validate(thing_data, schema_data)
self.custom_check(thing, thing_data)
self.logger.verbose_print(" OK\n", 2)
except jsonschema.ValidationError as e:
self.logger.verbose_print("ERROR\n", 2)
self.logger.verbose_print("Validation error in %s: (code: '%s')\n" % (thing, thing_data.get("code")), 0)
self.validation_errors += 1
for line in str(e).split('\n'):
self.logger.verbose_print(" | "+line+"\n", 0)
return False
return True
def custom_check(self, thing, thing_data):
custom_check_method = "custom_check_%s" % thing
if hasattr(self, custom_check_method) and callable(getattr(self, custom_check_method)):
getattr(self, custom_check_method)(thing_data)
def custom_check_set(self, set):
validations = []
if not set.get('cycle_code') in self.collections['cycle']:
validations.append("Cycle code '%s' does not exist in set '%s'" % (set.get('cycle_code'), set.get('code')))
if validations:
raise jsonschema.ValidationError("\n".join(["- %s" % v for v in validations]))
def is_side_a(self, card):
return self.re_side_a.match(card.get('code'))
def custom_check_card(self, card):
validations = []
#check foreing codes
for collection in ["affiliation", "faction", "rarity", "type"]:
field = collection + "_code"
if field in card and not card.get(field) in self.collections[collection]:
validations.append("%s code '%s' does not exist in card '%s'" % (collection, card.get(field), card.get('code')))
#check subtypes
if 'subtypes' in card:
for subtype in [s for s in card.get('subtypes') if not s in self.collections['subtype']]:
validations.append("Subtype code '%s' does not exist in card '%s'" % (subtype, card.get('code')))
#check reprint of
if 'reprint_of' in card and not card.get('reprint_of') in self.collections['card']:
validations.append("Reprinted card %s does not exist" % (card.get('reprint_of')))
#checks by type
check_by_type_method = "custom_check_%s_card" % card.get('type_code')
if hasattr(self, check_by_type_method) and callable(getattr(self, check_by_type_method)):
validations.extend(getattr(self, check_by_type_method)(card))
if validations:
raise jsonschema.ValidationError("\n".join(["- %s" % v for v in validations]))
def custom_check_character_card(self, card):
validations = []
for attr in ['points', 'health']:
if not card.has_key(attr) and self.is_side_a(card):
validations.append("Character card %s must have attribute '%s'" % (card.get('code'), attr))
return validations
def custom_check_event_card(self, card):
validations = []
if not card.has_key('cost'):
validations.append("Character card %s must have attribute 'cost'" % card.get('code'))
return validations
def custom_check_upgrade_card(self, card):
return self.custom_check_event_card(card)
def custom_check_support_card(self, card):
return self.custom_check_event_card(card)
def custom_check_plot_card(self, card):
validations = []
for attr in ['points']:
if not card.has_key(attr) and self.is_side_a(card):
validations.append("Plot card %s must have attribute '%s'" % (card.get('code'), attr))
return validations
def custom_check_format(self, format):
validations = []
for set in format.get('data').get('sets'):
if not set in self.collections['set']:
validations.append("Set code '%s' does not exist in format '%s'" % (set, format.get('code')))
if format.get('data').get('balance') is not None:
for card, points in format.get('data').get('balance').items():
if not card in self.collections['card']:
validations.append("Card code '%s' does not exist in format '%s' balance of the force" % (card, format.get('code')))
if format.get('data').get('banned') is not None:
for card in format.get('data').get('banned'):
if not card in self.collections['card']:
validations.append("Card code '%s' does not exist in format '%s' ban list" % (card, format.get('code')))
if format.get('data').get('errata') is not None:
for card in format.get('data').get('errata'):
if not card in self.collections['card']:
validations.append("Card code '%s' does not exist in format '%s' errata" % (card, format.get('code')))
if validations:
raise jsonschema.ValidationError("\n".join(["- %s" % v for v in validations]))
def load_json_file(self, path):
try:
with open(path, "rb") as data_file:
bin_data = data_file.read()
raw_data = bin_data.decode("utf-8")
json_data = json.loads(raw_data)
except ValueError as e:
self.logger.verbose_print("%s: File is not valid JSON.\n" % path, 0)
self.validation_errors += 1
print(e)
return None
self.logger.verbose_print("%s: Checking JSON formatting...\n" % path, 4)
formatted_raw_data = self.format_json(json_data, SET_PATH_STRING in path)
if formatted_raw_data != raw_data:
self.logger.verbose_print("%s: File is not correctly formatted JSON.\n" % path, 0)
self.formatting_errors += 1
if self.fix_formatting and len(formatted_raw_data) > 0:
self.logger.verbose_print("%s: Fixing JSON formatting...\n" % path, 0)
try:
with open(path, "wb") as json_file:
bin_formatted_data = formatted_raw_data.encode("utf-8")
json_file.write(bin_formatted_data)
except IOError as e:
self.logger.verbose_print("%s: Cannot open file to write.\n" % path, 0)
print(e)
return json_data
def format_json(self, json_data, sorting=False):
if sorting:
json_data = sorted(json_data, key=lambda k: k['code'])
formatted_data = json.dumps(json_data, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
formatted_data += "\n"
return formatted_data
def check_json_schema(self, data, path):
try:
jsonschema.Draft4Validator.check_schema(data)
return True
except jsonschema.exceptions.SchemaError as e:
self.logger.verbose_print("%s: Schema file is not valid Draft 4 JSON schema.\n" % path, 0)
self.validation_errors += 1
print(e)
return False
class Validator(ValidatorBase):
def __init__(self, base_path, logger, fix_formatting):
ValidatorBase.__init__(self, base_path, logger, fix_formatting)
self.i18n_path = os.path.join(base_path, "translations")
def validate(self):
ValidatorBase.validate(self)
if self.validation_errors == 0:
self.validate_locales()
else:
self.logger.verbose_print("There were errors in main files. Validation of translated files skipped.\n", 0)
def validate_locales(self):
if os.path.exists(self.i18n_path):
self.logger.verbose_print("Validating I18N files...\n", 0)
check_dir_access(self.i18n_path)
locales_path = self.i18n_path
if os.path.exists(locales_path):
check_dir_access(locales_path)
for locale in [l for l in os.listdir(locales_path) if os.path.isdir(os.path.join(locales_path, l))]:
self.logger.verbose_print("Validating I18N files for locale '%s'...\n" % locale, 1)
i18nValidator = I18NValidator(self, locale, self.i18n_path, self.logger)
i18nValidator.validate()
self.formatting_errors += i18nValidator.formatting_errors
self.validation_errors += i18nValidator.validation_errors
class I18NValidator(ValidatorBase):
def __init__(self, parent, locale, base_path, logger):
ValidatorBase.__init__(self, base_path, Logger(logger.verbose, logger.indent+4, "[%s] " % locale), parent.fix_formatting)
self.parent = parent
self.locale = locale
self.data_path = os.path.join(base_path, locale)
self.schema_path = os.path.join(parent.schema_path, 'translations')
def custom_check(self, thing, thing_data):
if thing_data.has_key("code") and not self.parent.collections[thing].has_key(thing_data["code"]):
raise jsonschema.ValidationError("- %s code '%s' does not exist in '%s' %s translations" % (thing, thing_data["code"], self.locale, thing))
def custom_check_character_card(self, card):
return []
def custom_check_event_card(self, card):
return []
def custom_check_upgrade_card(self, card):
return self.custom_check_event_card(card)
def custom_check_support_card(self, card):
return self.custom_check_event_card(card)
def parse_commandline():
argparser = argparse.ArgumentParser(description="Validate JSON in the swdestinydb data repository.")
argparser.add_argument("-f", "--fix_formatting", default=False, action="store_true", help="write suggested formatting changes to files")
argparser.add_argument("-v", "--verbose", default=0, action="count", help="verbose mode")
argparser.add_argument("-b", "--base_path", default=os.getcwd(), help="root directory of JSON repo (default: current directory)")
args = argparser.parse_args()
check_dir_access(args.base_path)
return args
def check_dir_access(path):
if not os.path.isdir(path):
sys.exit("%s is not a valid path" % path)
elif os.access(path, os.R_OK):
return
else:
sys.exit("%s is not a readable directory")
def test_file_access(path):
if not os.path.isfile(path):
return "%s does not exist" % path
elif os.access(path, os.R_OK):
return
else:
return "%s is not a readable file"
def check_file_access(path):
result = test_file_access(path)
if result:
sys.exit(result)
if __name__ == "__main__":
args = parse_commandline()
logger = Logger(args.verbose)
validator = Validator(args.base_path, logger, args.fix_formatting)
validator.validate()
validator.show_results()