-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_groups.py
233 lines (218 loc) · 8.23 KB
/
generate_groups.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
# Python script to generate the groups.rs file
# This script parses the dataset file of tohosort.
import re
import sys
if len(sys.argv) <= 1:
print("Usage: generate_groups.py [tohosort data.js]")
sys.exit()
else:
file_name = sys.argv[1]
with open(file_name, 'r') as data, open('groups.rs', 'w') as output:
# PART 0 - preamble
output.write("// Character groups for detailed stats\n")
line = data.readline()
date = re.search(r'dataSetVersion = "([^"]*)"', line)
output.write("// Generated from tohosort's dataset: " + date.group(1) + "\n")
output.write("\n")
output.write("use serde::{Serialize, Deserialize};\n")
output.write("use std::str::FromStr;\n")
output.write("use strum_macros::EnumIter;\n")
output.write("\n")
output.write("// Tell the compiler to stop complaining\n")
output.write("#[allow(non_camel_case_types)]\n")
output.write("\n")
output.write("// Group by the work they appeared in\n")
output.write("// taken from tohosort\n")
output.write("\n")
# PART 1 - enum tags
output.write("#[derive(Hash, Eq, PartialEq, Serialize, Deserialize, Debug, Clone, EnumIter)]\n")
output.write("pub enum Tags {\n")
## start parsin'
while line := data.readline():
match = re.search(r'Filter by Series Entry', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
## format: { name: "*NAME*", tooltip: "*TITLE*", key: "*KEY*" },
## --> *KEY*,
start = False
while line := data.readline():
match = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for tag in match:
output.write("\t" + tag + ",\n")
elif start:
break
output.write("\t// Stages\n")
## stage tags
while line := data.readline():
match = re.search(r'Filter by Stage Enemy Appearances', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
start = False
while line := data.readline():
match = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for stage in match:
output.write("\t" + stage + ",\n")
elif start:
break
output.write("}\n\n")
# PART 2 - enum -> group name
## preamble
output.write("impl Tags {\n")
output.write("\t// Names of groups (touhou works and stages)\n")
output.write("\tpub fn name(&self) -> &'static str {\n")
output.write("\t\tmatch *self {\n")
## parse!
data.seek(0)
while line := data.readline():
match = re.search(r'Filter by Series Entry', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
## format: { name: "*NAME*", tooltip: "*TITLE*", key: "*KEY*" },
## --> Tag::*KEY* => "*NAME*"
start = False
while line := data.readline():
match = re.findall(r'name: ("[^"]*").*?key: "(.*)"', line)
if match:
start = True
for (name, key) in match:
output.write("\t\t\tTags::" + key + "\t=> " + name + ",\n")
elif start:
break
## stage
output.write("\t\t\t// Stages\n")
while line := data.readline():
match = re.search(r'Filter by Stage Enemy Appearances', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
start = False
while line := data.readline():
match = re.findall(r'name: ("[^"]*").*?key: "([^"]*)"', line)
if match:
start = True
for (name, tag) in match:
output.write("\t\t\tTags::" + tag + "\t=> " + name + ",\n")
elif start:
break
output.write("\t\t}\n\t}\n")
# PART 3 - enum -> touhou titles
## preamble
output.write("\t// Touhou game titles.\n")
output.write("\tpub fn exname(&self) -> &'static str {\n")
output.write("\t\tmatch *self {\n")
## parse!
data.seek(0)
while line := data.readline():
match = re.search(r'Filter by Series Entry', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
## format: { name: "*NAME*", tooltip: "*TITLE*", key: "*KEY*" },
## --> Tag::*KEY* => "*NAME*"
start = False
while line := data.readline():
match = re.findall(r'tooltip: ("[^"]*").*?key: "([^"]*)"', line)
match2 = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for (title, key) in match:
output.write("\t\t\tTags::" + key + "\t=> " + title + ",\n")
elif match2:
# "book" doesn't have a tooltip
start = True
for key in match2:
output.write("\t\t\tTags::" + key + "\t=> \"\",\n")
elif start:
break
## stages have no titles, but we need to include them still
output.write("\t\t\t// Stages\n")
while line := data.readline():
match = re.search(r'Filter by Stage Enemy Appearances', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
start = False
while line := data.readline():
match = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for tag in match:
output.write("\t\t\tTags::" + tag + "\t=> \"\",\n")
elif start:
break
output.write("\t\t}\n\t}\n")
# PART 4 - separate series and stages
output.write("\t// Returns true if it's a series tag\n")
output.write("\tpub fn is_series_tag(&self) -> bool {\n")
output.write("\t\tmatch self {\n")
data.seek(0)
while line := data.readline():
match = re.search(r'Filter by Stage Enemy Appearances', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
start = False
while line := data.readline():
match = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for tag in match:
output.write("\t\t\tTags::" + tag + "\t=> false,\n")
elif start:
break
output.write("\t\t\t_\t=> true,\n\t\t}\n\t}\n}\n")
# PART 5 - string -> enum
output.write("\n// String to name utility\n")
output.write("impl FromStr for Tags {\n")
output.write("\ttype Err = ();\n")
output.write("\tfn from_str(input: &str) -> Result<Tags, Self::Err> {\n")
output.write("\t\tmatch input.to_lowercase().as_str() {\n")
## start parsin'
data.seek(0)
while line := data.readline():
match = re.search(r'Filter by Series Entry', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
## format: { name: "*NAME*", tooltip: "*TITLE*", key: "*KEY*" },
## --> "*KEY*" => Ok(Tags::*KEY*),
start = False
while line := data.readline():
match = re.findall(r'tooltip: "([^ ]*).*key: "([^"]*)"', line)
match2 = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for (number, tag) in match:
output.write("\t\t\t\"" + tag.lower() + "\"\t| \"th" + number + "\"\t=> Ok(Tags::" + tag + "),\n")
elif match2:
# "book" doesn't have a tooltip
start = True
for tag in match2:
output.write("\t\t\t\"" + tag.lower() + "\"\t=> Ok(Tags::" + tag + "),\n")
elif start:
break
output.write("\t\t\t// Stages\n")
## stage tags
while line := data.readline():
match = re.search(r'Filter by Stage Enemy Appearances', line)
if match:
data.readline() # the next line is key for the option so we skip that
break
start = False
while line := data.readline():
match = re.findall(r'key: "([^"]*)"', line)
if match:
start = True
for stage in match:
output.write("\t\t\t\"" + stage.lower() + "\"\t=> Ok(Tags::" + stage + "),\n")
elif start:
break
output.write("\t\t\t_\t=> Err(()),\n")
output.write("\t\t}\n\t}\n}")