Files
Medios-Macina/cmdnat/__init__.py
T

48 lines
1.6 KiB
Python
Raw Normal View History

2025-12-05 03:42:57 -08:00
from __future__ import annotations
import os
from importlib import import_module
from typing import Any, Callable, Dict, Sequence
2026-05-26 15:32:01 -07:00
from SYS.cmdlet_spec import collect_registered_cmdlet_names
2025-12-05 03:42:57 -08:00
CmdletFn = Callable[[Any, Sequence[str], Dict[str, Any]], int]
def _register_cmdlet_object(cmdlet_obj, registry: Dict[str, CmdletFn]) -> None:
run_fn = getattr(cmdlet_obj, "exec", None) if hasattr(cmdlet_obj, "exec") else None
if not callable(run_fn):
return
2026-05-26 15:32:01 -07:00
for registered_name in collect_registered_cmdlet_names(cmdlet_obj):
registry[registered_name] = run_fn
2025-12-05 03:42:57 -08:00
2026-04-30 18:56:22 -07:00
def _iter_legacy_native_module_names() -> list[str]:
2025-12-05 03:42:57 -08:00
base_dir = os.path.dirname(__file__)
2026-04-30 18:56:22 -07:00
module_names: list[str] = []
2025-12-05 03:42:57 -08:00
for filename in os.listdir(base_dir):
if not (filename.endswith(".py") and not filename.startswith("_")
and filename != "__init__.py"):
2025-12-05 03:42:57 -08:00
continue
2026-04-30 18:56:22 -07:00
module_names.append(filename[:-3])
return module_names
2025-12-05 03:42:57 -08:00
2026-04-30 18:56:22 -07:00
def register_native_commands(registry: Dict[str, CmdletFn]) -> None:
"""Import legacy local command modules from cmdnat/ and register them."""
for mod_name in _iter_legacy_native_module_names():
2025-12-05 03:42:57 -08:00
try:
module = import_module(f".{mod_name}", __name__)
cmdlet_obj = getattr(module, "CMDLET", None)
if cmdlet_obj:
_register_cmdlet_object(cmdlet_obj, registry)
except Exception as exc:
import sys
2025-12-29 17:05:03 -08:00
print(
f"Error importing native command '{mod_name}': {exc}",
file=sys.stderr
)
2025-12-05 03:42:57 -08:00
continue