-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_file.py
28 lines (26 loc) · 896 Bytes
/
import_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from anki.importing import TextImporter
from aqt import mw
from os.path import dirname, join, realpath
from aqt.utils import showWarning, tooltip
from sqlite3 import connect
db_path = join(dirname(realpath(__file__)), 'database.db')
conn = connect(db_path)
c = conn.cursor()
config = mw.addonManager.getConfig(__name__)
language = config['language']
def importfile(filename, deck, type):
try:
file = join(dirname(realpath(__file__)), filename)
did = mw.col.decks.id(deck)
mw.col.decks.select(did)
m = mw.col.models.byName(type)
deck = mw.col.decks.get(did)
deck['mid'] = m['id']
mw.col.decks.save(deck)
m['did'] = did
ti = TextImporter(mw.col, file)
ti.initMapping()
ti.run()
tooltip('Successfully imported results.')
except:
showWarning('<b>Import error</b><br>Make sure that the notetype and deck exist and that you have enough fields.')