2013-10-20 03:57:42 +02:00
|
|
|
# coding=utf-8
|
2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2019-12-20 10:19:03 +01:00
|
|
|
import json
|
2013-10-20 03:57:42 +02:00
|
|
|
import os
|
|
|
|
import re
|
2019-12-20 10:19:03 +01:00
|
|
|
import shutil
|
2013-10-20 03:57:42 +02:00
|
|
|
import traceback
|
2017-09-30 11:29:21 +02:00
|
|
|
import unicodedata
|
2019-12-20 10:19:03 +01:00
|
|
|
import zipfile
|
2020-03-06 05:36:05 +01:00
|
|
|
from concurrent.futures import Future
|
2020-07-24 20:38:34 +02:00
|
|
|
from typing import Optional
|
2013-10-20 03:57:42 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import anki.importing as importing
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.deckchooser
|
2013-10-20 03:57:42 +02:00
|
|
|
import aqt.forms
|
|
|
|
import aqt.modelchooser
|
2020-01-15 04:49:26 +01:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
2020-02-27 11:32:57 +01:00
|
|
|
TR,
|
2019-12-23 01:34:10 +01:00
|
|
|
askUser,
|
|
|
|
getFile,
|
|
|
|
getOnlyText,
|
|
|
|
openHelp,
|
|
|
|
showInfo,
|
|
|
|
showText,
|
|
|
|
showWarning,
|
|
|
|
tooltip,
|
2020-02-27 11:32:57 +01:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2013-10-20 03:57:42 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class ChangeMap(QDialog):
|
2019-12-16 11:27:58 +01:00
|
|
|
def __init__(self, mw: AnkiQt, model, current):
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.model = model
|
|
|
|
self.frm = aqt.forms.changemap.Ui_ChangeMap()
|
|
|
|
self.frm.setupUi(self)
|
|
|
|
n = 0
|
|
|
|
setCurrent = False
|
2019-12-23 01:34:10 +01:00
|
|
|
for field in self.model["flds"]:
|
2020-11-17 12:47:47 +01:00
|
|
|
item = QListWidgetItem(tr(TR.IMPORTING_MAP_TO, val=field["name"]))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.frm.fields.addItem(item)
|
2019-12-23 01:34:10 +01:00
|
|
|
if current == field["name"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
setCurrent = True
|
|
|
|
self.frm.fields.setCurrentRow(n)
|
|
|
|
n += 1
|
2020-11-17 08:42:43 +01:00
|
|
|
self.frm.fields.addItem(QListWidgetItem(tr(TR.IMPORTING_MAP_TO_TAGS)))
|
|
|
|
self.frm.fields.addItem(QListWidgetItem(tr(TR.IMPORTING_IGNORE_FIELD)))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not setCurrent:
|
|
|
|
if current == "_tags":
|
|
|
|
self.frm.fields.setCurrentRow(n)
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.frm.fields.setCurrentRow(n + 1)
|
2020-07-24 20:38:34 +02:00
|
|
|
self.field: Optional[str] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def getField(self):
|
|
|
|
self.exec_()
|
|
|
|
return self.field
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
row = self.frm.fields.currentRow()
|
2019-12-23 01:34:10 +01:00
|
|
|
if row < len(self.model["flds"]):
|
|
|
|
self.field = self.model["flds"][row]["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
elif row == self.frm.fields.count() - 2:
|
|
|
|
self.field = "_tags"
|
|
|
|
else:
|
|
|
|
self.field = None
|
|
|
|
QDialog.accept(self)
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
self.accept()
|
|
|
|
|
|
|
|
|
2020-03-06 05:36:05 +01:00
|
|
|
# called by importFile() when importing a mappable file like .csv
|
2019-12-23 01:34:10 +01:00
|
|
|
class ImportDialog(QDialog):
|
2020-01-15 22:41:23 +01:00
|
|
|
def __init__(self, mw: AnkiQt, importer) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.importer = importer
|
|
|
|
self.frm = aqt.forms.importing.Ui_ImportDialog()
|
|
|
|
self.frm.setupUi(self)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(
|
|
|
|
self.frm.buttonBox.button(QDialogButtonBox.Help).clicked, self.helpRequested
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupMappingFrame()
|
|
|
|
self.setupOptions()
|
|
|
|
self.modelChanged()
|
2013-04-15 06:46:07 +02:00
|
|
|
self.frm.autoDetect.setVisible(self.importer.needDelimiter)
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.current_note_type_did_change.append(self.modelChanged)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.frm.autoDetect.clicked, self.onDelimiter)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.updateDelimiterButtonText()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.frm.allowHTML.setChecked(self.mw.pm.profile.get("allowHTML", True))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.frm.importMode.currentIndexChanged, self.importModeChanged)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.frm.importMode.setCurrentIndex(self.mw.pm.profile.get("importMode", 1))
|
2020-01-15 09:33:32 +01:00
|
|
|
self.frm.tagModified.setText(self.mw.pm.profile.get("tagModified", ""))
|
|
|
|
self.frm.tagModified.setCol(self.mw.col)
|
2014-02-22 11:30:32 +01:00
|
|
|
# import button
|
2020-11-17 08:42:43 +01:00
|
|
|
b = QPushButton(tr(TR.ACTIONS_IMPORT))
|
2014-02-22 11:30:32 +01:00
|
|
|
self.frm.buttonBox.addButton(b, QDialogButtonBox.AcceptRole)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
def setupOptions(self):
|
|
|
|
self.model = self.mw.col.models.current()
|
|
|
|
self.modelChooser = aqt.modelchooser.ModelChooser(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw, self.frm.modelArea, label=False
|
|
|
|
)
|
|
|
|
self.deck = aqt.deckchooser.DeckChooser(self.mw, self.frm.deckArea, label=False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 04:49:26 +01:00
|
|
|
def modelChanged(self, unused=None):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.importer.model = self.mw.col.models.current()
|
|
|
|
self.importer.initMapping()
|
|
|
|
self.showMapping()
|
|
|
|
|
|
|
|
def onDelimiter(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
str = (
|
|
|
|
getOnlyText(
|
2020-11-18 02:32:22 +01:00
|
|
|
tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE),
|
2019-12-23 01:34:10 +01:00
|
|
|
self,
|
|
|
|
help="importing",
|
|
|
|
)
|
|
|
|
or "\t"
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
str = str.replace("\\t", "\t")
|
2017-02-13 13:12:19 +01:00
|
|
|
if len(str) > 1:
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
2020-11-18 02:32:22 +01:00
|
|
|
tr(TR.IMPORTING_MULTICHARACTER_SEPARATORS_ARE_NOT_SUPPORTED_PLEASE)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2017-02-13 13:12:19 +01:00
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
self.hideMapping()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def updateDelim():
|
|
|
|
self.importer.delimiter = str
|
|
|
|
self.importer.updateDelimiter()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.showMapping(hook=updateDelim)
|
|
|
|
self.updateDelimiterButtonText()
|
|
|
|
|
|
|
|
def updateDelimiterButtonText(self):
|
|
|
|
if not self.importer.needDelimiter:
|
|
|
|
return
|
|
|
|
if self.importer.delimiter:
|
|
|
|
d = self.importer.delimiter
|
|
|
|
else:
|
|
|
|
d = self.importer.dialect.delimiter
|
|
|
|
if d == "\t":
|
2020-11-17 08:42:43 +01:00
|
|
|
d = tr(TR.IMPORTING_TAB)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif d == ",":
|
2020-11-17 08:42:43 +01:00
|
|
|
d = tr(TR.IMPORTING_COMMA)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif d == " ":
|
2020-11-17 08:42:43 +01:00
|
|
|
d = tr(TR.STUDYING_SPACE)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif d == ";":
|
2020-11-17 08:42:43 +01:00
|
|
|
d = tr(TR.IMPORTING_SEMICOLON)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif d == ":":
|
2020-11-17 08:42:43 +01:00
|
|
|
d = tr(TR.IMPORTING_COLON)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2016-05-12 06:45:35 +02:00
|
|
|
d = repr(d)
|
2020-11-17 12:47:47 +01:00
|
|
|
txt = tr(TR.IMPORTING_FIELDS_SEPARATED_BY, val=d)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.frm.autoDetect.setText(txt)
|
2017-02-13 13:12:19 +01:00
|
|
|
|
2014-02-22 11:30:32 +01:00
|
|
|
def accept(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.importer.mapping = self.mapping
|
|
|
|
if not self.importer.mappingOk():
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.IMPORTING_THE_FIRST_FIELD_OF_THE_NOTE))
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
self.importer.importMode = self.frm.importMode.currentIndex()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw.pm.profile["importMode"] = self.importer.importMode
|
2012-12-21 08:51:59 +01:00
|
|
|
self.importer.allowHTML = self.frm.allowHTML.isChecked()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw.pm.profile["allowHTML"] = self.importer.allowHTML
|
2020-01-15 09:33:32 +01:00
|
|
|
self.importer.tagModified = self.frm.tagModified.text()
|
2020-01-02 15:01:44 +01:00
|
|
|
self.mw.pm.profile["tagModified"] = self.importer.tagModified
|
2012-12-21 08:51:59 +01:00
|
|
|
did = self.deck.selectedId()
|
2020-10-15 14:14:55 +02:00
|
|
|
self.importer.model["did"] = did
|
|
|
|
self.mw.col.models.save(self.importer.model, updateReqs=False)
|
2013-09-03 20:13:02 +02:00
|
|
|
self.mw.col.decks.select(did)
|
2020-05-31 03:24:33 +02:00
|
|
|
self.mw.progress.start()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.ACTIONS_IMPORT))
|
2020-03-06 05:36:05 +01:00
|
|
|
|
|
|
|
def on_done(future: Future):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.progress.finish()
|
2020-03-06 05:36:05 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
future.result()
|
|
|
|
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
|
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
txt = tr(TR.IMPORTING_IMPORTING_COMPLETE) + "\n"
|
2020-03-06 05:36:05 +01:00
|
|
|
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)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupMappingFrame(self):
|
|
|
|
# qt seems to have a bug with adding/removing from a grid, so we add
|
|
|
|
# to a separate object and add/remove that instead
|
|
|
|
self.frame = QFrame(self.frm.mappingArea)
|
|
|
|
self.frm.mappingArea.setWidget(self.frame)
|
|
|
|
self.mapbox = QVBoxLayout(self.frame)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mapbox.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mapwidget = None
|
|
|
|
|
|
|
|
def hideMapping(self):
|
|
|
|
self.frm.mappingGroup.hide()
|
|
|
|
|
|
|
|
def showMapping(self, keepMapping=False, hook=None):
|
|
|
|
if hook:
|
|
|
|
hook()
|
|
|
|
if not keepMapping:
|
|
|
|
self.mapping = self.importer.mapping
|
|
|
|
self.frm.mappingGroup.show()
|
|
|
|
assert self.importer.fields()
|
|
|
|
# set up the mapping grid
|
|
|
|
if self.mapwidget:
|
|
|
|
self.mapbox.removeWidget(self.mapwidget)
|
|
|
|
self.mapwidget.deleteLater()
|
|
|
|
self.mapwidget = QWidget()
|
|
|
|
self.mapbox.addWidget(self.mapwidget)
|
|
|
|
self.grid = QGridLayout(self.mapwidget)
|
|
|
|
self.mapwidget.setLayout(self.grid)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.grid.setContentsMargins(3, 3, 3, 3)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.grid.setSpacing(6)
|
|
|
|
fields = self.importer.fields()
|
|
|
|
for num in range(len(self.mapping)):
|
2020-11-17 12:47:47 +01:00
|
|
|
text = tr(TR.IMPORTING_FIELD_OF_FILE_IS, val=num + 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.grid.addWidget(QLabel(text), num, 0)
|
|
|
|
if self.mapping[num] == "_tags":
|
2020-11-17 08:42:43 +01:00
|
|
|
text = tr(TR.IMPORTING_MAPPED_TO_TAGS)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif self.mapping[num]:
|
2020-11-17 12:47:47 +01:00
|
|
|
text = tr(TR.IMPORTING_MAPPED_TO, val=self.mapping[num])
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
text = tr(TR.IMPORTING_IGNORED)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.grid.addWidget(QLabel(text), num, 1)
|
2020-11-17 08:42:43 +01:00
|
|
|
button = QPushButton(tr(TR.IMPORTING_CHANGE))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.grid.addWidget(button, num, 2)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(button.clicked, lambda _, s=self, n=num: s.changeMappingNum(n))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def changeMappingNum(self, n):
|
|
|
|
f = ChangeMap(self.mw, self.importer.model, self.mapping[n]).getField()
|
|
|
|
try:
|
|
|
|
# make sure we don't have it twice
|
|
|
|
index = self.mapping.index(f)
|
|
|
|
self.mapping[index] = None
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
self.mapping[n] = f
|
|
|
|
if getattr(self.importer, "delimiter", False):
|
|
|
|
self.savedDelimiter = self.importer.delimiter
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def updateDelim():
|
|
|
|
self.importer.delimiter = self.savedDelimiter
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.showMapping(hook=updateDelim, keepMapping=True)
|
|
|
|
else:
|
|
|
|
self.showMapping(keepMapping=True)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChooser.cleanup()
|
2018-03-01 05:20:30 +01:00
|
|
|
self.deck.cleanup()
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.current_note_type_did_change.remove(self.modelChanged)
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
def helpRequested(self):
|
2014-02-22 11:30:32 +01:00
|
|
|
openHelp("importing")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-03 07:32:44 +01:00
|
|
|
def importModeChanged(self, newImportMode):
|
|
|
|
if newImportMode == 0:
|
2020-01-15 09:33:32 +01:00
|
|
|
self.frm.tagModified.setEnabled(True)
|
2020-01-03 07:32:44 +01:00
|
|
|
else:
|
2020-01-15 09:33:32 +01:00
|
|
|
self.frm.tagModified.setEnabled(False)
|
2020-01-03 07:32:44 +01:00
|
|
|
|
2013-05-03 14:29:25 +02:00
|
|
|
|
|
|
|
def showUnicodeWarning():
|
|
|
|
"""Shorthand to show a standard warning."""
|
2020-11-18 02:32:22 +01:00
|
|
|
showWarning(tr(TR.IMPORTING_SELECTED_FILE_WAS_NOT_IN_UTF8))
|
2013-05-03 14:29:25 +02:00
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onImport(mw):
|
|
|
|
filt = ";;".join([x[0] for x in importing.Importers])
|
2020-11-17 08:42:43 +01:00
|
|
|
file = getFile(mw, tr(TR.ACTIONS_IMPORT), None, key="import", filter=filt)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not file:
|
|
|
|
return
|
2016-05-12 06:45:35 +02:00
|
|
|
file = str(file)
|
2018-09-27 03:35:21 +02:00
|
|
|
|
|
|
|
head, ext = os.path.splitext(file)
|
|
|
|
ext = ext.lower()
|
|
|
|
if ext == ".anki":
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.IMPORTING_ANKI_FILES_ARE_FROM_A_VERY))
|
2018-09-27 03:35:21 +02:00
|
|
|
return
|
|
|
|
elif ext == ".anki2":
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.IMPORTING_ANKI2_FILES_ARE_NOT_DIRECTLY_IMPORTABLE))
|
2018-09-27 03:35:21 +02:00
|
|
|
return
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
importFile(mw, file)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def importFile(mw, file):
|
2019-03-04 02:22:40 +01:00
|
|
|
importerClass = None
|
2012-12-21 08:51:59 +01:00
|
|
|
done = False
|
|
|
|
for i in importing.Importers:
|
|
|
|
if done:
|
|
|
|
break
|
2019-03-04 08:03:43 +01:00
|
|
|
for mext in re.findall(r"[( ]?\*\.(.+?)[) ]", i[0]):
|
2013-02-24 02:09:03 +01:00
|
|
|
if file.endswith("." + mext):
|
2019-03-04 02:22:40 +01:00
|
|
|
importerClass = i[1]
|
2012-12-21 08:51:59 +01:00
|
|
|
done = True
|
|
|
|
break
|
2019-03-04 02:22:40 +01:00
|
|
|
if not importerClass:
|
2012-12-21 08:51:59 +01:00
|
|
|
# if no matches, assume TSV
|
2019-03-04 02:22:40 +01:00
|
|
|
importerClass = importing.Importers[0][1]
|
|
|
|
importer = importerClass(mw.col, file)
|
2012-12-21 08:51:59 +01:00
|
|
|
# need to show import dialog?
|
|
|
|
if importer.needMapper:
|
|
|
|
# make sure we can load the file first
|
|
|
|
mw.progress.start(immediate=True)
|
|
|
|
try:
|
|
|
|
importer.open()
|
2020-05-29 00:40:03 +02:00
|
|
|
mw.progress.finish()
|
2020-05-12 16:15:01 +02:00
|
|
|
diag = ImportDialog(mw, importer)
|
2012-12-21 08:51:59 +01:00
|
|
|
except UnicodeDecodeError:
|
2020-03-14 09:42:03 +01:00
|
|
|
mw.progress.finish()
|
2013-05-03 14:29:25 +02:00
|
|
|
showUnicodeWarning()
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2016-05-12 06:45:35 +02:00
|
|
|
except Exception as e:
|
2020-03-14 09:42:03 +01:00
|
|
|
mw.progress.finish()
|
2013-10-20 03:57:42 +02:00
|
|
|
msg = repr(str(e))
|
2014-06-17 15:31:38 +02:00
|
|
|
if msg == "'unknownFormat'":
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.IMPORTING_UNKNOWN_FILE_FORMAT))
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-02-27 11:32:57 +01:00
|
|
|
msg = tr(TR.IMPORTING_FAILED_DEBUG_INFO) + "\n"
|
2016-05-12 06:45:35 +02:00
|
|
|
msg += str(traceback.format_exc())
|
2012-12-21 08:51:59 +01:00
|
|
|
showText(msg)
|
|
|
|
return
|
|
|
|
finally:
|
2020-05-12 16:15:01 +02:00
|
|
|
importer.close()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2013-09-11 08:56:59 +02:00
|
|
|
# if it's an apkg/zip, first test it's a valid file
|
2012-12-21 08:51:59 +01:00
|
|
|
if importer.__class__.__name__ == "AnkiPackageImporter":
|
2013-09-11 08:56:59 +02:00
|
|
|
try:
|
2014-09-27 17:19:43 +02:00
|
|
|
z = zipfile.ZipFile(importer.file)
|
2013-09-11 08:56:59 +02:00
|
|
|
z.getinfo("collection.anki2")
|
|
|
|
except:
|
2014-06-28 22:55:01 +02:00
|
|
|
showWarning(invalidZipMsg())
|
2013-09-11 08:56:59 +02:00
|
|
|
return
|
2020-03-06 05:36:05 +01:00
|
|
|
# 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()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not setupApkgImport(mw, importer):
|
|
|
|
return
|
2020-03-06 05:36:05 +01:00
|
|
|
|
|
|
|
# importing non-colpkg files
|
2012-12-21 08:51:59 +01:00
|
|
|
mw.progress.start(immediate=True)
|
2020-03-06 05:38:35 +01:00
|
|
|
|
|
|
|
def on_done(future: Future):
|
|
|
|
mw.progress.finish()
|
2018-12-01 04:37:26 +01:00
|
|
|
try:
|
2020-03-06 05:38:35 +01:00
|
|
|
future.result()
|
|
|
|
except zipfile.BadZipfile:
|
|
|
|
showWarning(invalidZipMsg())
|
|
|
|
except Exception as e:
|
|
|
|
err = repr(str(e))
|
|
|
|
if "invalidFile" in err:
|
2020-11-18 02:32:22 +01:00
|
|
|
msg = tr(TR.IMPORTING_INVALID_FILE_PLEASE_RESTORE_FROM_BACKUP)
|
2020-03-06 05:38:35 +01:00
|
|
|
showWarning(msg)
|
|
|
|
elif "invalidTempFolder" in err:
|
|
|
|
showWarning(mw.errorHandler.tempFolderMsg())
|
|
|
|
elif "readonly" in err:
|
2020-11-18 02:32:22 +01:00
|
|
|
showWarning(tr(TR.IMPORTING_UNABLE_TO_IMPORT_FROM_A_READONLY))
|
2020-03-06 05:38:35 +01:00
|
|
|
else:
|
|
|
|
msg = tr(TR.IMPORTING_FAILED_DEBUG_INFO) + "\n"
|
|
|
|
msg += str(traceback.format_exc())
|
|
|
|
showText(msg)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-03-06 05:38:35 +01:00
|
|
|
log = "\n".join(importer.log)
|
|
|
|
if "\n" not in log:
|
|
|
|
tooltip(log)
|
|
|
|
else:
|
|
|
|
showText(log)
|
|
|
|
|
|
|
|
mw.reset()
|
|
|
|
|
|
|
|
mw.taskman.run_in_background(importer.run, on_done)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2014-06-28 22:55:01 +02:00
|
|
|
def invalidZipMsg():
|
2020-11-18 02:32:22 +01:00
|
|
|
return tr(TR.IMPORTING_THIS_FILE_DOES_NOT_APPEAR_TO)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2014-06-28 22:55:01 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def setupApkgImport(mw, importer):
|
|
|
|
base = os.path.basename(importer.file).lower()
|
2019-12-23 01:34:10 +01:00
|
|
|
full = (
|
|
|
|
(base == "collection.apkg")
|
|
|
|
or re.match("backup-.*\\.apkg", base)
|
|
|
|
or base.endswith(".colpkg")
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not full:
|
|
|
|
# adding
|
|
|
|
return True
|
2019-12-23 01:34:10 +01:00
|
|
|
if not mw.restoringBackup and not askUser(
|
2020-11-18 02:32:22 +01:00
|
|
|
tr(TR.IMPORTING_THIS_WILL_DELETE_YOUR_EXISTING_COLLECTION),
|
2019-12-23 01:34:10 +01:00
|
|
|
msgfunc=QMessageBox.warning,
|
|
|
|
defaultno=True,
|
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
return False
|
2019-04-29 08:59:29 +02:00
|
|
|
|
|
|
|
replaceWithApkg(mw, importer.file, mw.restoringBackup)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-22 00:34:11 +01:00
|
|
|
def replaceWithApkg(mw, file, backup):
|
2017-08-16 11:45:39 +02:00
|
|
|
mw.unloadCollection(lambda: _replaceWithApkg(mw, file, backup))
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-03-06 05:55:15 +01:00
|
|
|
def _replaceWithApkg(mw, filename, backup):
|
2017-08-16 11:45:39 +02:00
|
|
|
mw.progress.start(immediate=True)
|
2018-01-29 05:12:04 +01:00
|
|
|
|
2020-03-06 05:55:15 +01:00
|
|
|
def do_import():
|
|
|
|
z = zipfile.ZipFile(filename)
|
2018-01-29 05:12:04 +01:00
|
|
|
|
2020-03-06 05:55:15 +01:00
|
|
|
# v2 scheduler?
|
|
|
|
colname = "collection.anki21"
|
|
|
|
try:
|
|
|
|
z.getinfo(colname)
|
|
|
|
except KeyError:
|
|
|
|
colname = "collection.anki2"
|
2018-01-29 05:12:04 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
with z.open(colname) as source, open(mw.pm.collectionPath(), "wb") as target:
|
2020-07-24 20:38:34 +02:00
|
|
|
# ignore appears related to https://github.com/python/typeshed/issues/4349
|
|
|
|
# see if can turn off once issue fix is merged in
|
2020-10-10 11:02:59 +02:00
|
|
|
shutil.copyfileobj(source, target)
|
2020-03-06 05:55:15 +01:00
|
|
|
|
|
|
|
d = os.path.join(mw.pm.profileFolder(), "collection.media")
|
|
|
|
for n, (cStr, file) in enumerate(
|
|
|
|
json.loads(z.read("media").decode("utf8")).items()
|
|
|
|
):
|
|
|
|
mw.taskman.run_on_main(
|
|
|
|
lambda n=n: mw.progress.update(
|
2020-11-18 01:52:13 +01:00
|
|
|
tr(TR.IMPORTING_PROCESSED_MEDIA_FILE, count=n)
|
2020-03-06 05:55:15 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
size = z.getinfo(cStr).file_size
|
|
|
|
dest = os.path.join(d, unicodedata.normalize("NFC", file))
|
|
|
|
# if we have a matching file size
|
|
|
|
if os.path.exists(dest) and size == os.stat(dest).st_size:
|
|
|
|
continue
|
|
|
|
data = z.read(cStr)
|
2020-05-10 02:58:42 +02:00
|
|
|
with open(dest, "wb") as file:
|
|
|
|
file.write(data)
|
2020-03-06 05:55:15 +01:00
|
|
|
|
|
|
|
z.close()
|
|
|
|
|
|
|
|
def on_done(future: Future):
|
2017-08-16 11:45:39 +02:00
|
|
|
mw.progress.finish()
|
2020-03-06 05:55:15 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
future.result()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.IMPORTING_THE_PROVIDED_FILE_IS_NOT_A))
|
2020-03-06 05:55:15 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if not mw.loadCollection():
|
|
|
|
return
|
|
|
|
if backup:
|
|
|
|
mw.col.modSchema(check=False)
|
|
|
|
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.IMPORTING_IMPORTING_COMPLETE))
|
2020-03-06 05:55:15 +01:00
|
|
|
|
|
|
|
mw.taskman.run_in_background(do_import, on_done)
|