j
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, Dict, Iterable, Sequence
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Callable, Dict, Iterable, Iterator, Sequence
|
||||
from importlib import import_module as _import_module
|
||||
|
||||
# A cmdlet is a callable taking (result, args, config) -> int
|
||||
@@ -47,51 +49,71 @@ def get(cmd_name: str) -> Cmdlet | None:
|
||||
return REGISTRY.get(_normalize_cmd_name(cmd_name))
|
||||
|
||||
|
||||
# Dynamically import all cmdlet modules in this directory (ignore files starting with _ and __init__.py)
|
||||
# cmdlet self-register when instantiated via their __init__ method
|
||||
import os
|
||||
_MODULES_LOADED = False
|
||||
|
||||
cmdlet_dir = os.path.dirname(__file__)
|
||||
for filename in os.listdir(cmdlet_dir):
|
||||
if not (filename.endswith(".py") and not filename.startswith("_")
|
||||
and filename != "__init__.py"):
|
||||
continue
|
||||
def _iter_cmdlet_module_names() -> Iterator[str]:
|
||||
cmdlet_dir = os.path.dirname(__file__)
|
||||
try:
|
||||
entries = os.listdir(cmdlet_dir)
|
||||
except Exception:
|
||||
return iter(())
|
||||
|
||||
mod_name = filename[:-3]
|
||||
def _generator() -> Iterator[str]:
|
||||
for filename in entries:
|
||||
if not (filename.endswith(".py") and not filename.startswith("_")
|
||||
and filename != "__init__.py"):
|
||||
continue
|
||||
mod_name = filename[:-3]
|
||||
if "_" not in mod_name:
|
||||
continue
|
||||
yield mod_name
|
||||
|
||||
# Enforce Powershell-style two-word cmdlet naming (e.g., add_file, get_file)
|
||||
# Skip native/utility scripts that are not cmdlet (e.g., adjective, worker, matrix, pipe)
|
||||
if "_" not in mod_name:
|
||||
continue
|
||||
return _generator()
|
||||
|
||||
|
||||
def _load_cmdlet_module(mod_name: str) -> None:
|
||||
try:
|
||||
_import_module(f".{mod_name}", __name__)
|
||||
except Exception as e:
|
||||
import sys
|
||||
except Exception as exc:
|
||||
print(f"Error importing cmdlet '{mod_name}': {exc}", file=sys.stderr)
|
||||
|
||||
print(f"Error importing cmdlet '{mod_name}': {e}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
# Import and register native commands that are not considered cmdlet
|
||||
try:
|
||||
from cmdnat import register_native_commands as _register_native_commands
|
||||
def _load_root_modules() -> None:
|
||||
for root in ("select_cmdlet",):
|
||||
try:
|
||||
_import_module(root)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
_register_native_commands(REGISTRY)
|
||||
except Exception:
|
||||
# Native commands are optional; ignore if unavailable
|
||||
pass
|
||||
|
||||
# Import root-level modules that also register cmdlet
|
||||
for _root_mod in ("select_cmdlet",
|
||||
):
|
||||
def _load_helper_modules() -> None:
|
||||
try:
|
||||
_import_module(_root_mod)
|
||||
import API.alldebrid as _alldebrid
|
||||
except Exception:
|
||||
# Allow missing optional modules
|
||||
continue
|
||||
pass
|
||||
|
||||
# Also import helper modules that register cmdlet
|
||||
try:
|
||||
import API.alldebrid as _alldebrid
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _register_native_commands() -> None:
|
||||
try:
|
||||
from cmdnat import register_native_commands
|
||||
except Exception:
|
||||
return
|
||||
try:
|
||||
register_native_commands(REGISTRY)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def ensure_cmdlet_modules_loaded() -> None:
|
||||
global _MODULES_LOADED
|
||||
|
||||
if _MODULES_LOADED:
|
||||
return
|
||||
|
||||
for mod_name in _iter_cmdlet_module_names():
|
||||
_load_cmdlet_module(mod_name)
|
||||
|
||||
_load_root_modules()
|
||||
_load_helper_modules()
|
||||
_register_native_commands()
|
||||
_MODULES_LOADED = True
|
||||
|
||||
Reference in New Issue
Block a user