-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_compile.py
90 lines (80 loc) · 4.35 KB
/
test_compile.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
import os
import sys
import warnings
from collections import defaultdict
try:
import yaml
except ImportError:
print("Your python installation must have pyyaml installed.")
sys.exit()
try:
import pokecat
except ImportError:
print("Your python installation must have pokecat installed.")
sys.exit()
#sets = []
#outfile = "pbrpokemondb.json"
existing_ids = {}
genders_per_species = defaultdict(set)
illegal_chars = r"[\]^`|"
name_replacements = {"ᴹ": "M", "ɴ": "N", "×": "x", "’": "'", "”": "\"", "ᵖ": "P", "ᵏ": "K", " ": " ", "ᴾ": "P"}
allowed_characters = ["\u2640", "\u2642", "â", "É"]
testdir = "./pokesets"
if len(sys.argv) > 1:
testdir = sys.argv[1]
for dir, _, files in os.walk(testdir):
for file in files:
if not (file.endswith(".yaml") or file.endswith(".yml")) or file.startswith("_"):
continue
filepath = os.path.join(dir, file)
with open(filepath, "r", encoding="utf-8") as f:
try:
contents = list(yaml.load_all(f, Loader=yaml.FullLoader))
for pokeset in contents:
if not pokeset:
print("Skipping empty pokeset in {}".format(filepath))
continue
identifier = "{}:{} {}".format(filepath, pokeset.get("species"), pokeset.get("setname"))
with warnings.catch_warnings(record=True) as w:
if "ingamename" in pokeset:
fixed_ingamename = pokeset["ingamename"].encode("ascii", "replace").decode()
for char in illegal_chars:
fixed_ingamename = fixed_ingamename.replace(char, "?")
temp = ""
for i, char in enumerate(pokeset["ingamename"]):
if char in allowed_characters:
temp += char
elif char in name_replacements:
temp += name_replacements[char]
else:
temp += fixed_ingamename[i]
fixed_ingamename = temp
if pokeset["ingamename"] != fixed_ingamename:
print("CHANGED INGAMENAME TO {} AS A TEMPORARY FIX TO AVOID ENCODING ISSUES"
.format(fixed_ingamename))
pokeset["ingamename"] = fixed_ingamename
try:
pokeset = pokecat.populate_pokeset(pokeset, skip_ev_check=True)
except ValueError as ex:
print("{}> ERROR: {}".format(identifier, str(ex).encode("ascii", "replace").decode()))
else:
for warning in w:
print("{}> WARNING: {}".format(identifier, str(warning.message).encode("ascii", "replace").decode()))
genders_this_species = genders_per_species[pokeset["species"]["id"]]
genders_this_species |= set(pokeset["gender"])
if None in genders_this_species and len(genders_this_species) > 1:
print("{}> ERROR: Starting with this set, that species now has both genderless and genderful sets! "
"Stick to either genderless or genderful per species or PBR might crash!"
.format(identifier))
id = (pokeset["species"]["id"], pokeset["setname"])
if id in existing_ids:
prev_identifier = existing_ids[id]
print("{}> ERROR: combination of species {} ({}) and setname {} already exists ({}), but must be unique!"
.format(identifier, id[0], pokeset["species"]["name"], id[1], prev_identifier))
else:
existing_ids[id] = identifier
except yaml.YAMLError as e:
print("Error reading file: {}/{}: {}".format(dir, file, str(e).encode("ascii", "replace").decode()))
except:
print("Error reading file: {}/{}".format(dir, file))
raise