update refactoring and add new features
This commit is contained in:
+34
-27
@@ -330,36 +330,43 @@ class PluginRegistry:
|
||||
for plugin_dir in _iter_external_plugin_dirs():
|
||||
if self._is_builtin_package_dir(plugin_dir):
|
||||
continue
|
||||
|
||||
plugin_dir_str = str(plugin_dir)
|
||||
path_added = False
|
||||
if plugin_dir_str and plugin_dir_str not in sys.path:
|
||||
sys.path.insert(0, plugin_dir_str)
|
||||
path_added = True
|
||||
|
||||
try:
|
||||
plugin_dir_str = str(plugin_dir)
|
||||
if plugin_dir_str and plugin_dir_str not in sys.path:
|
||||
sys.path.insert(0, plugin_dir_str)
|
||||
except Exception:
|
||||
pass
|
||||
for module_name, module_path, is_package in _iter_external_plugin_entries(plugin_dir):
|
||||
if module_name in self._external_modules:
|
||||
continue
|
||||
|
||||
for module_name, module_path, is_package in _iter_external_plugin_entries(plugin_dir):
|
||||
if module_name in self._external_modules:
|
||||
continue
|
||||
try:
|
||||
if is_package:
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
module_name,
|
||||
str(module_path),
|
||||
submodule_search_locations=[str(module_path.parent)],
|
||||
)
|
||||
else:
|
||||
spec = importlib.util.spec_from_file_location(module_name, str(module_path))
|
||||
if spec is None or spec.loader is None:
|
||||
raise ImportError("missing module spec loader")
|
||||
|
||||
try:
|
||||
if is_package:
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
module_name,
|
||||
str(module_path),
|
||||
submodule_search_locations=[str(module_path.parent)],
|
||||
)
|
||||
else:
|
||||
spec = importlib.util.spec_from_file_location(module_name, str(module_path))
|
||||
if spec is None or spec.loader is None:
|
||||
raise ImportError("missing module spec loader")
|
||||
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[module_name] = module
|
||||
spec.loader.exec_module(module)
|
||||
self._external_modules.add(module_name)
|
||||
self._register_module(module)
|
||||
except Exception as exc:
|
||||
log(f"[plugin] Failed to load external plugin {module_path}: {exc}", file=sys.stderr)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[module_name] = module
|
||||
spec.loader.exec_module(module)
|
||||
self._external_modules.add(module_name)
|
||||
self._register_module(module)
|
||||
except Exception as exc:
|
||||
log(f"[plugin] Failed to load external plugin {module_path}: {exc}", file=sys.stderr)
|
||||
finally:
|
||||
if path_added:
|
||||
try:
|
||||
sys.path.remove(plugin_dir_str)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def discover(self) -> None:
|
||||
"""Import and register plugins from the package."""
|
||||
|
||||
Reference in New Issue
Block a user