mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 00:12:25 -04:00
import mapped files like csv in a background thread
This commit is contained in:
parent
53952ba131
commit
231fa30a86
1 changed files with 35 additions and 24 deletions
|
@ -9,6 +9,7 @@ import shutil
|
||||||
import traceback
|
import traceback
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import zipfile
|
import zipfile
|
||||||
|
from concurrent.futures import Future
|
||||||
|
|
||||||
import anki.importing as importing
|
import anki.importing as importing
|
||||||
import aqt.deckchooser
|
import aqt.deckchooser
|
||||||
|
@ -74,6 +75,7 @@ class ChangeMap(QDialog):
|
||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
|
|
||||||
|
# called by importFile() when importing a mappable file like .csv
|
||||||
class ImportDialog(QDialog):
|
class ImportDialog(QDialog):
|
||||||
def __init__(self, mw: AnkiQt, importer) -> None:
|
def __init__(self, mw: AnkiQt, importer) -> None:
|
||||||
QDialog.__init__(self, mw, Qt.Window)
|
QDialog.__init__(self, mw, Qt.Window)
|
||||||
|
@ -192,30 +194,35 @@ you can enter it here. Use \\t to represent tab."""
|
||||||
self.mw.col.decks.select(did)
|
self.mw.col.decks.select(did)
|
||||||
self.mw.progress.start(immediate=True)
|
self.mw.progress.start(immediate=True)
|
||||||
self.mw.checkpoint(_("Import"))
|
self.mw.checkpoint(_("Import"))
|
||||||
try:
|
|
||||||
self.importer.run()
|
def on_done(future: Future):
|
||||||
except UnicodeDecodeError:
|
|
||||||
showUnicodeWarning()
|
|
||||||
return
|
|
||||||
except Exception as e:
|
|
||||||
msg = tr(TR.IMPORTING_FAILED_DEBUG_INFO) + "\n"
|
|
||||||
err = repr(str(e))
|
|
||||||
if "1-character string" in err:
|
|
||||||
msg += err
|
|
||||||
elif "invalidTempFolder" in err:
|
|
||||||
msg += self.mw.errorHandler.tempFolderMsg()
|
|
||||||
else:
|
|
||||||
msg += traceback.format_exc()
|
|
||||||
showText(msg)
|
|
||||||
return
|
|
||||||
finally:
|
|
||||||
self.mw.progress.finish()
|
self.mw.progress.finish()
|
||||||
txt = _("Importing complete.") + "\n"
|
|
||||||
if self.importer.log:
|
try:
|
||||||
txt += "\n".join(self.importer.log)
|
future.result()
|
||||||
self.close()
|
except UnicodeDecodeError:
|
||||||
showText(txt)
|
showUnicodeWarning()
|
||||||
self.mw.reset()
|
return
|
||||||
|
except Exception as e:
|
||||||
|
msg = tr(TR.IMPORTING_FAILED_DEBUG_INFO) + "\n"
|
||||||
|
err = repr(str(e))
|
||||||
|
if "1-character string" in err:
|
||||||
|
msg += err
|
||||||
|
elif "invalidTempFolder" in err:
|
||||||
|
msg += self.mw.errorHandler.tempFolderMsg()
|
||||||
|
else:
|
||||||
|
msg += traceback.format_exc()
|
||||||
|
showText(msg)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
txt = _("Importing complete.") + "\n"
|
||||||
|
if self.importer.log:
|
||||||
|
txt += "\n".join(self.importer.log)
|
||||||
|
self.close()
|
||||||
|
showText(txt)
|
||||||
|
self.mw.reset()
|
||||||
|
|
||||||
|
self.mw.taskman.run_in_background(self.importer.run, on_done)
|
||||||
|
|
||||||
def setupMappingFrame(self):
|
def setupMappingFrame(self):
|
||||||
# qt seems to have a bug with adding/removing from a grid, so we add
|
# qt seems to have a bug with adding/removing from a grid, so we add
|
||||||
|
@ -380,9 +387,13 @@ def importFile(mw, file):
|
||||||
except:
|
except:
|
||||||
showWarning(invalidZipMsg())
|
showWarning(invalidZipMsg())
|
||||||
return
|
return
|
||||||
# we need to ask whether to import/replace
|
# we need to ask whether to import/replace; if it's
|
||||||
|
# a colpkg file then the rest of the import process
|
||||||
|
# will happen in setupApkgImport()
|
||||||
if not setupApkgImport(mw, importer):
|
if not setupApkgImport(mw, importer):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# importing non-colpkg files
|
||||||
mw.progress.start(immediate=True)
|
mw.progress.start(immediate=True)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue