-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
55 lines (40 loc) · 1.65 KB
/
__init__.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import subprocess
import sys
import traceback
from aqt import mw
from aqt.utils import showInfo
from aqt.qt import *
from anki.importing.apkg import AnkiPackageImporter
DECK_DIR = os.path.expanduser(
mw.addonManager.getConfig(__name__).get(
"Markdown Deck Library Path", "~/Flashcard Decks"))
ANKDOWN = os.path.expanduser(
mw.addonManager.getConfig(__name__).get(
"Ankdown Location", "~/.local/bin/ankdown"))
CSS = os.path.expanduser(
mw.addonManager.getConfig(__name__).get("CSS", "None"))
def importDecks():
if not os.path.isdir(DECK_DIR):
raise(ValueError("{} is not a directory".format(DECK_DIR)))
os.chdir(os.path.join(os.path.split(__file__)[0], "user_files"))
# NOTE this loop is because ankdown can't easily create multiple decks
# - genanki ends up lumping them all together. Maybe one day when that
# bug is fixed, we can call ankdown once.
for (d, subds, fns) in os.walk(DECK_DIR):
if not any([fn.endswith(".md") for fn in fns]):
continue
package_name = os.path.basename(d) + ".apkg"
subprocess_cmd = [ANKDOWN, "-r", d, "-p", package_name]
if CSS is not "None":
subprocess_cmd.extend(["-css", CSS])
subprocess.run(subprocess_cmd)
AnkiPackageImporter(mw.col, package_name).run()
def wrapImportDecks():
try:
importDecks()
except:
showInfo("Failed to import; received error: {}".format(traceback.format_exc()))
action = QAction("Reload Markdown Decks", mw, shortcut=QKeySequence(QKeySequence.Refresh))
action.triggered.connect(wrapImportDecks)
mw.form.menuTools.addAction(action)