Skip to content

Commit

Permalink
Merge pull request #596 from nicola-corbellini/feature/plugin_hooks
Browse files Browse the repository at this point in the history
Add event on plugin de-/activation
  • Loading branch information
Pingdred authored Nov 30, 2023
2 parents 455347d + 581c821 commit a6698ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
26 changes: 25 additions & 1 deletion core/cat/mad_hatter/core_plugin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,28 @@ def save_settings(settings):
"""

# In core_plugin we do nothing (for now).
return {}
return {}


@plugin
def activated(plugin):
"""This method allows executing custom code right after a plugin is activated.
Parameters
----------
plugin
Plugin: Cat object representing the plugin instance in memory.
"""
return None


@plugin
def deactivated(plugin):
"""This method allows executing custom code right after a plugin is deactivated.
Parameters
----------
plugin
Plugin: Cat object representing the plugin instance in memory.
"""
return None
17 changes: 17 additions & 0 deletions core/cat/mad_hatter/mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,31 @@ def toggle_plugin(self, plugin_id):
# update list of active plugins
if plugin_is_active:
log.warning(f"Toggle plugin {plugin_id}: Deactivate")

# Execute hook on plugin deactivation
# Deactivation hook must happen before actual deactivation,
# otherwise the hook will not be available in _plugin_overrides anymore
for hook in self.plugins[plugin_id]._plugin_overrides:
if hook.name == "deactivated":
hook.function(self.plugins[plugin_id])

# Deactivate the plugin
self.plugins[plugin_id].deactivate()
# Remove the plugin from the list of active plugins
self.active_plugins.remove(plugin_id)
else:
log.warning(f"Toggle plugin {plugin_id}: Activate")

# Activate the plugin
self.plugins[plugin_id].activate()

# Execute hook on plugin activation
# Activation hook must happen before actual activation,
# otherwise the hook will still not be available in _plugin_overrides
for hook in self.plugins[plugin_id]._plugin_overrides:
if hook.name == "activated":
hook.function(self.plugins[plugin_id])

# Add the plugin in the list of active plugins
self.active_plugins.append(plugin_id)

Expand Down

0 comments on commit a6698ce

Please sign in to comment.