diff --git a/pylib/anki/exporting.py b/pylib/anki/exporting.py index 5c3e21aea..1d2fd4a13 100644 --- a/pylib/anki/exporting.py +++ b/pylib/anki/exporting.py @@ -8,7 +8,7 @@ import shutil import unicodedata import zipfile from io import BufferedWriter -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, Union from zipfile import ZipFile from anki import hooks @@ -20,7 +20,6 @@ from anki.utils import ids2str, namedtmp, splitFields, stripHTML class Exporter: includeHTML: Union[bool, None] = None ext: Optional[str] = None - key: Union[str, Callable, None] = None includeTags: Optional[bool] = None includeSched: Optional[bool] = None includeMedia: Optional[bool] = None @@ -35,6 +34,10 @@ class Exporter: self.did = did self.cids = cids + @staticmethod + def key(col: Collection) -> str: + return "" + def doExport(self, path) -> None: raise Exception("not implemented") @@ -97,7 +100,10 @@ class TextCardExporter(Exporter): def __init__(self, col) -> None: Exporter.__init__(self, col) - self.key = col.tr(TR.EXPORTING_CARDS_IN_PLAIN_TEXT) + + @staticmethod + def key(col: Collection) -> str: + return col.tr(TR.EXPORTING_CARDS_IN_PLAIN_TEXT) def doExport(self, file) -> None: ids = sorted(self.cardIds()) @@ -129,7 +135,10 @@ class TextNoteExporter(Exporter): def __init__(self, col: Collection) -> None: Exporter.__init__(self, col) self.includeID = False - self.key = col.tr(TR.EXPORTING_NOTES_IN_PLAIN_TEXT) + + @staticmethod + def key(col: Collection) -> str: + return col.tr(TR.EXPORTING_NOTES_IN_PLAIN_TEXT) def doExport(self, file: BufferedWriter) -> None: cardIds = self.cardIds() @@ -170,7 +179,10 @@ class AnkiExporter(Exporter): def __init__(self, col: Collection) -> None: Exporter.__init__(self, col) - self.key = col.tr(TR.EXPORTING_ANKI_20_DECK) + + @staticmethod + def key(col: Collection) -> str: + return col.tr(TR.EXPORTING_ANKI_20_DECK) def deckIds(self) -> List[int]: if self.cids: @@ -317,7 +329,10 @@ class AnkiPackageExporter(AnkiExporter): def __init__(self, col: Collection) -> None: AnkiExporter.__init__(self, col) - self.key = col.tr(TR.EXPORTING_ANKI_DECK_PACKAGE) + + @staticmethod + def key(col: Collection) -> str: + return col.tr(TR.EXPORTING_ANKI_DECK_PACKAGE) def exportInto(self, path: str) -> None: # open a zip file @@ -401,7 +416,10 @@ class AnkiCollectionPackageExporter(AnkiPackageExporter): def __init__(self, col): AnkiPackageExporter.__init__(self, col) - self.key = col.tr(TR.EXPORTING_ANKI_COLLECTION_PACKAGE) + + @staticmethod + def key(col: Collection) -> str: + return col.tr(TR.EXPORTING_ANKI_COLLECTION_PACKAGE) def doExport(self, z, path): "Export collection. Caller must re-open afterwards." @@ -425,10 +443,10 @@ class AnkiCollectionPackageExporter(AnkiPackageExporter): ########################################################################## -def exporters() -> List[Tuple[str, Any]]: +def exporters(col: Collection) -> List[Tuple[str, Any]]: def id(obj): if callable(obj.key): - key_str = obj.key(obj) + key_str = obj.key(col) else: key_str = obj.key return ("%s (*%s)" % (key_str, obj.ext), obj) diff --git a/pylib/tests/test_exporting.py b/pylib/tests/test_exporting.py index 291030ac6..b112c8e70 100644 --- a/pylib/tests/test_exporting.py +++ b/pylib/tests/test_exporting.py @@ -161,4 +161,4 @@ def test_export_textnote(): def test_exporters(): - assert "*.apkg" in str(exporters()) + assert "*.apkg" in str(exporters(getEmptyCol())) diff --git a/qt/aqt/exporting.py b/qt/aqt/exporting.py index 2d9b5c909..d9cdf683d 100644 --- a/qt/aqt/exporting.py +++ b/qt/aqt/exporting.py @@ -34,7 +34,7 @@ class ExportDialog(QDialog): self.exec_() def setup(self, did: Optional[int]): - self.exporters = exporters() + self.exporters = exporters(self.col) # if a deck specified, start with .apkg type selected idx = 0 if did or self.cids: @@ -114,7 +114,7 @@ class ExportDialog(QDialog): filename = "{0}{1}".format(deck_name, self.exporter.ext) if callable(self.exporter.key): - key_str = self.exporter.key() + key_str = self.exporter.key(self.col) else: key_str = self.exporter.key while 1: