2021-04-13 10:45:05 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# coding: utf-8
|
|
|
|
|
2019-12-25 22:36:26 +01:00
|
|
|
import os
|
|
|
|
import tempfile
|
2019-12-24 12:42:40 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki import Collection as aopen
|
|
|
|
from anki.exporting import *
|
|
|
|
from anki.importing import Anki2Importer
|
2020-03-22 05:41:01 +01:00
|
|
|
from tests.shared import errorsAfterMidnight
|
2019-12-27 12:14:52 +01:00
|
|
|
from tests.shared import getEmptyCol as getEmptyColOrig
|
|
|
|
|
|
|
|
|
|
|
|
def getEmptyCol():
|
|
|
|
col = getEmptyColOrig()
|
2021-02-21 06:50:41 +01:00
|
|
|
col.upgrade_to_v2_scheduler()
|
2019-12-27 12:14:52 +01:00
|
|
|
return col
|
2019-12-25 22:36:26 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-17 05:21:01 +02:00
|
|
|
col = None
|
2012-12-21 08:51:59 +01:00
|
|
|
ds = None
|
|
|
|
testDir = os.path.dirname(__file__)
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def setup1():
|
2020-07-17 05:21:01 +02:00
|
|
|
global col
|
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "foo"
|
|
|
|
note["Back"] = "bar<br>"
|
|
|
|
note.tags = ["tag", "tag2"]
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
|
|
|
# with a different col
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "baz"
|
|
|
|
note["Back"] = "qux"
|
2021-06-27 04:12:23 +02:00
|
|
|
note.note_type()["did"] = col.decks.id("new col")
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
##########################################################################
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_export_anki():
|
2020-01-02 23:52:10 +01:00
|
|
|
setup1()
|
2020-07-17 05:21:01 +02:00
|
|
|
# create a new col with its own conf to test conf copying
|
|
|
|
did = col.decks.id("test")
|
|
|
|
dobj = col.decks.get(did)
|
|
|
|
confId = col.decks.add_config_returning_id("newconf")
|
|
|
|
conf = col.decks.get_config(confId)
|
2019-12-25 05:18:34 +01:00
|
|
|
conf["new"]["perDay"] = 5
|
2020-07-17 05:21:01 +02:00
|
|
|
col.decks.save(conf)
|
2021-06-27 06:02:48 +02:00
|
|
|
col.decks.set_config_id_for_deck_dict(dobj, confId)
|
2012-12-21 08:51:59 +01:00
|
|
|
# export
|
2020-07-17 05:21:01 +02:00
|
|
|
e = AnkiExporter(col)
|
2014-04-21 23:04:46 +02:00
|
|
|
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
|
2016-05-12 06:45:35 +02:00
|
|
|
newname = str(newname)
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2012-12-21 08:51:59 +01:00
|
|
|
os.unlink(newname)
|
|
|
|
e.exportInto(newname)
|
|
|
|
# exporting should not have changed conf for original deck
|
2021-06-27 06:02:48 +02:00
|
|
|
conf = col.decks.config_dict_for_deck_id(did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert conf["id"] != 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# connect to new deck
|
2020-07-17 17:55:57 +02:00
|
|
|
col2 = aopen(newname)
|
2021-06-27 07:12:22 +02:00
|
|
|
assert col2.card_count() == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# as scheduling was reset, should also revert decks to default conf
|
2020-07-17 17:55:57 +02:00
|
|
|
did = col2.decks.id("test", create=False)
|
2012-12-21 08:51:59 +01:00
|
|
|
assert did
|
2021-06-27 06:02:48 +02:00
|
|
|
conf2 = col2.decks.config_dict_for_deck_id(did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert conf2["new"]["perDay"] == 20
|
2020-07-17 17:55:57 +02:00
|
|
|
dobj = col2.decks.get(did)
|
2012-12-21 08:51:59 +01:00
|
|
|
# conf should be 1
|
2019-12-25 05:18:34 +01:00
|
|
|
assert dobj["conf"] == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# try again, limited to a deck
|
2014-04-21 23:04:46 +02:00
|
|
|
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
|
2016-05-12 06:45:35 +02:00
|
|
|
newname = str(newname)
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2012-12-21 08:51:59 +01:00
|
|
|
os.unlink(newname)
|
|
|
|
e.did = 1
|
|
|
|
e.exportInto(newname)
|
2020-07-17 17:55:57 +02:00
|
|
|
col2 = aopen(newname)
|
2021-06-27 07:12:22 +02:00
|
|
|
assert col2.card_count() == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_export_ankipkg():
|
2020-01-02 23:52:10 +01:00
|
|
|
setup1()
|
2012-12-21 08:51:59 +01:00
|
|
|
# add a test file to the media folder
|
2020-07-17 05:21:01 +02:00
|
|
|
with open(os.path.join(col.media.dir(), "今日.mp3"), "w") as note:
|
2020-07-17 05:18:09 +02:00
|
|
|
note.write("test")
|
2020-07-17 05:21:01 +02:00
|
|
|
n = col.newNote()
|
2019-12-25 05:18:34 +01:00
|
|
|
n["Front"] = "[sound:今日.mp3]"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(n)
|
|
|
|
e = AnkiPackageExporter(col)
|
2014-04-21 23:04:46 +02:00
|
|
|
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".apkg")
|
2016-05-12 06:45:35 +02:00
|
|
|
newname = str(newname)
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2012-12-21 08:51:59 +01:00
|
|
|
os.unlink(newname)
|
|
|
|
e.exportInto(newname)
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2020-03-22 05:41:01 +01:00
|
|
|
@errorsAfterMidnight
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_export_anki_due():
|
2020-01-02 23:52:10 +01:00
|
|
|
setup1()
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "foo"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
|
|
|
col.crt -= 86400 * 10
|
|
|
|
col.sched.reset()
|
|
|
|
c = col.sched.getCard()
|
|
|
|
col.sched.answerCard(c, 3)
|
|
|
|
col.sched.answerCard(c, 3)
|
2012-12-21 08:51:59 +01:00
|
|
|
# should have ivl of 1, due on day 11
|
|
|
|
assert c.ivl == 1
|
|
|
|
assert c.due == 11
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.sched.today == 10
|
|
|
|
assert c.due - col.sched.today == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# export
|
2020-07-17 05:21:01 +02:00
|
|
|
e = AnkiExporter(col)
|
2012-12-21 08:51:59 +01:00
|
|
|
e.includeSched = True
|
2014-04-21 23:04:46 +02:00
|
|
|
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
|
2016-05-12 06:45:35 +02:00
|
|
|
newname = str(newname)
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2012-12-21 08:51:59 +01:00
|
|
|
os.unlink(newname)
|
|
|
|
e.exportInto(newname)
|
|
|
|
# importing into a new deck, the due date should be equivalent
|
2020-07-17 17:27:40 +02:00
|
|
|
col2 = getEmptyCol()
|
|
|
|
imp = Anki2Importer(col2, newname)
|
2012-12-21 08:51:59 +01:00
|
|
|
imp.run()
|
2020-07-17 17:27:40 +02:00
|
|
|
c = col2.getCard(c.id)
|
|
|
|
col2.sched.reset()
|
|
|
|
assert c.due - col2.sched.today == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# def test_export_textcard():
|
2020-01-02 23:52:10 +01:00
|
|
|
# setup1()
|
2020-07-17 05:21:01 +02:00
|
|
|
# e = TextCardExporter(col)
|
2020-07-17 05:18:09 +02:00
|
|
|
# note = unicode(tempfile.mkstemp(prefix="ankitest")[1])
|
|
|
|
# os.unlink(note)
|
|
|
|
# e.exportInto(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
# e.includeTags = True
|
2020-07-17 05:18:09 +02:00
|
|
|
# e.exportInto(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_export_textnote():
|
2020-01-02 23:52:10 +01:00
|
|
|
setup1()
|
2020-07-17 05:21:01 +02:00
|
|
|
e = TextNoteExporter(col)
|
2020-07-17 05:18:09 +02:00
|
|
|
fd, note = tempfile.mkstemp(prefix="ankitest")
|
|
|
|
note = str(note)
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2020-07-17 05:18:09 +02:00
|
|
|
os.unlink(note)
|
|
|
|
e.exportInto(note)
|
|
|
|
with open(note) as file:
|
2020-05-10 02:58:42 +02:00
|
|
|
assert file.readline() == "foo\tbar<br>\ttag tag2\n"
|
2019-03-04 23:57:53 +01:00
|
|
|
e.includeTags = False
|
|
|
|
e.includeHTML = False
|
2020-07-17 05:18:09 +02:00
|
|
|
e.exportInto(note)
|
|
|
|
with open(note) as file:
|
2020-05-10 02:58:42 +02:00
|
|
|
assert file.readline() == "foo\tbar\n"
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_exporters():
|
2020-11-19 20:59:24 +01:00
|
|
|
assert "*.apkg" in str(exporters(getEmptyCol()))
|