forked from rrgeorge/5eTtoFC5
-
Notifications
You must be signed in to change notification settings - Fork 2
/
race.py
254 lines (239 loc) · 12.7 KB
/
race.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
# vim: set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab : #
import xml.etree.cElementTree as ET
import re
import utils
import json
import os
import copy
from slugify import slugify
from wand.image import Image
def parseRace(m, compendium, args):
if '_copy' in m:
if args.verbose:
print("COPY: " + m['name'] + " from " + m['_copy']['name'] + " in " + m['_copy']['source'])
xtrsrc = "./data/races.json"
try:
with open(xtrsrc) as f:
d = json.load(f)
f.close()
mcpy = m
for mn in d['race']:
racefound = False
if mn['name'] == mcpy['_copy']['name'] and mn['source'] == mcpy['_copy']['source']:
m = copy.deepcopy(mn)
m['name'] = mcpy['name']
m['source'] = mcpy['source']
if "otherSources" in mcpy:
m["otherSources"] = mcpy["otherSources"]
if 'page' in mcpy:
m['page'] = mcpy['page']
elif 'page' in m:
del m['page']
if '_mod' in mcpy['_copy']:
m = utils.modifyItem(m,mcpy['_copy']['_mod'])
racefound = True
break
if not racefound:
print("Could not find ",mcpy['_copy']['name'])
except IOError as e:
if args.verbose:
print ("Could not load additional source ({}): {}".format(e.errno, e.strerror))
return
race = ET.SubElement(compendium, 'race')
name = ET.SubElement(race, 'name')
name.text = m['name']
if 'entries' not in m:
m['entries'] = []
if 'source' in m and not args.srd:
slug = slugify(m["name"])
if args.addimgs and os.path.isdir("./img") and not os.path.isfile("./items/" + slug + ".png"):
if not os.path.isdir("./items/"):
os.mkdir("./items/")
artworkpath = None
if os.path.isfile("./img/items/" + m["name"] + ".jpg"):
artworkpath = "./img/items/" + m["name"] + ".jpg"
elif os.path.isfile("./img/items/" + m["name"] + ".png"):
artworkpath = "./img/items/" + m["name"] + ".png"
elif os.path.isfile("./img/items/" + m["source"] + "/" + m["name"] + ".png"):
artworkpath = "./img/items/" + m["source"] + "/" + m["name"] + ".png"
if artworkpath is not None:
if args.verbose:
print("Converting Image: " + artworkpath)
with Image(filename=artworkpath) as img:
img.format='png'
img.save(filename="./items/" + slug + ".png")
imagetag = ET.SubElement(race, 'image')
imagetag.text = slug + ".png"
elif args.addimgs and os.path.isfile("./items/" + slug + ".png"):
imagetag = ET.SubElement(race, 'image')
imagetag.text = slug + ".png"
sourcetext = "{} p. {}".format(
utils.getFriendlySource(m['source'],args), m['page']) if 'page' in m and m['page'] != 0 else utils.getFriendlySource(m['source'],args)
if 'otherSources' in m and m["otherSources"] is not None:
for s in m["otherSources"]:
sourcetext += ", "
sourcetext += "{} p. {}".format(
utils.getFriendlySource(s["source"],args), s["page"]) if 'page' in s and s["page"] != 0 else utils.getFriendlySource(s["source"],args)
if 'entries' in m:
if args.nohtml:
m['entries'].append("Source: {}".format(sourcetext))
else:
m['entries'].append("<i>Source: {}</i>".format(sourcetext))
else:
if args.nohtml:
m['entries'] = ["Source: {}".format(sourcetext)]
else:
m['entries'] = ["<i>Source: {}</i>".format(sourcetext)]
if not args.nohtml:
source = ET.SubElement(race, 'source')
source.text = sourcetext
size = ET.SubElement(race, 'size')
if m['size'] == 'V':
size.text = 'M'
elif type(m['size']) == list:
size.text = m['size'][0]
else:
size.text = m['size']
speed = ET.SubElement(race, 'speed')
if type(m['speed']) == dict:
speed.text = str(m['speed']['walk'])
elif m['speed'] != "Varies":
speed.text = str(m['speed'])
else:
speed.text = "30"
ability = ET.SubElement(race, 'ability')
if 'ability' in m:
abils = []
for ab in m['ability']:
for k,v in ab.items():
if k != 'choose':
abils.append("{} {}".format(k,v).capitalize())
ability.text = ", ".join(abils)
prof = ET.SubElement(race, 'proficiency')
if 'skillProficiencies' in m:
profs = []
for skill in m['skillProficiencies']:
for k,v in skill.items():
if k != 'choose' and k != 'tool':
if v:
profs.append(k.title())
prof.text = ", ".join(profs)
spell = ET.SubElement(race, 'spellAbility')
trait = ET.SubElement(race, 'trait')
name = ET.SubElement(trait, 'name')
name.text = "Description"
description = ET.SubElement(trait, 'text')
description.text = ""
if 'entries' in m:
for e in m['entries']:
if type(e) == dict:
if 'name' in e:
trait = ET.SubElement(race, 'trait')
name = ET.SubElement(trait, 'name')
name.text = e['name']
text = ET.SubElement(trait, 'text')
text.text = ""
else:
text = description
if "colLabels" in e:
if 'caption' in e:
text.text += "{}\n".format(e['caption'])
text.text += " | ".join([utils.remove5eShit(x)
for x in e['colLabels']])
text.text += "\n"
for row in e['rows']:
rowthing = []
for r in row:
if isinstance(r, dict) and 'roll' in r:
rowthing.append(
"{}-{}".format(
r['roll']['min'],
r['roll']['max']) if 'min' in r['roll'] else str(
r['roll']['exact']))
else:
rowthing.append(utils.fixTags(str(r),m,args.nohtml))
text.text += " | ".join(rowthing) + "\n"
elif "entries" in e:
subentries = []
for sube in e["entries"]:
if type(sube) == str:
subentries.append(utils.fixTags(sube,m,args.nohtml))
elif type(sube) == dict and "text" in sube:
subentries.append(utils.fixTags(sube["text"],m,args.nohtml))
elif type(sube) == dict and sube["type"] == "list" and "style" in sube and sube["style"] == "list-hang-notitle":
for item in sube["items"]:
if type(item) == dict and 'type' in item and (item['type'] == 'item' or item['type'] == 'itemSpell'):
if 'entry' not in item and 'entries' in item:
item['entry'] = "; ".join(item['entries'])
if args.nohtml:
subentries.append("• {} {}".format(item["name"] if item["name"].endswith(':') else item["name"] + ':',utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• <i>{}</i> {}".format(item["name"] if item["name"].endswith(':') else item["name"] + ':',utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• {}".format(utils.fixTags(item,m,args.nohtml)))
elif type(sube) == dict and sube["type"] == "list":
for item in sube["items"]:
if type(item) == dict and "entries" in item:
ssubentries = []
for sse in item["entries"]:
if type(sse) == str:
ssubentries.append(utils.fixTags(sse,m,args.nohtml))
elif type(sse) == dict and "text" in sse:
ssubentries.append(utils.fixTags(sse["text"],m,args.nohtml))
subentries.append("\n".join(ssubentries))
elif type(item) == dict and 'type' in item and item['type'] == 'item':
if args.nohtml:
subentries.append("• {}: {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• <i>{}:</i> {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• {}".format(utils.fixTags(item,m,args.nohtml)))
else:
if "colLabels" in sube:
tabletext = ""
if 'caption' in sube:
text.text += "{}\n".format(sube['caption'])
tabletext += " | ".join([utils.remove5eShit(x)
for x in sube['colLabels']])
tabletext += "\n"
for row in sube['rows']:
rowthing = []
for r in row:
if isinstance(r, dict) and 'roll' in r:
rowthing.append(
"{}-{}".format(
r['roll']['min'],
r['roll']['max']) if 'min' in r['roll'] else str(
r['roll']['exact']))
else:
rowthing.append(utils.fixTags(str(r),m,args.nohtml))
tabletext += " | ".join(rowthing) + "\n"
subentries.append(tabletext)
text.text += "\n".join(subentries) + "\n"
else:
if type(e) == dict and e["type"] == "list" and "style" in e and e["style"] == "list-hang-notitle":
for item in e["items"]:
if 'entries' in item and 'entry' not in item:
item['entry'] = ", ".join(item['entries'])
if args.nohtml:
text.text += "• {}: {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)) + "\n"
else:
text.text += "• <i>{}:</i> {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)) + "\n"
elif type(e) == dict and e["type"] == "list":
for item in e["items"]:
if "entries" in item:
subentries = []
for sube in item["entries"]:
if type(sube) == str:
subentries.append(utils.fixTags(sube,m,args.nohtml))
elif type(sube) == dict and "text" in sube:
subentries.append(utils.fixTags(sube["text"],m,args.nohtml))
text.text += "\n".join(subentries) + "\n"
else:
text.text += "• {}".format(utils.fixTags(item,m,args.nohtml)) + "\n"
match = re.search(r'(\w+) is your spellcasting ability', text.text)
if match:
spell.text = match.group(1)
else:
description.text += utils.fixTags(e,m,args.nohtml) + "\n"
description.text = description.text.rstrip()