anki/qt/aqt/exporting.py

178 lines
6.5 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import os
import re
2019-12-20 10:19:03 +01:00
import time
2020-02-09 07:37:50 +01:00
from typing import List, Optional
2019-12-20 10:19:03 +01:00
import aqt
2020-01-15 04:49:26 +01:00
from anki import hooks
from anki.exporting import exporters
2019-12-20 10:19:03 +01:00
from anki.lang import _, ngettext
from aqt.qt import *
2019-12-23 01:34:10 +01:00
from aqt.utils import checkInvalidFilename, getSaveFile, showInfo, showWarning, tooltip
2019-12-20 10:19:03 +01:00
class ExportDialog(QDialog):
2020-02-09 07:37:50 +01:00
def __init__(self, mw, did: Optional[int] = None, cids: Optional[List[int]] = None):
QDialog.__init__(self, mw, Qt.Window)
self.mw = mw
self.col = mw.col
self.frm = aqt.forms.exporting.Ui_ExportDialog()
self.frm.setupUi(self)
self.exporter = None
2020-02-09 07:37:50 +01:00
self.cids = cids
2014-06-20 02:13:12 +02:00
self.setup(did)
self.exec_()
2020-02-09 07:37:50 +01:00
def setup(self, did: Optional[int]):
self.exporters = exporters()
# if a deck specified, start with .apkg type selected
idx = 0
2020-02-09 07:19:37 +01:00
if did or self.cids:
2019-12-23 01:34:10 +01:00
for c, (k, e) in enumerate(self.exporters):
if e.ext == ".apkg":
idx = c
break
self.frm.format.insertItems(0, [e[0] for e in self.exporters])
self.frm.format.setCurrentIndex(idx)
self.frm.format.activated.connect(self.exporterChanged)
self.exporterChanged(idx)
# deck list
if self.cids is None:
self.decks = [_("All Decks")] + sorted(self.col.decks.allNames())
else:
self.decks = [_("Browser Selection")]
self.frm.deck.addItems(self.decks)
# save button
b = QPushButton(_("Export..."))
self.frm.buttonBox.addButton(b, QDialogButtonBox.AcceptRole)
2014-06-20 02:13:12 +02:00
# set default option if accessed through deck button
if did:
2019-12-23 01:34:10 +01:00
name = self.mw.col.decks.get(did)["name"]
2014-06-20 02:13:12 +02:00
index = self.frm.deck.findText(name)
self.frm.deck.setCurrentIndex(index)
def exporterChanged(self, idx):
self.exporter = self.exporters[idx][1](self.col)
self.isApkg = self.exporter.ext == ".apkg"
self.isVerbatim = getattr(self.exporter, "verbatim", False)
self.isTextNote = hasattr(self.exporter, "includeTags")
self.frm.includeSched.setVisible(
2019-12-23 01:34:10 +01:00
getattr(self.exporter, "includeSched", None) is not None
)
self.frm.includeMedia.setVisible(
2019-12-23 01:34:10 +01:00
getattr(self.exporter, "includeMedia", None) is not None
)
self.frm.includeTags.setVisible(
2019-12-23 01:34:10 +01:00
getattr(self.exporter, "includeTags", None) is not None
)
2019-03-04 23:57:53 +01:00
html = getattr(self.exporter, "includeHTML", None)
if html is not None:
self.frm.includeHTML.setVisible(True)
self.frm.includeHTML.setChecked(html)
else:
self.frm.includeHTML.setVisible(False)
# show deck list?
self.frm.deck.setVisible(not self.isVerbatim)
def accept(self):
2019-12-23 01:34:10 +01:00
self.exporter.includeSched = self.frm.includeSched.isChecked()
self.exporter.includeMedia = self.frm.includeMedia.isChecked()
self.exporter.includeTags = self.frm.includeTags.isChecked()
self.exporter.includeHTML = self.frm.includeHTML.isChecked()
idx = self.frm.deck.currentIndex()
if self.cids is not None:
# Browser Selection
self.exporter.cids = self.cids
self.exporter.did = None
elif idx == 0:
# All decks
self.exporter.did = None
self.exporter.cids = None
else:
# Deck idx-1 in the list of decks
self.exporter.cids = None
name = self.decks[self.frm.deck.currentIndex()]
self.exporter.did = self.col.decks.id(name)
if self.isVerbatim:
2019-12-23 01:34:10 +01:00
name = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time()))
deck_name = _("collection") + name
else:
# Get deck name and remove invalid filename characters
deck_name = self.decks[self.frm.deck.currentIndex()]
2019-12-23 01:34:10 +01:00
deck_name = re.sub('[\\\\/?<>:*|"^]', "_", deck_name)
2019-12-23 01:34:10 +01:00
if (
not self.isVerbatim
and self.isApkg
and self.exporter.includeSched
and self.col.schedVer() == 2
):
showInfo(
"Please switch to the regular scheduler before exporting a single deck .apkg with scheduling."
)
return
2019-12-23 01:34:10 +01:00
filename = "{0}{1}".format(deck_name, self.exporter.ext)
while 1:
2019-12-23 01:34:10 +01:00
file = getSaveFile(
self,
_("Export"),
"export",
self.exporter.key,
self.exporter.ext,
fname=filename,
)
if not file:
return
if checkInvalidFilename(os.path.basename(file), dirsep=False):
continue
break
self.hide()
if file:
self.mw.progress.start(immediate=True)
try:
f = open(file, "wb")
f.close()
except (OSError, IOError) as e:
showWarning(_("Couldn't save file: %s") % str(e))
else:
os.unlink(file)
exportedMedia = lambda cnt: self.mw.progress.update(
2019-12-23 01:34:10 +01:00
label=ngettext(
"Exported %d media file", "Exported %d media files", cnt
)
% cnt
)
2020-01-15 07:53:24 +01:00
hooks.media_files_did_export.append(exportedMedia)
self.exporter.exportInto(file)
2020-01-15 07:53:24 +01:00
hooks.media_files_did_export.remove(exportedMedia)
period = 3000
if self.isVerbatim:
msg = _("Collection exported.")
else:
if self.isTextNote:
2019-12-23 01:34:10 +01:00
msg = (
ngettext(
"%d note exported.",
"%d notes exported.",
self.exporter.count,
)
% self.exporter.count
)
else:
2019-12-23 01:34:10 +01:00
msg = (
ngettext(
"%d card exported.",
"%d cards exported.",
self.exporter.count,
)
% self.exporter.count
)
tooltip(msg, period=period)
finally:
self.mw.progress.finish()
QDialog.accept(self)