When possible, install add-on before add-ons are loaded

Removes the need to restart Anki if it is not running when
user launches .ankiaddon file
This commit is contained in:
Glutanimate 2020-01-04 04:34:16 +01:00
parent d9e56e22f9
commit 6a7f11b172
2 changed files with 19 additions and 14 deletions

View file

@ -859,18 +859,19 @@ class ConfigEditor(QDialog):
def installAddonPackages(
addonsManager: AddonManager,
paths: List[str],
parent: QWidget = None,
external: bool = False,
parent: Optional[QWidget] = None,
warn: bool = False,
strictly_modal: bool = False
) -> bool:
if external:
if warn:
names = ",<br>".join(f"<b>{os.path.basename(p)}</b>" for p in paths)
q = _(
"<b>Important</b>: As add-ons are programs downloaded from the internet, "
"they are potentially malicious."
"<b>You should only install add-ons you trust.</b><br><br>"
"Are you sure you want to proceed with the installation of the "
"following add-on(s)?<br><br>%(names)s"
"following Anki add-on(s)?<br><br>%(names)s"
) % dict(names=names)
if (
not showInfo(
@ -888,11 +889,7 @@ def installAddonPackages(
if log:
log_html = "<br>".join(log)
if external:
log_html += "<br><br>" + _(
"<b>Please restart Anki to complete the installation.</b>"
)
if len(log) == 1:
if len(log) == 1 and not strictly_modal:
tooltip(log_html, parent=parent)
else:
showInfo(

View file

@ -72,7 +72,7 @@ class AnkiQt(QMainWindow):
self.safeMode = self.app.queryKeyboardModifiers() & Qt.ShiftModifier
try:
self.setupUI()
self.setupAddons()
self.setupAddons(args)
except:
showInfo(_("Error during startup:\n%s") % traceback.format_exc())
sys.exit(1)
@ -85,7 +85,7 @@ class AnkiQt(QMainWindow):
)
)
# were we given a file to import?
if args and args[0]:
if args and args[0] and not self._isAddon(args[0]):
self.onAppMsg(args[0])
# Load profile in a timer so we can let the window finish init and not
# close on profile load error.
@ -750,10 +750,14 @@ title="%s" %s>%s</button>""" % (
self.errorHandler = aqt.errors.ErrorHandler(self)
def setupAddons(self) -> None:
def setupAddons(self, args: Optional[List]) -> None:
import aqt.addons
self.addonManager = aqt.addons.AddonManager(self)
if args and args[0] and self._isAddon(args[0]):
self.installAddon(args[0], startup=True)
if not self.safeMode:
self.addonManager.loadAddons()
@ -1028,10 +1032,14 @@ QTreeWidget {
# Installing add-ons from CLI / mimetype handler
##########################################################################
def installAddon(self, path):
def installAddon(self, path: str, startup: bool = False):
from aqt.addons import installAddonPackages
installAddonPackages(self.addonManager, [path], external=True, parent=self)
parent = None if startup else self
installAddonPackages(
self.addonManager, [path], warn=True, strictly_modal=startup, parent=parent
)
# Cramming
##########################################################################