From da4447e1986a05ab0213274998fdc8324fbc8f39 Mon Sep 17 00:00:00 2001 From: Christopher Schramm Date: Wed, 24 Jul 2024 13:46:19 +0200 Subject: [PATCH] Actually make PluginManager generic (#2437) --- blueman/gui/applet/PluginDialog.py | 8 +++++--- blueman/main/Applet.py | 2 +- blueman/main/PluginManager.py | 6 +++--- blueman/plugins/applet/ShowConnected.py | 2 +- blueman/plugins/applet/StatusIcon.py | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/blueman/gui/applet/PluginDialog.py b/blueman/gui/applet/PluginDialog.py index cdb8566af..3f2be5871 100644 --- a/blueman/gui/applet/PluginDialog.py +++ b/blueman/gui/applet/PluginDialog.py @@ -1,11 +1,11 @@ from gettext import gettext as _ import logging -from typing import TYPE_CHECKING, Type, Dict, cast, Optional +from typing import TYPE_CHECKING, Type, Dict, cast, Optional, TypeVar from blueman.main.Builder import Builder from blueman.main.PluginManager import PluginManager from blueman.plugins.AppletPlugin import AppletPlugin -from blueman.plugins.BasePlugin import Option +from blueman.plugins.BasePlugin import Option, BasePlugin import gi gi.require_version("Gtk", "3.0") @@ -329,7 +329,9 @@ def populate(self) -> None: self.model.insert_sorted(plugin_item, self._model_sort_func) self.listbox.select_row(self.listbox.get_row_at_index(0)) - def plugin_state_changed(self, _plugins: PluginManager, name: str, loaded: bool) -> None: + _T = TypeVar("_T", bound="BasePlugin") + + def plugin_state_changed(self, _plugins: PluginManager[_T], name: str, loaded: bool) -> None: logging.debug(f"{name} {loaded}") action = self.lookup_action(name) assert isinstance(action, Gio.SimpleAction) diff --git a/blueman/main/Applet.py b/blueman/main/Applet.py index ad69aad26..9e51f9d9f 100644 --- a/blueman/main/Applet.py +++ b/blueman/main/Applet.py @@ -115,7 +115,7 @@ def on_device_removed(self, _manager: Manager, path: str) -> None: plugin.on_device_removed(path) -class Plugins(PersistentPluginManager): +class Plugins(PersistentPluginManager[AppletPlugin]): def __init__(self, applet: BluemanApplet): super().__init__(AppletPlugin, blueman.plugins.applet, applet) diff --git a/blueman/main/PluginManager.py b/blueman/main/PluginManager.py index f44c15211..8b2d1a761 100644 --- a/blueman/main/PluginManager.py +++ b/blueman/main/PluginManager.py @@ -4,7 +4,7 @@ import traceback import importlib from types import ModuleType -from typing import Dict, List, Type, TypeVar, Iterable, Optional +from typing import Dict, List, Type, TypeVar, Iterable, Optional, Generic from gi.repository import GObject, Gio @@ -22,7 +22,7 @@ class LoadException(Exception): _T = TypeVar("_T", bound=BasePlugin) -class PluginManager(GObject.GObject): +class PluginManager(GObject.GObject, Generic[_T]): __gsignals__: GSignals = { 'plugin-loaded': (GObject.SignalFlags.NO_HOOKS, None, (GObject.TYPE_STRING,)), 'plugin-unloaded': (GObject.SignalFlags.NO_HOOKS, None, (GObject.TYPE_STRING,)), @@ -211,7 +211,7 @@ def get_loaded_plugins(self, protocol: Type[_U]) -> Iterable[_U]: yield plugin -class PersistentPluginManager(PluginManager): +class PersistentPluginManager(PluginManager[_T]): def __init__(self, plugin_class: Type[_T], module_path: ModuleType, parent: object) -> None: super().__init__(plugin_class, module_path, parent) diff --git a/blueman/plugins/applet/ShowConnected.py b/blueman/plugins/applet/ShowConnected.py index 3b99171ce..9d9c918e9 100644 --- a/blueman/plugins/applet/ShowConnected.py +++ b/blueman/plugins/applet/ShowConnected.py @@ -111,6 +111,6 @@ def on_adapter_added(self, _path: str) -> None: def on_adapter_removed(self, _path: str) -> None: self.enumerate_connections() - def _on_plugins_changed(self, _pluginmngr: PluginManager, name: str) -> None: + def _on_plugins_changed(self, _pluginmngr: PluginManager[AppletPlugin], name: str) -> None: if name == "PowerManager": self.update_statusicon() diff --git a/blueman/plugins/applet/StatusIcon.py b/blueman/plugins/applet/StatusIcon.py index 6c2efd89c..cb01e80e3 100644 --- a/blueman/plugins/applet/StatusIcon.py +++ b/blueman/plugins/applet/StatusIcon.py @@ -106,7 +106,7 @@ def on_manager_state_changed(self, state: bool) -> None: if state: launch('blueman-tray', icon_name='blueman', sn=False) - def _on_plugins_changed(self, _plugins: PluginManager, _name: str) -> None: + def _on_plugins_changed(self, _plugins: PluginManager[AppletPlugin], _name: str) -> None: implementations = self._get_status_icon_implementations() if not self._implementations or self._implementations != implementations: self._implementations = implementations