-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
389 lines (334 loc) · 11 KB
/
run.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
import re, time
start_time = time.time()
head_before_title = "<!doctype html>\n<html>\n<head>\n\t<title>"
head_after_title=" Dynasty</title>\n<link rel='stylesheet' href='style.css'>\n</head>\n<body>"
end_body = "</body></html>"
html = ""
dynasties = {}
titles = {}
title_holders = {}
chars_done = []
children = {}
ho = ""
class Character(object):
fat_patt = re.compile("(father|fat)=(\d+)")
mot_patt = re.compile("(mother|mot)=(\d+)")
gender_string = "female=yes"
bn_patt = re.compile("(birth_name|bn)=\"(.*)\"")
dyn_patt = re.compile("dynasty=(\d+)")
holding_pattern_1 = re.compile('primary=\s*{\s*title="(.*?)"')
holding_pattern_2 = re.compile('old_holding="(.*?)"')
#def readable_title():
def __init__(self, character, block):
self.id = character
self.block = block
self.gender = "female" if self.gender_string in block else "male"
self.name = "Unknown"
self.father = -1
self.mother = -1
self.dynasty = dynasties[-1]
self.holding = ""
self.position = ""
name_match = self.bn_patt.search(block)
if (name_match != None):
self.name = name_match.group(2)
father_match = self.fat_patt.search(block)
if (father_match != None):
self.father = father_match.group(2)
children[self.father] = children.get(self.father, [])
children[self.father].append(character)
mother_match = self.mot_patt.search(block)
if (mother_match != None):
self.mother = mother_match.group(2)
children[self.mother] = children.get(self.mother, [])
children[self.mother].append(character)
dynasty_match = self.dyn_patt.search(block)
if (dynasty_match != None):
dynasty_id = dynasty_match.group(1)
self.dynasty = dynasties[dynasty_id]
holding_match = self.holding_pattern_1.search(block)
if (holding_match != None):
self.holding = holding_match.group(1)
else:
holding_match = self.holding_pattern_2.search(block)
if (holding_match != None):
self.holding = holding_match.group(1)
else:
if self.id in title_holders:
self.holding = title_holders[self.id]
if (self.holding != ""):
if any(x in self.holding for x in ['band', 'host', 'revolt']) or "_dyn_" in self.holding:
#print self.holding, " # ",
self.position = titles[self.holding].title
self.holding = titles[self.holding].name
#print self.holding
type = self.holding[0]
self.holding = self.holding[2:].capitalize()
if "_" in self.holding:
self.holding = " ".join([h.capitalize() for h in self.holding.split("_")])
if (type == "e"):
self.holding = ("Empress of " if self.gender == "female" else "Emperor of ") + self.holding
self.position = "emperor"
elif (type == "l"):
print "GGG " + self.position + " HHH " + self.holding
self.holding = self.position.capitalize() + " of " + self.holding
elif (type == "k"):
self.holding = ("Queen of " if self.gender == "female" else "King of ") + self.holding
self.position = "king"
elif (type == "d"):
self.holding = ("Duchess of " if self.gender == "female" else "Duke of ") + self.holding
self.position = "duke"
elif (type == "c"):
self.holding = ("Countess of " if self.gender == "female" else "Count of ") + self.holding
self.position = "count"
elif (type == "b"):
self.holding = ("Baroness of " if self.gender == "female" else "Baron of ") + self.holding
self.position = "baron"
class Dynasty(object):
name_patt = re.compile("name=\"(.*)\"")
def __init__(self, dynasty, block):
self.id = dynasty
self.name = "NoDYN"
name_match = self.name_patt.search(block)
if (name_match != None):
self.name = name_match.group(1)
class Title(object):
dynamic_holding_patt = re.compile("\n\t\t\tname=\"(.*?)\"")
holder_patt = re.compile("holder=(\d+)")
title_patt = re.compile('title="(.*?)"')
def __init__(self, title, block):
global ho
ho += title + "\n"
self.id = title
self.name = title
self.type = title[0]
self.title = "Leader"
if 'landless=yes' in block:
title_match = self.title_patt.search(block)
self.type = "l"
if (title_match != None):
self.title = title_match.group(1)
if "_dyn_" in title:
dynamic_holding_match = self.dynamic_holding_patt.search(block)
if (dynamic_holding_match != None):
self.name = (self.type + "_" + dynamic_holding_match.group(1)).replace(" ", "_").lower()
holder_match = self.holder_patt.search(block)
if (holder_match != None):
holder = holder_match.group(1)
current_held = title_holders.get(holder, " ")[0]
if (self.type == "b" and (current_held not in "ekdc")):
title_holders[holder] = self.name
elif (self.type == "c" and (current_held not in "ekd")):
title_holders[holder] = self.name
elif (self.type == "d" and (current_held not in "ek")):
title_holders[holder] = self.name
elif (self.type == "k" and (current_held not in "e")):
title_holders[holder] = self.name
elif (self.type == "e"):
title_holders[holder] = self.name
elif (self.type == "l"):
title_holders[holder] = self.name
titles[self.name] = self
def dict_by_list(char):
print "Beginning list method"
start_time = time.time()
char_patt = re.compile('(\d+)=')
it = char_patt.finditer(char)
characters = {}
l = list(it)
length = len(l)
print length
for i in xrange(length):
match = l[i]
character = match.group(1)
start = match.start()
if (i != (length - 1)):
characters[character] = Character(character, char[start:l[i+1].start()])
else:
characters[character] = Character(character, char[start:])
print len(characters)
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
return characters
def dict_by_iter(char):
print "Beginning iter method"
char_patt = re.compile('(\d+)=')
it = char_patt.finditer(char)
characters = {}
cnt = 0
start_time = time.time()
match = next(it, None)
start = match.start()
character = match.group(1)
while (match != None):
match = next(it, None)
if (match != None):
characters[character] = Character(character, char[start:match.start()])
else:
characters[character] = Character(character, char[start:])
break
start = match.start()
character = match.group(1)
cnt += 1
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
return characters
def get_characters(char):
return dict_by_list(char)
def dict_dyn(dyn):
print "Beginning list method"
start_time = time.time()
dyn_patt = re.compile('(\d+)=')
it = dyn_patt.finditer(dyn)
#dynasties = {}
l = list(it)
length = len(l)
print length
for i in xrange(length):
match = l[i]
dynasty = match.group(1)
start = match.start()
if (i != (length - 1)):
dynasties[dynasty] = Dynasty(dynasty, dyn[start:l[i+1].start()])
else:
dynasties[dynasty] = Dynasty(dynasty, dyn[start:])
dynasties[-1] = Dynasty(-1, "name=\"(Lowborn)\"")
print len(dynasties)
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
return dynasties
def get_dynasties(dyn):
return dict_dyn(dyn)
def update_dynasties(dyn):
return dict_dyn(dyn)
def dict_titles(raw_titles):
print "Beginning list method"
start_time = time.time()
title_patt = re.compile('([ekdcb]_[\d\w_]+)=\s+{')
it = title_patt.finditer(raw_titles)
l = list(it)
length = len(l)
print length
for i in xrange(length):
match = l[i]
title = match.group(1)
start = match.start()
if (i != (length - 1)):
titles[title] = Title(title, raw_titles[start:l[i+1].start()])
else:
titles[title] = Title(title, raw_titles[start:])
titles[-1] = Title("notitle", "")
print len(titles)
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
return titles
def get_titles(raw_titles):
return dict_titles(raw_titles)
def get_block(start, char):
lines = char.split('\n')
count = 0
block = ""
for line in lines:
for char in line:
block += char
if (char == '{'):
count += 1
elif (char == '}'):
count -= 1
if count == 0:
return block
block += "\n"
def get_ancestor(player, char, characters):
block = characters[player].block
bn = characters[player].name
print bn, "->",
father_reg = characters[player].father
if (father_reg == -1):
return player
father = characters[player].father
return get_ancestor(father, char, characters)
def get_children(player, characters, dynasties, titles, count = 0):
global html
current_parent = characters[player]
if (count == 0):
title = current_parent.name
html += head_before_title + title + head_after_title
html += "<ol><li>" + "<b>" + title + " " + current_parent.dynasty.name + ", " + current_parent.holding + "</b>"
chars_done.append(player)
total_kids = sorted(children.get(player, []))
boys = []
girls = []
for child in total_kids:
if (characters[child].gender == "female"):
girls.append(child)
else:
boys.append(child)
kids = boys + girls
html += "<ol>\n"
previous_parent_id = -2
for child in kids:
character = characters[child]
mother = characters[character.mother]
father = characters[character.father]
dynasty = character.dynasty
other_parent = father if (current_parent.gender == "female") else mother
if (other_parent.id != previous_parent_id):
html += "with " + other_parent.name + " " + other_parent.dynasty.name + ", " + other_parent.holding
previous_parent_id = other_parent.id
#for i in xrange(count):
# print " ",
# html += " "
#print character.id, character.gender,
if (character.position != ""):
html += '<li class="' + character.position + '">'
else:
html += '<li>'
html += '' + "<b>" + character.name + " " + character.dynasty.name + ", " + character.holding + "</b>"
#print character.name,
#print dynasty.name,
#print character.mother
#print " Mother: ", mother.name, #mother.dynasty,
#print dynasties[mother.dynasty].name
#print character.father, father.name, dynasties[father.dynasty].name,
#print dynasty.id,
if character.id not in chars_done:
get_children(character.id, characters, dynasties, titles, count + 1)
html += "</li>\n"
html += "</ol>\n"
s = open("save.ck2", "r").read();
player = s.find("player=");
dyn_start = s.find("dynasties=\n")
char_start = s.find("character=\n")
delayed_start = s.find("delayed_event=")
relat_start = s.find("\trelation=\n")
titles_start = s.find("\n\ttitle=")
nomad_start = s.find("\n\tnomad=")
print dyn_start, char_start, delayed_start
dyn = s[dyn_start:char_start]
#d = open("dynasties.cksour", "w")
#d.write(dynasties)
#d.close()
get_dynasties(dyn)
dyn = open("00_dynasties.txt", "r").read()
update_dynasties(dyn)
raw_titles = s[titles_start:nomad_start]
get_titles(raw_titles)
open("holdings.txt", "w").write(ho)
char = s[char_start:delayed_start]
#d = open("characters.cksour", "w")
#d.write(characters)
#d.close()
player = "606574"
characters = get_characters(char)
characters[-1] = Character(-1, "UNKNOWN2")
anc = get_ancestor(player, char, characters)
print "ROOT: " + anc
kdis = get_children(anc, characters, dynasties, titles)
html += "</li></ol>"
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
#print html
treepage = open("tree.html", "w")
treepage.write(html)
print len(chars_done)
print "END"