def test_copy(): deck = DeckStorage.Deck() dir = tempfile.mkdtemp(prefix="anki") path = os.path.join(dir, "foo.jpg") open(path, "w").write("hello") # new file assert m.copyToMedia(deck, path) == "foo.jpg" # dupe md5 deck.s.statement(""" insert into media values (null, 'foo.jpg', 0, 0, :sum, '')""", sum=checksum("hello")) path = os.path.join(dir, "bar.jpg") open(path, "w").write("hello") assert m.copyToMedia(deck, path) == "foo.jpg"
def buildImg(deck, latex): log = open(os.path.join(tmpdir, "latex_log.txt"), "w+") texpath = os.path.join(tmpdir, "tmp.tex") texfile = file(texpath, "w") texfile.write(latexPreamble + "\n") texfile.write(latex + "\n") texfile.write(latexPostamble + "\n") texfile.close() texpath = texpath.encode(sys.getfilesystemencoding()) oldcwd = os.getcwd() if sys.platform == "win32": si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW else: si = None try: os.chdir(tmpdir) errmsg = _("Error executing 'latex' or 'dvipng'.\n" "A log file is available here:\n%s") % tmpdir if call(["latex", "-interaction=nonstopmode", texpath], stdout=log, stderr=log, startupinfo=si): return (False, errmsg) if call(latexDviPngCmd + ["tmp.dvi", "-o", "tmp.png"], stdout=log, stderr=log, startupinfo=si): return (False, errmsg) # add to media path = copyToMedia(deck, "tmp.png", latex=checksum(latex)) return (True, path) finally: os.chdir(oldcwd)
def recordFact(parent, fact, ordinal, count, total): recorded = False while 1: mb = QMessageBox(parent) mb.setWindowTitle("%d of %d" % (count, total)) mb.setTextFormat(Qt.RichText) # display string str = "" for field in fact.fields: str += "%s: %s<br>" % (field.name, field.value) mb.setText(str) # save bSave = QPushButton("Save and continue") mb.addButton(bSave, QMessageBox.RejectRole) if not recorded: bSave.setEnabled(False) # replay bReplay = QPushButton("Replay") mb.addButton(bReplay, QMessageBox.RejectRole) if not recorded: bReplay.setEnabled(False) # record (again) if recorded: bRecord = QPushButton("Record again") else: bRecord = QPushButton("Record") mb.addButton(bRecord, QMessageBox.RejectRole) # skip bSkip = QPushButton("Skip this fact") mb.addButton(bSkip, QMessageBox.RejectRole) # stop bStop = QPushButton("Stop bulk update") mb.addButton(bStop, QMessageBox.RejectRole) mb.exec_() if mb.clickedButton() == bRecord: recorded = getAudio(str, parent) continue elif mb.clickedButton() == bReplay: subprocess.Popen(audioPlayCommand + [os.path.abspath("tmp.mp3")]) continue elif mb.clickedButton() == bSave: new = copyToMedia(mw.deck, recorded) os.unlink("tmp.mp3") os.unlink("tmp.wav") os.unlink("tmp2.wav") os.unlink("tmp3.wav") fact.fields[ordinal].value = u"[sound:%s]" % new fact.setModified(textChanged=True) mw.deck.flushMod() mw.deck.save() editorObj.updateAfterCardChange() return True elif mb.clickedButton() == bSkip: return True elif mb.clickedButton() == bStop: return False
def test_db(): deck = DeckStorage.Deck() deck.addModel(BasicModel()) dir = tempfile.mkdtemp(prefix="anki") path = os.path.join(dir, "foo.jpg") open(path, "w").write("hello") # add a new fact that references it twice f = deck.newFact() f['Front'] = u"<img src='foo.jpg'>" f['Back'] = u"back [sound:foo.jpg]" deck.addFact(f) # 1 entry in the media db, with two references, and missing file assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 2 assert deck.s.scalar("select not originalPath from media") # copy to media folder & check db path = m.copyToMedia(deck, path) m.rebuildMediaDir(deck) # md5 should be set now assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 2 assert deck.s.scalar("select originalPath from media") # edit the fact to remove a reference f['Back'] = u"" f.setModified(True, deck) deck.s.flush() assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 1 # remove the front reference too f['Front'] = u"" f.setModified(True, deck) assert deck.s.scalar("select size from media") == 0 # add the reference back f['Front'] = u"<img src='foo.jpg'>" f.setModified(True, deck) assert deck.s.scalar("select size from media") == 1 # detect file modifications oldsum = deck.s.scalar("select originalPath from media") open(path, "w").write("world") m.rebuildMediaDir(deck) newsum = deck.s.scalar("select originalPath from media") assert newsum and newsum != oldsum # delete underlying file and check db os.unlink(path) m.rebuildMediaDir(deck) # md5 should be gone again assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select not originalPath from media") # media db should pick up media defined via templates & bulk update f['Back'] = u"bar.jpg" f.setModified(True, deck) deck.s.flush() # modify template & regenerate assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select sum(size) from media") == 1 deck.currentModel.cardModels[0].aformat=u'<img src="{{{Back}}}">' deck.updateCardsFromModel(deck.currentModel) assert deck.s.scalar("select sum(size) from media") == 2 assert deck.s.scalar("select count() from media") == 2 deck.currentModel.cardModels[0].aformat=u'{{{Back}}}' deck.updateCardsFromModel(deck.currentModel) assert deck.s.scalar("select count() from media") == 2 assert deck.s.scalar("select sum(size) from media") == 1