• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LCA-ActivityBrowser / activity-browser / 10885577882

16 Sep 2024 02:09PM UTC coverage: 54.118% (+0.02%) from 54.099%
10885577882

push

github

web-flow
Merge pull request #1347 from marc-vdm/plugin-import-improvement

LGTM!

5 of 21 new or added lines in 1 file covered. (23.81%)

2 existing lines in 1 file now uncovered.

8280 of 15300 relevant lines covered (54.12%)

0.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.28
/activity_browser/controllers/plugin.py
1
# -*- coding: utf-8 -*-
2
import importlib.util
1✔
3
from pkgutil import iter_modules
1✔
4
from logging import getLogger
1✔
5

6
from PySide2.QtCore import QObject
1✔
7

8
from activity_browser import ab_settings, application, project_settings, signals
1✔
9
from activity_browser.mod import bw2data as bd
1✔
10

11
log = getLogger(__name__)
1✔
12

13

14
class PluginController(QObject):
1✔
15

16
    def __init__(self, parent=None):
1✔
17
        super().__init__(parent)
1✔
18
        self.connect_signals()
1✔
19
        # Shortcut to ab_settings plugins list
20
        self.plugins = ab_settings.plugins
1✔
21
        self.import_plugins()
1✔
22

23
    def connect_signals(self):
1✔
24
        bd.projects.current_changed.connect(self.reload_plugins)
1✔
25
        signals.plugin_selected.connect(self.load_plugin)
1✔
26

27
    def import_plugins(self):
1✔
28
        """Import plugins from python environment."""
29
        names = self.discover_plugins()
1✔
30
        for name in names:
1✔
NEW
31
            plugin = self.import_plugin(name)
×
NEW
32
            if plugin:
×
NEW
33
               self.plugins[name] = plugin
×
34

35
    def discover_plugins(self):
1✔
36
        """Discover available plugins in python environment."""
37
        plugins = []
1✔
38
        for module in iter_modules():
1✔
39
            if module.name.startswith("ab_plugin"):
1✔
40
                plugins.append(module.name)
×
41
        return plugins
1✔
42

43
    def import_plugin(self, name):
1✔
44
        """Import plugin from python environment."""
45
        try:
×
46
            log.info("Importing plugin {}".format(name))
×
47
            plugin_lib = importlib.import_module(name)
×
48
            importlib.reload(plugin_lib)
×
49
            return plugin_lib.Plugin()
×
50
        except Exception as e:
×
NEW
51
            log.error(f"Import of plugin module '{name}' failed. "
×
52
                      "If this keeps happening contact the plugin developers and let them know of this error:"
53
                      f"\n{e}")
NEW
54
            return None
×
55

56
    def load_plugin(self, name, select: bool = True):
1✔
57
        """Load or unload the Plugin, depending on select."""
58
        if select:
×
59
            # load the plugin
UNCOV
60
            plugin = self.plugins[name]
×
UNCOV
61
            try:
×
62
                plugin.load()
×
63
            except Exception as e:
×
NEW
64
                log.error(f"Loading of plugin '{name}' failed. "
×
65
                            "If this keeps happening contact the plugin developers and let them know of this error:"
66
                            f"\n{e}")
67
                return
×
68

69
            # Add plugins tabs
70
            for tab in plugin.tabs:
×
71
                application.main_window.add_tab_to_panel(
×
72
                    tab, plugin.infos["name"], tab.panel
73
                )
NEW
74
            log.info(f"Loaded tab '{name}'")
×
75
            return
×
76

77
        # not select, remove the plugin
NEW
78
        log.info(f"Removing plugin '{name}'")
×
79

NEW
80
        self.close_plugin_tabs(self.plugins[name])  # close tabs in AB
×
NEW
81
        self.plugins[name].close()  # call close of the plugin
×
NEW
82
        self.plugins[name].remove()  # call remove of the plugin
×
83

84
    def close_plugin_tabs(self, plugin):
1✔
85
        for panel in (
×
86
            application.main_window.left_panel,
87
            application.main_window.right_panel,
88
        ):
89
            panel.close_tab_by_tab_name(plugin.infos["name"])
×
90

91
    def reload_plugins(self):
1✔
92
        """close all plugins then reload all plugins."""
93
        plugins_list = [name for name in self.plugins.keys()]  # copy plugins list
1✔
94
        for name in plugins_list:
1✔
NEW
95
            self.close_plugin_tabs(self.plugins[name])  # close tabs in AB
×
NEW
96
            self.plugins[name].close()  # call close of the plugin
×
97
        for name in project_settings.get_plugins_list():
1✔
NEW
98
            if name in self.plugins:
×
NEW
99
                self.load_plugin(name)
×
100
            else:
NEW
101
                log.warning(f"Reloading of plugin '{name}' was skipped due to a previous error. "
×
102
                            "To reload this plugin, restart Activity Browser")
103

104
    def close(self):
1✔
105
        """Close all plugins, called when AB closes."""
106
        for plugin in self.plugins.values():
1✔
107
            plugin.close()
×
108

109

110
plugin_controller = PluginController(application)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc