~jmehne/mnemosyne-proj/pbienst

« back to all changes in this revision

Viewing changes to mnemosyne/mnemosyne/libmnemosyne/file_formats/anki2.py

  • Committer: Peter Bienstman
  • Date: 2017-08-26 09:08:33 UTC
  • Revision ID: peter.bienstman@ugent.be-20170826090833-wwe3ydld8mzurv61
Syncing of Anki card types.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import sqlite3
11
11
import zipfile
12
12
 
 
13
from xml.sax.saxutils import escape as xml_escape
 
14
 
13
15
from mnemosyne.libmnemosyne.fact import Fact
14
16
from mnemosyne.libmnemosyne.card import Card
15
17
from mnemosyne.libmnemosyne.translator import _
222
224
            # csum: checksum, ignore.
223
225
            # flags: seems empty, ignore.
224
226
            # data: seems empty, ignore.
 
227
            guid = xml_escape(guid)  # Make compatible with openSM2sync.
225
228
            modification_time_for_nid[id] = mod
226
229
            card_type = card_type_for_mid[int(mid)]
227
230
            card_type_for_nid[id] = card_type
254
257
            fact_for_nid[id] = fact
255
258
            tag_names_for_nid[id] = tags
256
259
            w.increase_progress(1)
 
260
        # Import logs. This needs to happen before creating the cards,
 
261
        # otherwise, the sync protocol will use the scheduling data from the
 
262
        # latest repetition log, instead of the correct current one.
 
263
        w.set_progress_text(_("Importing logs..."))
 
264
        number_of_logs = con.execute("select count() from revlog").fetchone()[0]
 
265
        w.set_progress_range(number_of_logs)
 
266
        w.set_progress_update_interval(number_of_logs/20)
 
267
        for id, cid, usn, ease, ivl, lastIvl, factor, time, type_ in \
 
268
            con.execute("""select id, cid, usn, ease, ivl, lastIvl, factor,
 
269
            time, type from revlog"""):
 
270
            # usn: syncing related, ignore.
 
271
            if type_ == 0:  # Acquisition phase.
 
272
                grade = 0
 
273
            else:  # Retention phase.
 
274
                grade = ease + 1  # Anki ease is from 1 to 4.
 
275
            timestamp = int(id/1000)
 
276
            scheduled_interval = lastIvl*86400 if lastIvl > 0 else 0
 
277
            new_interval = ivl*86400 if ivl > 0 else 0
 
278
            next_rep = timestamp + new_interval
 
279
            easiness = factor/1000 if factor else 2.5
 
280
            db.log_repetition(timestamp=timestamp, card_id=cid, grade=grade,
 
281
                easiness=easiness, acq_reps=0, ret_reps=0, lapses=0,
 
282
                acq_reps_since_lapse=0, ret_reps_since_lapse=0,
 
283
                scheduled_interval=scheduled_interval,
 
284
                actual_interval=scheduled_interval,
 
285
                thinking_time=int(time/1000), next_rep=next_rep,
 
286
                scheduler_data=0)
 
287
            w.increase_progress(1)
257
288
        # Import cards.
258
289
        w.set_progress_text(_("Importing cards..."))
259
290
        number_of_cards = con.execute("select count() from cards").fetchone()[0]
328
359
            else:
329
360
                db.add_card(card)
330
361
            w.increase_progress(1)
331
 
        # Import logs.
332
 
        w.set_progress_text(_("Importing logs..."))
333
 
        number_of_logs = con.execute("select count() from revlog").fetchone()[0]
334
 
        w.set_progress_range(number_of_logs)
335
 
        w.set_progress_update_interval(number_of_logs/20)
336
 
        for id, cid, usn, ease, ivl, lastIvl, factor, time, type_ in \
337
 
            con.execute("""select id, cid, usn, ease, ivl, lastIvl, factor,
338
 
            time, type from revlog"""):
339
 
            # usn: syncing related, ignore.
340
 
            if type_ == 0:  # Acquisition phase.
341
 
                grade = 0
342
 
            else:  # Retention phase.
343
 
                grade = ease + 1  # Anki ease is from 1 to 4.
344
 
            timestamp = int(id/1000)
345
 
            scheduled_interval = lastIvl*86400 if lastIvl > 0 else 0
346
 
            new_interval = ivl*86400 if ivl > 0 else 0
347
 
            next_rep = timestamp + new_interval
348
 
            easiness = factor/1000 if factor else 2.5
349
 
            db.log_repetition(timestamp=timestamp, card_id=cid, grade=grade,
350
 
                easiness=easiness, acq_reps=0, ret_reps=0, lapses=0,
351
 
                acq_reps_since_lapse=0, ret_reps_since_lapse=0,
352
 
                scheduled_interval=scheduled_interval,
353
 
                actual_interval=scheduled_interval,
354
 
                thinking_time=int(time/1000), next_rep=next_rep,
355
 
                scheduler_data=0)
356
 
            w.increase_progress(1)
357
362
        # Create criteria for 'database' tags.
358
363
        for deck_name in deck_name_for_did.values():
359
364
            deck_name = deck_name.strip().replace(",", ";")