def buildCardsAndDeck(path: Path): global AnkiNotes, AnkiModels, Anki_Collections, totalCardCount, FAILED_DECKS conn = sqlite3.connect(path.joinpath("collection.anki2").as_posix()) cursor = conn.cursor() cursor.execute( "SELECT * FROM cards ORDER BY factor ASC") # min ease would at rows[0] and max index would be at rows[-1] rows = cursor.fetchall() with IncrementalBar("\tBuilding Cards and deck", max=len(rows)) as bar: for row in rows: cid, nid, did, ordi, mod, usn, crtype, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data = row reqNote = AnkiNotes[str(nid)] genCard = None if reqNote.model.type == 0: reqTemplate = getTemplateofOrd(reqNote.model.tmpls, int(ordi)) questionTg = "<style> " + buildCssForOrd(reqNote.model.css, ordi) \ + "</style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.qfmt, buildStubbleDict(reqNote)) + "</section>" answerTag = "<style> " + buildCssForOrd(reqNote.model.css, ordi) \ + "</style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.afmt, buildStubbleDict(reqNote)) + "</section>" questionTg = premailer.transform(questionTg) answerTag = premailer.transform(answerTag) genCard = Card(cid, questionTg, answerTag) elif reqNote.model.type == 1: reqTemplate = getTemplateofOrd(reqNote.model.tmpls, 0) mustache.filters["cloze"] = lambda txt: Formatters.cloze_q_filter(txt, str(int(ordi) + 1)) css = reqNote.model.css css = buildCssForOrd(css, ordi) if css else "" questionTg = "<style> " + css + " </style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.qfmt, buildStubbleDict(reqNote)) + "</section>" mustache.filters["cloze"] = lambda txt: Formatters.cloze_a_filter(txt, str(int(ordi) + 1)) answerTag = "<section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.afmt, buildStubbleDict(reqNote)) + "</section>" questionTg = premailer.transform(questionTg) answerTag = premailer.transform(answerTag) genCard = Card(cid, questionTg, answerTag) if genCard is not None: reqDeck = getDeckFromID(Anki_Collections, str(did)) if reqDeck is not None: reqDeck.cards.append(genCard) else: if did not in FAILED_DECKS: FAILED_DECKS.append(did) else: if did not in FAILED_DECKS: FAILED_DECKS.append(did) totalCardCount += 1 bar.next() bar.finish()