Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add add-on installation hooks #2523

Merged
merged 3 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions qt/aqt/addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ def install(
conflicts = manifest.get("conflicts", [])
found_conflicts = self._disableConflicting(package, conflicts)
meta = self.addonMeta(package)
gui_hooks.addon_manager_will_install_addon(self, package)
self._install(package, zfile)
gui_hooks.addon_manager_did_install_addon(self, package)

schema = self._manifest_schema["properties"]
manifest_meta = {
Expand All @@ -450,10 +452,11 @@ def _install(self, module: str, zfile: ZipFile) -> None:
base = self.addonsFolder(module)
if os.path.exists(base):
self.backupUserFiles(module)
if not self.deleteAddon(module):
try:
self.deleteAddon(module)
except Exception:
self.restoreUserFiles(module)
return

raise
os.mkdir(base)
self.restoreUserFiles(module)

Expand All @@ -469,17 +472,8 @@ def _install(self, module: str, zfile: ZipFile) -> None:
continue
zfile.extract(n, base)

# true on success
def deleteAddon(self, module: str) -> bool:
try:
send_to_trash(Path(self.addonsFolder(module)))
return True
except OSError as e:
showWarning(
tr.addons_unable_to_update_or_delete_addon(val=str(e)),
textFormat="plain",
)
return False
def deleteAddon(self, module: str) -> None:
send_to_trash(Path(self.addonsFolder(module)))

# Processing local add-on files
######################################################################
Expand Down Expand Up @@ -912,12 +906,17 @@ def onDelete(self) -> None:
if not askUser(tr.addons_delete_the_numd_selected_addon(count=len(selected))):
return
gui_hooks.addons_dialog_will_delete_addons(self, selected)
for module in selected:
# doing this before deleting, as `enabled` is always True afterwards
if self.mgr.addon_meta(module).enabled:
self._require_restart = True
if not self.mgr.deleteAddon(module):
break
try:
for module in selected:
# doing this before deleting, as `enabled` is always True afterwards
if self.mgr.addon_meta(module).enabled:
self._require_restart = True
self.mgr.deleteAddon(module)
except OSError as e:
showWarning(
tr.addons_unable_to_update_or_delete_addon(val=str(e)),
textFormat="plain",
)
self.form.addonList.clearSelection()
self.redrawAddons()

Expand Down
16 changes: 16 additions & 0 deletions qt/tools/genhooks_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,22 @@ def might_reject_empty_tag(optional_problems, note):
args=["dialog: aqt.addons.AddonsDialog", "ids: list[str]"],
doc="""Allows doing an action before an add-on is deleted.""",
),
Hook(
name="addon_manager_will_install_addon",
args=["manager: aqt.addons.AddonManager", "module: str"],
doc="""Called before installing or updating an addon.

Can be used to release DB connections or open files that
would prevent an update from succeeding.""",
),
Hook(
name="addon_manager_did_install_addon",
args=["manager: aqt.addons.AddonManager", "module: str"],
doc="""Called after installing or updating an addon.

Can be used to restore DB connections or open files after
an add-on has been updated.""",
),
# Model
###################
Hook(
Expand Down