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

Better plugin logging #1347

Merged
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
58 changes: 35 additions & 23 deletions activity_browser/controllers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,50 @@ def __init__(self, parent=None):
self.connect_signals()
# Shortcut to ab_settings plugins list
self.plugins = ab_settings.plugins
self.load_plugins()
self.import_plugins()

def connect_signals(self):
bd.projects.current_changed.connect(self.reload_plugins)
signals.plugin_selected.connect(self.add_plugin)
signals.plugin_selected.connect(self.load_plugin)

def load_plugins(self):
def import_plugins(self):
"""Import plugins from python environment."""
names = self.discover_plugins()
for name in names:
self.plugins[name] = self.load_plugin(name)
plugin = self.import_plugin(name)
if plugin:
self.plugins[name] = plugin

def discover_plugins(self):
"""Discover available plugins in python environment."""
plugins = []
for module in iter_modules():
if module.name.startswith("ab_plugin"):
plugins.append(module.name)
return plugins

def load_plugin(self, name):
def import_plugin(self, name):
"""Import plugin from python environment."""
try:
log.info("Importing plugin {}".format(name))
plugin_lib = importlib.import_module(name)
importlib.reload(plugin_lib)
return plugin_lib.Plugin()
except Exception as e:
log.error(f"Import of plugin '{name}' failed: {e}")
log.error(f"Import of plugin module '{name}' failed. "
"If this keeps happening contact the plugin developers and let them know of this error:"
f"\n{e}")
return None

def add_plugin(self, name, select: bool = True):
"""add or reload tabs of the given plugin"""
def load_plugin(self, name, select: bool = True):
"""Load or unload the Plugin, depending on select."""
if select:
# load the plugin
plugin = self.plugins[name]
# Apply plugin load() function
try:
plugin.load()
except Exception as e:
log.warning(f"Failed to load plugin '{name}' due to an error in the plugin, ignoring plugin. "
log.error(f"Loading of plugin '{name}' failed. "
"If this keeps happening contact the plugin developers and let them know of this error:"
f"\n{e}")
return
Expand All @@ -63,13 +71,15 @@ def add_plugin(self, name, select: bool = True):
application.main_window.add_tab_to_panel(
tab, plugin.infos["name"], tab.panel
)
log.info("Loaded tab {}".format(name))
log.info(f"Loaded tab '{name}'")
return
log.info("Removing plugin {}".format(name))
# Apply plugin remove() function
self.plugins[name].remove()
# Close plugin tabs
self.close_plugin_tabs(self.plugins[name])

# not select, remove the plugin
log.info(f"Removing plugin '{name}'")

self.close_plugin_tabs(self.plugins[name]) # close tabs in AB
self.plugins[name].close() # call close of the plugin
self.plugins[name].remove() # call remove of the plugin

def close_plugin_tabs(self, plugin):
for panel in (
Expand All @@ -79,18 +89,20 @@ def close_plugin_tabs(self, plugin):
panel.close_tab_by_tab_name(plugin.infos["name"])

def reload_plugins(self):
"""close all plugins tabs then import all plugins tabs"""
"""close all plugins then reload all plugins."""
plugins_list = [name for name in self.plugins.keys()] # copy plugins list
for name in plugins_list:
self.close_plugin_tabs(self.plugins[name])
self.close_plugin_tabs(self.plugins[name]) # close tabs in AB
self.plugins[name].close() # call close of the plugin
for name in project_settings.get_plugins_list():
try:
self.add_plugin(name)
except:
log.warning(f"Tried to load plugin '{name}' but it is not installed, ignoring plugin")
if name in self.plugins:
self.load_plugin(name)
else:
log.warning(f"Reloading of plugin '{name}' was skipped due to a previous error. "
"To reload this plugin, restart Activity Browser")

def close(self):
"""close all plugins"""
"""Close all plugins, called when AB closes."""
for plugin in self.plugins.values():
plugin.close()

Expand Down
5 changes: 4 additions & 1 deletion activity_browser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ def __init__(self, filename: str):
self.write_settings()

def connect_signals(self):
"""Reload the project settings whenever a project switch occurs."""

# Reload the project settings whenever a project switch occurs.
bd.projects.current_changed.connect(self.reset_for_project_selection)

# save new plugin for this project
signals.plugin_selected.connect(self.add_plugin)

@classmethod
Expand Down
Loading