2019-12-25 22:36:26 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki import Collection as aopen
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def assertException(exception, func):
|
|
|
|
found = False
|
|
|
|
try:
|
|
|
|
func()
|
|
|
|
except exception:
|
|
|
|
found = True
|
|
|
|
assert found
|
|
|
|
|
2014-02-13 08:33:44 +01:00
|
|
|
|
|
|
|
# Creating new decks is expensive. Just do it once, and then spin off
|
|
|
|
# copies from the master.
|
2019-12-06 09:37:39 +01:00
|
|
|
def getEmptyCol():
|
2014-06-03 10:38:47 +02:00
|
|
|
if len(getEmptyCol.master) == 0:
|
2014-02-13 08:33:44 +01:00
|
|
|
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2014-02-13 08:33:44 +01:00
|
|
|
os.unlink(nam)
|
|
|
|
col = aopen(nam)
|
|
|
|
col.db.close()
|
2014-06-03 10:38:47 +02:00
|
|
|
getEmptyCol.master = nam
|
2014-02-13 08:33:44 +01:00
|
|
|
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
|
2014-06-03 10:38:47 +02:00
|
|
|
shutil.copy(getEmptyCol.master, nam)
|
2018-01-14 07:58:12 +01:00
|
|
|
col = aopen(nam)
|
|
|
|
return col
|
2014-02-13 08:33:44 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2014-06-03 10:38:47 +02:00
|
|
|
getEmptyCol.master = ""
|
2014-02-13 08:33:44 +01:00
|
|
|
|
|
|
|
# Fallback for when the DB needs options passed in.
|
|
|
|
def getEmptyDeckWith(**kwargs):
|
2012-12-21 08:51:59 +01:00
|
|
|
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
|
2014-04-21 23:04:46 +02:00
|
|
|
os.close(fd)
|
2012-12-21 08:51:59 +01:00
|
|
|
os.unlink(nam)
|
|
|
|
return aopen(nam, **kwargs)
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def getUpgradeDeckPath(name="anki12.anki"):
|
|
|
|
src = os.path.join(testDir, "support", name)
|
|
|
|
(fd, dst) = tempfile.mkstemp(suffix=".anki2")
|
|
|
|
shutil.copy(src, dst)
|
2016-05-12 06:45:35 +02:00
|
|
|
return dst
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
testDir = os.path.dirname(__file__)
|