/
note_factory.py
443 lines (354 loc) · 13.4 KB
/
note_factory.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from abc import ABCMeta, abstractmethod
import re
from anki import collection, notes, models, importing, exporting
from aqt.utils import showInfo
from aqt import mw
from .const import *
from .factory import Factory
from .htmlement import fromstring
import xml.etree.ElementTree as ET
import re
class NoteFactory(Factory, metaclass=ABCMeta):
"""
"""
def __init__(self, path):
super().__init__(path)
self.init_decks()
self.init_notetypes()
self.data_key = ''
def init_decks(self):
"""
Check to see if the aoe2 and techtree decks exist and create if they do not
"""
self.base_deck_id = mw.col.decks.add_normal_deck_with_name(BASE_DECK).id
self.techtree_deck_id = mw.col.decks.add_normal_deck_with_name(TECH_TREE_DECK).id
def init_notetypes(self):
# TODO create the aoe2 notetypes rather than relying on them already existing
models = mw.col.models.all_names_and_ids()
self.cloze_model_id = None
self.reg_model_id = None
for model in models:
model
if model.name == AOE2_CLOZE_NOTE:
self.cloze_model_id = model.id
if model.name == AOE2_REG_NOTE:
self.reg_model_id = model.id
if self.cloze_model_id is None or self.reg_model_id is None:
raise Exception("Aoe2 note types can not be found.")
def _get_data_object(self, data_id):
return self.data['data'][f'{self.data_key}s'][str(data_id)]
def _get_available_ids_for_civ(self, civ):
return self.data['techtrees'][civ][f'{self.data_key}s']
def _get_string(self, str_id):
return self.strings[str(str_id)]
def _get_data_help_str(self, data_id):
return self._get_string(self._get_data_object(data_id)[AOE2_LANG_HELP_ID])
def _get_data_name_str(self, data_id):
return self._get_string(self._get_data_object(data_id)[AOE2_LANG_NAME_ID])
def _update_note_field(self, note, field, value, note_state):
current_value = note[field]
if current_value != value:
note[field] = value
if note_state > AOE2_NOTE_STATE_MODIFIED:
note_state = AOE2_NOTE_STATE_MODIFIED
return note_state
def get_notes(self):
new_notes = []
updated_notes = []
for civ in self.civ_names:
note, note_state = self.get_note_civ(civ)
if note_state == AOE2_NOTE_STATE_NEW:
new_notes.append(note)
elif note_state == AOE2_NOTE_STATE_MODIFIED:
updated_notes.append(note)
elif note_state == AOE2_NOTE_STATE_UNCHANGED:
pass
else:
raise Exception('Undefined behaviour')
return new_notes, updated_notes
@abstractmethod
def get_note_civ(self, civ):
pass
def _get_id_str(self, civ):
return ' '.join([AOE2_IMPORT_TAG, self.tag, civ])
def _get_note(self, civ, model_id):
note = None
note_state = AOE2_NOTE_STATE_UNCHANGED
id_str = self._get_id_str(civ)
nids = mw.col.find_notes(
mw.col.build_search_string(f'"{id_str}"', collection.SearchNode(field_name=AOE2_ID))
)
if len(nids) == 0:
note = mw.col.new_note(notetype=mw.col.models.get(model_id))
note_state = AOE2_NOTE_STATE_NEW
elif len(nids) == 1:
note = mw.col.get_note(nids[0])
else:
raise Exception(f'Muiltiple existing notes for ID {id_str} found.')
note_state = self._update_note_field(
note,
AOE2_ID,
id_str,
note_state,
)
# Add tags
note.add_tag(AOE2_IMPORT_TAG)
note.add_tag(civ)
note.add_tag(self.tag)
return note, note_state
def _get_new_elems_for_styling(self, text):
new_subelems = []
pattern = r'((?<!c)[+-]?\d+%?)'
new_text = ''
for segment in re.split(pattern, text):
if re.fullmatch(pattern, segment):
if segment[-1] == '%':
subelem = self._wrap_percent(segment)
else:
subelem = self._wrap_number(segment)
new_subelems.append(subelem)
else:
if len(new_subelems) == 0:
new_text += segment
else:
if new_subelems[-1].tail is None:
new_subelems[-1].tail = segment
else:
new_subelems[-1].tail += segment
return new_subelems, new_text
'''
Takes an ET element and adds digit styling to all the contained text
Recursive
'''
def _add_digit_styling(self, element, parent=None, parent_idx=None):
# Process the text in this element
new_subelems = []
new_parent_tail_elems = []
if element.text is not None:
new_subelems, element.text = self._get_new_elems_for_styling(element.text)
if element.tail is not None:
new_parent_tail_elems, element.tail = self._get_new_elems_for_styling(element.tail)
# Process the subelements
i = 0
while i < len(element):
new_tail_elems = self._add_digit_styling(element[i], parent=element, parent_idx=i)
for new_tail_elem in new_tail_elems:
i += 1
element.insert(i, new_tail_elem)
i += 1
# Add the new subelems
new_subelems.reverse()
for new_elem in new_subelems:
element.insert(0, new_elem)
return new_parent_tail_elems
def _wrap_percent(self, text):
return self._wrap_with_span(text, 'percentage')
def _wrap_number(self, text):
return self._wrap_with_span(text, 'number')
def _wrap_with_span(self, text, klass):
element = ET.Element('span', {'class': klass})
element.text = text
return element
def str_2_html_str(self, input_str):
element = fromstring(input_str, encoding='unicode')
return self._element_2_str(element)
# Element to str
def _element_2_str(self, element):
self._add_digit_styling(element)
return ET.tostring(element, encoding='unicode', method='html')
class RegNoteFactory(NoteFactory, metaclass=ABCMeta):
def __init__(self, path):
super().__init__(path)
def get_note_civ(self, civ):
note, note_state = self._get_note(civ, self.reg_model_id)
note_state = self._update_note_field(
note,
AOE2_REG_QUESTION,
self._get_question_str(civ),
note_state,
)
return note, note_state
@abstractmethod
def _get_question_str(self, civ):
return ''
@abstractmethod
def _get_pos_str(self, **kwargs):
return ''
@abstractmethod
def _get_neg_str(self, **kwargs):
return ''
class TechTreeRegNoteFactory(RegNoteFactory):
def __init__(self,
ids: list[int],
question: str,
tag: str,
path: str,
):
super(TechTreeRegNoteFactory, self).__init__(path)
self.deck_id = self.techtree_deck_id
self.ids = ids
self.question = question
self.tag = tag
def get_note_civ(self, civ):
note, note_state = super().get_note_civ(civ)
answer_set = False
for data_id in self.ids:
# Check if the civ receives tech
if data_id in self._get_available_ids_for_civ(civ):
note_state = self._update_note_field(
note,
AOE2_REG_ANSWER,
self._get_pos_str(data_id=str(data_id)),
note_state,
)
note_state = self._update_note_field(
note,
AOE2_REG_DESC,
self.str_2_html_str(self._get_data_help_str(data_id).split('<br>')[1]),
note_state,
)
answer_set = True
break
if not answer_set:
note_state = self._update_note_field(
note,
AOE2_REG_ANSWER,
self._get_neg_str(),
note_state,
)
return note, note_state
class TechNoteFactory(TechTreeRegNoteFactory):
"""e"""
def __init__(self,
ids: list[int],
question: str,
tag: str,
path: str,
):
super(TechNoteFactory, self).__init__(ids, question, tag, path)
self.data_key = 'tech'
class TechSeriesNoteFactory(TechNoteFactory):
def __init__(self, ids: list[int], question: str, tag: str, path: str):
super().__init__(ids, question, tag, path)
def _get_question_str(self, civ):
return self.question.format(civ)
def _get_pos_str(self, **kwargs):
return self._get_data_name_str(kwargs['data_id'])
def _get_neg_str(self, **kwargs):
return 'Not Available'
class TechSingleNoteFactory(TechNoteFactory):
def __init__(self, ids: list[int], tag: str, path: str):
question = 'Do {0} get {1}?'
super().__init__(ids, question, tag, path)
def _get_question_str(self, civ):
return self.question.format(
civ,
self._get_data_name_str(self.ids[0]),
)
def _get_pos_str(self, **kwargs):
return 'Yes'
def _get_neg_str(self, **kwargs):
return 'No'
class UnitNoteFactory(TechTreeRegNoteFactory):
def __init__(self,
ids: list[int],
question: str,
tag: str,
path: str,
):
super(UnitNoteFactory, self).__init__(ids, question, tag, path)
self.data_key = 'unit'
class UnitSeriesNoteFactory(UnitNoteFactory):
def __init__(self,
ids: list[int],
question: str,
tag: str,
path: str,
):
super().__init__(ids, question, tag, path)
def _get_question_str(self, civ):
return self.question.format(civ)
def _get_pos_str(self, **kwargs):
return self._get_data_name_str(kwargs['data_id'])
def _get_neg_str(self, **kwargs):
return 'Not Available'
class UnitSingleNoteFactory(UnitNoteFactory):
def __init__(self, ids: list[int], tag: str, path: str):
question = 'Do {0} get {1}?'
super().__init__(ids, question, tag, path)
def _get_question_str(self, civ):
return self.question.format(
civ,
self._get_data_name_str(self.ids[0]),
)
def _get_pos_str(self, **kwargs):
return 'Yes'
def _get_neg_str(self, **kwargs):
return 'No'
class ClozeNoteFactory(NoteFactory, metaclass=ABCMeta):
def __init__(self, path: str):
super().__init__(path)
self.cloze_cnt = 0
def _get_cloze(self, str):
self.cloze_cnt += 1
return '{{{{c{0}::{1}}}}}'.format(self.cloze_cnt, str)
def _reset_cloze(self):
self.cloze_cnt = 0
def get_note_civ(self, civ):
self._reset_cloze()
note, note_state = super()._get_note(civ, self.cloze_model_id)
note_state = self._update_note_field(
note,
AOE2_CLOZE_TEXT,
self._get_text_str(civ),
note_state,
)
return note, note_state
@abstractmethod
def _get_text_str(self, civ):
return ''
class CivBonusNoteFactory(ClozeNoteFactory):
def __init__(self, path: str):
super().__init__(path)
self.deck_id = self.techtree_deck_id
self.tag = 'civbonus'
def _get_civ_bonuses(self, bonus_str):
bonus_strs = [x.strip() for x in bonus_str.replace('\n','').replace(u'\u2022', ' ').split('<br>') if len(x) > 0]
uu_idx, ut_idx, tb_idx = None, None, None
for i in range(len(bonus_strs)):
if 'Unique Unit' in bonus_strs[i]: uu_idx = i
if 'Unique Techs' in bonus_strs[i]: ut_idx = i
if 'Team Bonus' in bonus_strs[i]: tb_idx = i
assert(uu_idx is not None and ut_idx is not None and tb_idx is not None)
return bonus_strs[1:uu_idx], bonus_strs[tb_idx+1:]
def _build_html(self, civ, bonuses, team_bonuses):
builder = ET.TreeBuilder()
builder.start('div', {'class':civ})
builder.start('span', {'class':civ})
builder.data(civ)
builder.start('br', {})
builder.start('ol', {})
for bonus in bonuses:
self._build_bonus_elem(builder, bonus, team_bonus=False)
for bonus in team_bonuses:
self._build_bonus_elem(builder, bonus, team_bonus=True)
builder.end('ol')
builder.end('div')
return self._element_2_str(builder.close())
def _build_bonus_elem(self, builder, bonus, team_bonus=False):
builder.start('li', {})
builder.data(bonus)
if team_bonus:
builder.start('span', {'class': 'teambonus'})
builder.data(' (team bonus)')
builder.end('span')
builder.end('li')
def _get_text_str(self, civ):
civ_help_id = self.data[AOE2_CIV_HELP][str(civ)]
bonuses, team_bonuses = self._get_civ_bonuses(self._get_string(civ_help_id))
text = self._build_html(
civ,
[self._get_cloze(x) for x in bonuses],
[self._get_cloze(x) for x in team_bonuses],
)
return text