export in a background thread
This commit is contained in:
parent
7986a79530
commit
53952ba131
@ -6,6 +6,7 @@ from __future__ import annotations
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from concurrent.futures import Future
|
||||
from typing import List, Optional
|
||||
|
||||
import aqt
|
||||
@ -131,7 +132,7 @@ class ExportDialog(QDialog):
|
||||
break
|
||||
self.hide()
|
||||
if file:
|
||||
self.mw.progress.start(immediate=True)
|
||||
# check we can write to file
|
||||
try:
|
||||
f = open(file, "wb")
|
||||
f.close()
|
||||
@ -139,39 +140,51 @@ class ExportDialog(QDialog):
|
||||
showWarning(_("Couldn't save file: %s") % str(e))
|
||||
else:
|
||||
os.unlink(file)
|
||||
exportedMedia = lambda cnt: self.mw.progress.update(
|
||||
label=ngettext(
|
||||
"Exported %d media file", "Exported %d media files", cnt
|
||||
|
||||
# progress handler
|
||||
def exported_media(cnt):
|
||||
self.mw.taskman.run_on_main(
|
||||
lambda: self.mw.progress.update(
|
||||
label=ngettext(
|
||||
"Exported %d media file", "Exported %d media files", cnt
|
||||
)
|
||||
% cnt
|
||||
)
|
||||
% cnt
|
||||
)
|
||||
hooks.media_files_did_export.append(exportedMedia)
|
||||
|
||||
def do_export():
|
||||
self.exporter.exportInto(file)
|
||||
hooks.media_files_did_export.remove(exportedMedia)
|
||||
period = 3000
|
||||
if self.isVerbatim:
|
||||
msg = _("Collection exported.")
|
||||
self.mw.reopen()
|
||||
else:
|
||||
if self.isTextNote:
|
||||
msg = (
|
||||
ngettext(
|
||||
"%d note exported.",
|
||||
"%d notes exported.",
|
||||
self.exporter.count,
|
||||
)
|
||||
% self.exporter.count
|
||||
)
|
||||
else:
|
||||
msg = (
|
||||
ngettext(
|
||||
"%d card exported.",
|
||||
"%d cards exported.",
|
||||
self.exporter.count,
|
||||
)
|
||||
% self.exporter.count
|
||||
)
|
||||
tooltip(msg, period=period)
|
||||
finally:
|
||||
|
||||
def on_done(future: Future):
|
||||
self.mw.progress.finish()
|
||||
QDialog.accept(self)
|
||||
hooks.media_files_did_export.remove(exported_media)
|
||||
# raises if exporter failed
|
||||
future.result()
|
||||
self.on_export_finished()
|
||||
|
||||
self.mw.progress.start(immediate=True)
|
||||
hooks.media_files_did_export.append(exported_media)
|
||||
|
||||
self.mw.taskman.run_in_background(do_export, on_done)
|
||||
|
||||
def on_export_finished(self):
|
||||
if self.isVerbatim:
|
||||
msg = _("Collection exported.")
|
||||
self.mw.reopen()
|
||||
else:
|
||||
if self.isTextNote:
|
||||
msg = (
|
||||
ngettext(
|
||||
"%d note exported.", "%d notes exported.", self.exporter.count,
|
||||
)
|
||||
% self.exporter.count
|
||||
)
|
||||
else:
|
||||
msg = (
|
||||
ngettext(
|
||||
"%d card exported.", "%d cards exported.", self.exporter.count,
|
||||
)
|
||||
% self.exporter.count
|
||||
)
|
||||
tooltip(msg, period=3000)
|
||||
QDialog.reject(self)
|
||||
|
Loading…
Reference in New Issue
Block a user