Skip to content

Commit 2caf87b

Browse files
committed
TEMPORARY: add migration script
1 parent d56743f commit 2caf87b

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

migrate.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import json
2+
import re
3+
import sys
4+
5+
re_subheading = re.compile(r'\n#{1,3} ')
6+
re_index_ignore = re.compile(r'[^A-Za-z0-9 -]')
7+
8+
def make_index(name):
9+
return re_index_ignore.sub('', name).replace(' ', '-').lower()
10+
11+
srd_index = {
12+
'name': 'System Reference Document 5.1',
13+
'index': 'srd',
14+
'url': '/api/rules/srd',
15+
}
16+
17+
section_lookup = dict()
18+
19+
output = {
20+
'srd': srd_index.copy(),
21+
}
22+
output['srd']['children'] = list()
23+
24+
# Massage rules to suit the new schema.
25+
with open('src/5e-SRD-Rules.json') as fh:
26+
for rule_in in json.load(fh):
27+
rule_out_ref = {
28+
'name': rule_in['name'],
29+
'index': rule_in['index'],
30+
'url': rule_in['url'],
31+
}
32+
rule_out = rule_out_ref.copy()
33+
34+
desc = rule_in['desc']
35+
if desc.startswith('#'):
36+
desc = '\n'.join(desc.split('\n')[1:]).strip()
37+
if desc:
38+
rule_out['desc'] = desc
39+
40+
rule_out['parent'] = srd_index
41+
if rule_in['subsections']:
42+
rule_out['children'] = [
43+
{
44+
'name': subsection['name'],
45+
'index': subsection['index'],
46+
'url': '/api/rules/' + subsection['index'],
47+
} for subsection in rule_in['subsections']
48+
]
49+
section_lookup.update({
50+
subsection['index']: rule_out_ref
51+
for subsection in rule_in['subsections']
52+
})
53+
54+
output[rule_out['index']] = rule_out
55+
output['srd']['children'].append(rule_out_ref)
56+
57+
# Import individual sections in the new schema.
58+
with open('src/5e-SRD-Rule-Sections.json') as fh:
59+
for rule_in in json.load(fh):
60+
rule_out_ref = {
61+
'name': rule_in['name'],
62+
'index': rule_in['index'],
63+
'url': '/api/rules/' + rule_in['index'],
64+
}
65+
rule_out = rule_out_ref.copy()
66+
67+
desc = rule_in['desc']
68+
if desc.startswith('#'):
69+
desc = '\n'.join(desc.split('\n')[1:]).strip()
70+
if desc:
71+
rule_out['desc'] = desc
72+
73+
rule_out['parent'] = section_lookup[rule_out['index']]
74+
75+
output[rule_out['index']] = rule_out
76+
77+
# Split sections containing subheadings.
78+
has_subheading = True
79+
while has_subheading:
80+
has_subheading = False
81+
for rule_index in list(output.keys()):
82+
rule = output[rule_index]
83+
if 'desc' in rule and re_subheading.search(rule['desc']):
84+
has_subheading = True
85+
rule_ref = {
86+
'name': rule['name'],
87+
'index': rule['index'],
88+
'url': rule['url'],
89+
}
90+
91+
min_depth = None
92+
for match in re_subheading.finditer(rule['desc']):
93+
if min_depth == None:
94+
min_depth = match.end() - match.start() - 2
95+
else:
96+
min_depth = min(min_depth, match.end() - match.start() - 2)
97+
98+
desc_parts = rule['desc'].split('\n' + ('#' * min_depth) + ' ')
99+
rule['desc'] = desc_parts[0].strip()
100+
for desc_part in desc_parts[1:]:
101+
(child_name, child_desc) = desc_part.split('\n', maxsplit = 1)
102+
child_index = make_index(child_name)
103+
104+
if child_index in output:
105+
print('Duplicate index "' + child_index + '" for "' +
106+
child_name + '"')
107+
sys.exit()
108+
109+
child_ref = {
110+
'name': child_name,
111+
'index': child_index,
112+
'url': '/api/rules/' + child_index,
113+
}
114+
115+
if 'children' in rule:
116+
rule['children'].append(child_ref)
117+
else:
118+
rule['children'] = [child_ref]
119+
120+
child = child_ref.copy()
121+
child['desc'] = child_desc.strip()
122+
child['parent'] = rule_ref
123+
124+
output[child_index] = child
125+
126+
json.dump(
127+
list(output.values()),
128+
sys.stdout,
129+
indent = 2,
130+
)

0 commit comments

Comments
 (0)