jw.util.Cmds, .Cmd, .App: Use jw.pkg.lib.App + .Cmd
This commit does a couple of things in one go:
- Move the implementations from jw.util.Cmds and jw.util.Cmd back to
jwutils where they came from to remain backwards compatible for
code still using jwutils.
- No code uses jw.util.Cmds and jw.util.Cmd yet, so remove them.
- Add new jw.util.App and jw.util.Cmd classes instead, which are
re-exported modules from jw.pkg.lib. These implementations are very
similar, but not identical to jwutils.Cmds and jwutils.App.
Originally, Cmds and Cmd had been copied from jw-python to jw-pkg App
and Cmd, respectively, because an App class proved indispensible in
jw-pkg. The two classes have subsequently evolved in jw-pkg, and
re-using them here ends the fork and removes the redundancy.
This makes jw-python expose the same App / Cmd pair as jw.pkg does
for as long as Cmds is still in use. In the future, it might be a
good idea to remove Cmds, Cmd and App from jw-python entirely.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-13 10:43:41 +02:00
|
|
|
from __future__ import annotations
|
2026-06-10 07:22:46 +02:00
|
|
|
|
jw.util.Cmds, .Cmd, .App: Use jw.pkg.lib.App + .Cmd
This commit does a couple of things in one go:
- Move the implementations from jw.util.Cmds and jw.util.Cmd back to
jwutils where they came from to remain backwards compatible for
code still using jwutils.
- No code uses jw.util.Cmds and jw.util.Cmd yet, so remove them.
- Add new jw.util.App and jw.util.Cmd classes instead, which are
re-exported modules from jw.pkg.lib. These implementations are very
similar, but not identical to jwutils.Cmds and jwutils.App.
Originally, Cmds and Cmd had been copied from jw-python to jw-pkg App
and Cmd, respectively, because an App class proved indispensible in
jw-pkg. The two classes have subsequently evolved in jw-pkg, and
re-using them here ends the fork and removes the redundancy.
This makes jw-python expose the same App / Cmd pair as jw.pkg does
for as long as Cmds is still in use. In the future, it might be a
good idea to remove Cmds, Cmd and App from jw-python entirely.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-13 10:43:41 +02:00
|
|
|
import abc
|
|
|
|
|
import argparse
|
|
|
|
|
import inspect
|
|
|
|
|
import re
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
|
|
|
|
|
|
from . import log
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from .Cmds import Cmds
|
|
|
|
|
|
|
|
|
|
# full blown example of one level of nested subcommands
|
|
|
|
|
# git -C project remote -v show -n myremote
|
|
|
|
|
|
|
|
|
|
class Cmd(abc.ABC): # export
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def run(self, args: Any) -> Any:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __init__(self, name: str, help: str) -> None:
|
|
|
|
|
self.name = name
|
|
|
|
|
self.help = help
|
|
|
|
|
self.parent = None
|
|
|
|
|
self.children: list[Cmd] = []
|
|
|
|
|
self.child_classes: list[type[Cmd]] = []
|
|
|
|
|
self.app: Cmds | None = None
|
|
|
|
|
|
|
|
|
|
async def _run(self, args: Any) -> Any:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def add_parser(self, parsers: Any) -> ArgumentParser:
|
|
|
|
|
r = parsers.add_parser(
|
|
|
|
|
self.name,
|
|
|
|
|
help = self.help,
|
|
|
|
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter
|
|
|
|
|
)
|
|
|
|
|
r.set_defaults(func = self.run)
|
|
|
|
|
return r # type: ignore[no-any-return]
|
|
|
|
|
|
|
|
|
|
def add_subcommands(self, cmd: str | type[Cmd] | list[type[Cmd]]) -> None:
|
|
|
|
|
if isinstance(cmd, str):
|
|
|
|
|
sc = []
|
|
|
|
|
for name, obj in inspect.getmembers(sys.modules[self.__class__.__module__]):
|
|
|
|
|
if inspect.isclass(obj):
|
|
|
|
|
if re.search(cmd, str(obj)):
|
|
|
|
|
sc.append(obj)
|
|
|
|
|
log.slog(log.DEBUG, f"Found subcommand {obj}")
|
|
|
|
|
self.add_subcommands(sc)
|
|
|
|
|
return
|
|
|
|
|
if isinstance(cmd, list):
|
|
|
|
|
for c in cmd:
|
|
|
|
|
self.add_subcommands(c)
|
|
|
|
|
return
|
|
|
|
|
self.child_classes.append(cmd)
|
|
|
|
|
|
|
|
|
|
# To be overridden by derived class in case the command does take arguments.
|
|
|
|
|
# Will be called from App base class constructor and set up the parser hierarchy
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def conf_value(self, path: str, default: Any = None) -> Any:
|
|
|
|
|
ret = None if self.app is None else self.app.conf_value(path, default)
|
|
|
|
|
if ret is None and default is not None:
|
|
|
|
|
return default
|
|
|
|
|
return ret
|