update and cleanup repo
This commit is contained in:
+17
-9
@@ -1,7 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from importlib import import_module
|
||||
from pathlib import Path
|
||||
import pkgutil
|
||||
from typing import Any, Callable, Dict, Iterable, Sequence
|
||||
|
||||
|
||||
@@ -64,22 +65,29 @@ def _register_command_object(cmdlet_obj: Any, registry: Dict[str, CmdletFn]) ->
|
||||
|
||||
def iter_plugin_command_module_names() -> list[str]:
|
||||
try:
|
||||
repo_root = Path(__file__).resolve().parent.parent
|
||||
package = import_module("plugins")
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
plugins_dir = repo_root / "plugins"
|
||||
if not plugins_dir.is_dir():
|
||||
package_path = getattr(package, "__path__", None)
|
||||
if not package_path:
|
||||
return []
|
||||
|
||||
module_names: list[str] = []
|
||||
for entry in sorted(plugins_dir.iterdir(), key=lambda path: path.name.lower()):
|
||||
if not entry.is_dir() or entry.name.startswith("."):
|
||||
seen: set[str] = set()
|
||||
for _, module_name, is_package in pkgutil.iter_modules(package_path):
|
||||
if not is_package or module_name.startswith("_"):
|
||||
continue
|
||||
if not (entry / "__init__.py").is_file():
|
||||
commands_module = f"plugins.{module_name}.commands"
|
||||
try:
|
||||
if importlib.util.find_spec(commands_module) is None:
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
if (entry / "commands.py").is_file() or (entry / "commands" / "__init__.py").is_file():
|
||||
module_names.append(f"plugins.{entry.name}.commands")
|
||||
if commands_module in seen:
|
||||
continue
|
||||
seen.add(commands_module)
|
||||
module_names.append(commands_module)
|
||||
return module_names
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user