搜索插件并缓存插件名称。
(self)
| 95 | } |
| 96 | |
| 97 | def _prepare_plugins(self) -> set[str]: |
| 98 | """搜索插件并缓存插件名称。""" |
| 99 | # get all previous ready to load plugins |
| 100 | previous_plugin_ids = self._previous_controlled_modules() |
| 101 | |
| 102 | # if self not in global managers, merge self's controlled modules |
| 103 | def get_controlled_modules(): |
| 104 | return ( |
| 105 | previous_plugin_ids |
| 106 | if self in _managers |
| 107 | else {**previous_plugin_ids, **self.controlled_modules} |
| 108 | ) |
| 109 | |
| 110 | # check third party plugins |
| 111 | for plugin in self.plugins: |
| 112 | plugin_id = _module_name_to_plugin_id(plugin, get_controlled_modules()) |
| 113 | if ( |
| 114 | plugin_id in self._third_party_plugin_ids |
| 115 | or plugin_id in previous_plugin_ids |
| 116 | ): |
| 117 | raise RuntimeError( |
| 118 | f"Plugin already exists: {plugin_id}! Check your plugin name" |
| 119 | ) |
| 120 | self._third_party_plugin_ids[plugin_id] = plugin |
| 121 | |
| 122 | # check plugins in search path |
| 123 | for module_info in pkgutil.iter_modules(self.search_path): |
| 124 | # ignore if startswith "_" |
| 125 | if module_info.name.startswith("_"): |
| 126 | continue |
| 127 | |
| 128 | if not ( |
| 129 | module_spec := module_info.module_finder.find_spec( |
| 130 | module_info.name, None |
| 131 | ) |
| 132 | ): |
| 133 | continue |
| 134 | |
| 135 | if not module_spec.origin: |
| 136 | continue |
| 137 | |
| 138 | # get module name from path, pkgutil does not return the actual module name |
| 139 | module_path = Path(module_spec.origin).resolve() |
| 140 | module_name = path_to_module_name(module_path) |
| 141 | plugin_id = _module_name_to_plugin_id(module_name, get_controlled_modules()) |
| 142 | |
| 143 | if ( |
| 144 | plugin_id in previous_plugin_ids |
| 145 | or plugin_id in self._third_party_plugin_ids |
| 146 | or plugin_id in self._searched_plugin_ids |
| 147 | ): |
| 148 | raise RuntimeError( |
| 149 | f"Plugin already exists: {plugin_id}! Check your plugin name" |
| 150 | ) |
| 151 | |
| 152 | self._searched_plugin_ids[plugin_id] = module_name |
| 153 | |
| 154 | return self.available_plugins |
no test coverage detected