插件管理器。 参数: plugins: 独立插件模块名集合。 search_path: 插件搜索路径(文件夹),相对于当前工作目录。
| 33 | |
| 34 | |
| 35 | class PluginManager: |
| 36 | """插件管理器。 |
| 37 | |
| 38 | 参数: |
| 39 | plugins: 独立插件模块名集合。 |
| 40 | search_path: 插件搜索路径(文件夹),相对于当前工作目录。 |
| 41 | """ |
| 42 | |
| 43 | def __init__( |
| 44 | self, |
| 45 | plugins: Iterable[str] | None = None, |
| 46 | search_path: Iterable[str] | None = None, |
| 47 | ): |
| 48 | # simple plugin not in search path |
| 49 | self.plugins: set[str] = set(plugins or []) |
| 50 | self.search_path: set[str] = set(search_path or []) |
| 51 | |
| 52 | # cache plugins |
| 53 | self._third_party_plugin_ids: dict[str, str] = {} |
| 54 | self._searched_plugin_ids: dict[str, str] = {} |
| 55 | self._prepare_plugins() |
| 56 | |
| 57 | def __repr__(self) -> str: |
| 58 | return f"PluginManager(available_plugins={self.controlled_modules})" |
| 59 | |
| 60 | @property |
| 61 | def third_party_plugins(self) -> set[str]: |
| 62 | """返回所有独立插件标识符。""" |
| 63 | return set(self._third_party_plugin_ids.keys()) |
| 64 | |
| 65 | @property |
| 66 | def searched_plugins(self) -> set[str]: |
| 67 | """返回已搜索到的插件标识符。""" |
| 68 | return set(self._searched_plugin_ids.keys()) |
| 69 | |
| 70 | @property |
| 71 | def available_plugins(self) -> set[str]: |
| 72 | """返回当前插件管理器中可用的插件标识符。""" |
| 73 | return self.third_party_plugins | self.searched_plugins |
| 74 | |
| 75 | @property |
| 76 | def controlled_modules(self) -> dict[str, str]: |
| 77 | """返回当前插件管理器中控制的插件标识符与模块路径映射字典。""" |
| 78 | return dict( |
| 79 | chain( |
| 80 | self._third_party_plugin_ids.items(), self._searched_plugin_ids.items() |
| 81 | ) |
| 82 | ) |
| 83 | |
| 84 | def _previous_controlled_modules(self) -> dict[str, str]: |
| 85 | _pre_managers: list[PluginManager] |
| 86 | if self in _managers: |
| 87 | _pre_managers = _managers[: _managers.index(self)] |
| 88 | else: |
| 89 | _pre_managers = _managers[:] |
| 90 | |
| 91 | return { |
| 92 | plugin_id: module_name |
no outgoing calls