This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_mn_anki_exports.py
executable file
·346 lines (297 loc) · 8.93 KB
/
fix_mn_anki_exports.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
#!/usr/bin/env python3
#coding: UTF-8
import copy
import datetime
import json
import logging
import os
import re
import sqlite3
import subprocess
import sys
import tempfile
import time
import zipfile
from os.path import (
abspath,
basename,
dirname,
exists,
expanduser,
isabs,
join,
realpath,
)
import click
from w3lib.html import remove_tags
import genanki
from processors import run_fields_processors
OLDDIR = os.getcwd()
os.chdir(dirname(abspath(realpath(__file__))))
sys.path.insert(0, '.')
@click.group()
@click.pass_context
def main(ctx):
ctx.obj['foo'] = 'bar'
def _check_or_mkdir(a, *parts):
d = join(a, *parts)
if not exists(d):
os.mkdir(d)
return d
WORKDIR = '/tmp/mn-anki-exports-fix'
OUTPUT_FILE = '/tmp/output.apkg'
DBNAME = 'collection.anki2'
ANKI_FIELD_SEP = '\x1f'
def _fix_dbfile(dbfile):
with sqlite3.connect(dbfile) as db:
return _fix_db(db)
def load_file(name):
with open(join('files', name), 'r') as fp:
return fp.read()
def _fix_cloze_template(model):
template = copy.deepcopy(model['tmpls'][0])
template.update({
'qfmt': load_file('cloze_question.mustache'),
'afmt': load_file('cloze_answer.mustache'),
})
return [template]
def _fix_non_cloze_template(model):
template = copy.deepcopy(model['tmpls'][0])
template.update({
'qfmt': load_file('non_cloze_question.mustache'),
'afmt': load_file('non_cloze_answer.mustache'),
})
return [template]
def _model_from_db(db):
model_str = db.execute('SELECT models from col').fetchone()[0]
models = json.loads(model_str)
model_id = list(models)[0]
model = list(models.values())[0]
# Switch first (Front) & second (ClozeFront) field to use the
# latter as sort field
cloze_model_fields = copy.deepcopy(model['flds'])
_swap_first_two(cloze_model_fields)
css = model['css']
css += '\n' + load_file('custom.css')
cloze_model = genanki.Model(
int(model_id) + 1,
model['name'] + '_CustomCloze',
fields=cloze_model_fields,
templates=_fix_cloze_template(model),
css=css,
# Set type to cloze, this is very important!
type=1,
)
non_cloze_model_fields = [x for x in copy.deepcopy(model['flds'])
if x['name'] not in NON_CLOZE_EXCLUDED_FIELDS]
non_cloze_model = genanki.Model(
int(model_id) + 2,
model['name'] + '_Custom',
fields=non_cloze_model_fields,
templates=_fix_non_cloze_template(model),
css=css,
# Set type to non-cloze
type=0,
)
logging.info('Loaded the model')
return cloze_model, non_cloze_model
def _fix_cloze(value):
pattern = re.compile(r'(\{\{c1::.*?\}\})')
i = 0
def repl(s):
nonlocal i
i += 1
return s.group(0).replace('{{c1', '{{c%s' % i)
new_value = re.sub(pattern, repl, value)
return i, new_value
def _swap_first_two(l):
assert len(l) >= 2
l[0], l[1] = l[1], l[0]
return l
def _fix_cloze_note_fields(model, note):
# The first two fields has been swapped, we need to remap
field_names = _swap_first_two([x['name'] for x in model.fields])
fields = list(zip(field_names, note['flds'].split(ANKI_FIELD_SEP)))
fields_d = dict(fields)
sort_field = fields_d['ClozeFront']
fixed_fields = []
# Remember how many clozes are there so later we can make up the
# missing ones.
n_clozes = 0
for name, value in fields:
if name == 'ClozeFront':
n_clozes, value = _fix_cloze(value)
fixed_fields.append(value)
processed = run_fields_processors(dict(zip(fields_d, fixed_fields)))
fixed_fields = list(processed.values())
_swap_first_two(fixed_fields)
return n_clozes, sort_field, fixed_fields
def _load_deck(db):
deck_str = db.execute('SELECT decks from col').fetchone()[0]
decks = json.loads(deck_str)
deck = [x for x in decks.values() if x['name'] != 'Default'][0]
deck_id = deck['id']
loaded_deck = genanki.Deck(
deck_id=deck_id,
name=deck['name']
)
return loaded_deck
CARD_ATTRS = [
'id',
'nid',
'did',
'ord',
'mod',
'usn',
'type',
'queue',
'due',
'ivl',
'factor',
'reps',
'lapses',
'left',
'odue',
'odid',
'flags',
'data',
]
NOTE_ATTRS = [
"id",
"guid",
"mid",
"mod",
"usn",
"tags",
"flds",
"sfld",
"csum",
"flags",
"data",
]
_card_id = None
def get_card_id():
global _card_id
if _card_id is None:
_card_id = int(time.time() * 1000)
_card_id += 1
return _card_id
_note_id = None
def get_note_id():
global _note_id
if _note_id is None:
_note_id = int(time.time())
_note_id += 1
return _note_id
def _fix_cloze_cards(db, note_id, note, n_clozes):
# logging.info('n_clozes = %s', n_clozes)
if n_clozes == 0:
return
cards = db.execute('SELECT * FROM cards where nid = {}'.format(note_id)).fetchall()
if not cards or len(cards) > 1:
return
fixed_cards = [genanki.Card(card_ord, card_id=get_card_id())
for card_ord in range(n_clozes)]
setattr(note, 'cards', fixed_cards)
def is_empty_field(v):
return not bool(remove_tags(v).strip())
NON_CLOZE_EXCLUDED_FIELDS = (
'ClozeFront',
'ClozeBack',
)
def _fix_non_cloze_note_fields(non_cloze_model, fields):
fixed_fields = dict([
(name, fields[name]) for name in fields
if name not in NON_CLOZE_EXCLUDED_FIELDS
])
return list(run_fields_processors(fixed_fields).values())
def _fix_note(db, cloze_model, non_cloze_model, _note):
# Turn a sql row to a dict
note = dict(zip(NOTE_ATTRS, _note))
field_names = _swap_first_two([x['name'] for x in cloze_model.fields])
fields = dict(list(zip(field_names, note['flds'].split(ANKI_FIELD_SEP))))
if is_empty_field(fields['ClozeFront']):
# A non-cloze note
fixed_fields = _fix_non_cloze_note_fields(non_cloze_model, fields)
fixed_note = genanki.Note(
model=non_cloze_model,
guid=note['guid'],
fields=fixed_fields,
note_id=get_note_id(),
)
else:
n_clozes, sort_field, fields = _fix_cloze_note_fields(cloze_model, note)
fixed_note = genanki.Note(
model=cloze_model,
guid=note['guid'],
fields=fields,
sort_field=sort_field,
note_id=note['id'],
)
_fix_cloze_cards(db, note['id'], fixed_note, n_clozes)
return fixed_note
def _fix_db(db):
cloze_model, non_cloze_model = _model_from_db(db)
notes = db.execute('SELECT * FROM notes').fetchall()
logging.info('Loaded %s notes', len(notes))
fixed_notes = [_fix_note(db, cloze_model, non_cloze_model, note) for note in notes]
logging.info('Fixed all %s notes', len(notes))
deck = _load_deck(db)
logging.info('Loaded deck info: deck name = %s, id = %s', deck.deck_id, deck.name)
for note in fixed_notes:
deck.add_note(note)
logging.info('Generating output file %s', OUTPUT_FILE)
genanki.Package(deck).write_to_file(OUTPUT_FILE)
def _find_apkg():
path = subprocess.getoutput('ls -1t ~/Downloads/*.apkg|head -1').strip()
logging.info('Auto located apkg file %s', path)
return path
@main.command()
def model():
"""
This command uses a demo apkg. It's purpose is just to regenerate
the models to be imported into anki.
"""
_fix_path('files/testdeck.apkg')
@main.command()
@click.argument('path', default='auto')
def fix(path):
if path == 'auto':
path = _find_apkg()
_fix_path(path)
def _fix_path(path):
_check_or_mkdir(WORKDIR)
path = expanduser(path)
if not exists(path):
raise RuntimeError('{} doesn not exist!'.format(path))
with zipfile.ZipFile(path) as zfp:
logging.info('files: %s', zfp.namelist())
with tempfile.TemporaryDirectory(dir=WORKDIR) as tempdir:
zfp.extract(DBNAME, path=tempdir)
logging.info('Extracted %s to %s', DBNAME, tempdir)
dbfile = join(tempdir, DBNAME)
_fix_dbfile(dbfile)
def test_fix_cloze():
maps = {
'{{c1::hello}}': '{{c1::hello}}',
'{{c1::hello}} {{c1::world}}': '{{c1::hello}} {{c2::world}}',
'{{c1::hello}} my {{c1::world}}': '{{c1::hello}} my {{c2::world}}',
'{{c1::hello}} {{c1::world}} {{c1::hey}}': '{{c1::hello}} {{c2::world}} {{c3::hey}}',
}
for before, after in maps.items():
assert _fix_cloze(before) == after
def setup_logging(level=logging.INFO):
kw = {
'format': '[%(asctime)s][%(module)s]: %(message)s',
'datefmt': '%m/%d/%Y %H:%M:%S',
'level': level,
'stream': sys.stdout
}
logging.basicConfig(**kw)
if __name__ == '__main__':
setup_logging()
if len(sys.argv) > 1 and sys.argv[1] == 'test':
import pytest
pytest.main([__file__, '-v'] + sys.argv[2:])
else:
main(obj={})