Skip to content

CommandRegistry & CommandHandle

Bases: Generic[T]

Registry for stack commands.

Source code in orche/stack.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class CommandRegistry(Generic[T]):
    """Registry for stack commands."""

    def __init__(self) -> None:
        self._commands: dict[str, Callable[[], None]] = {}
        self._before_hooks: dict[str, list[Callable[[], None]]] = {}
        self._after_hooks: dict[str, list[Callable[[], None]]] = {}

    def register(self, name: str) -> "CommandHandle[T]":
        """Decorator to register a command."""
        return CommandHandle(name, self)

    @property
    def up(self) -> "CommandHandle[T]":
        """Decorator for the 'up' command."""
        return self.register("up")

    @property
    def down(self) -> "CommandHandle[T]":
        """Decorator for the 'down' command."""
        return self.register("down")

    @property
    def build(self) -> "CommandHandle[T]":
        """Decorator for the 'build' command."""
        return self.register("build")

    @property
    def stop(self) -> "CommandHandle[T]":
        """Decorator for the 'stop' command."""
        return self.register("stop")

    def get(self, name: str) -> Callable[[], None] | None:
        """Get a registered command handler."""
        return self._commands.get(name)

    def available_commands(self) -> list[str]:
        """Return list of registered command names."""
        return list(self._commands.keys())

    def get_before_hooks(self, name: str) -> list[Callable[[], None]]:
        """Return before-hooks for the given command."""
        return self._before_hooks.get(name, [])

    def get_after_hooks(self, name: str) -> list[Callable[[], None]]:
        """Return after-hooks for the given command."""
        return self._after_hooks.get(name, [])

build property

Decorator for the 'build' command.

down property

Decorator for the 'down' command.

stop property

Decorator for the 'stop' command.

up property

Decorator for the 'up' command.

available_commands()

Return list of registered command names.

Source code in orche/stack.py
109
110
111
def available_commands(self) -> list[str]:
    """Return list of registered command names."""
    return list(self._commands.keys())

get(name)

Get a registered command handler.

Source code in orche/stack.py
105
106
107
def get(self, name: str) -> Callable[[], None] | None:
    """Get a registered command handler."""
    return self._commands.get(name)

get_after_hooks(name)

Return after-hooks for the given command.

Source code in orche/stack.py
117
118
119
def get_after_hooks(self, name: str) -> list[Callable[[], None]]:
    """Return after-hooks for the given command."""
    return self._after_hooks.get(name, [])

get_before_hooks(name)

Return before-hooks for the given command.

Source code in orche/stack.py
113
114
115
def get_before_hooks(self, name: str) -> list[Callable[[], None]]:
    """Return before-hooks for the given command."""
    return self._before_hooks.get(name, [])

register(name)

Decorator to register a command.

Source code in orche/stack.py
81
82
83
def register(self, name: str) -> "CommandHandle[T]":
    """Decorator to register a command."""
    return CommandHandle(name, self)

Bases: Generic[T]

A command slot with optional before/after hooks.

Source code in orche/stack.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class CommandHandle(Generic[T]):
    """A command slot with optional before/after hooks."""

    def __init__(self, name: str, registry: "CommandRegistry[T]") -> None:
        self._name = name
        self._registry = registry

    def __call__(self, func: T) -> T:
        """Register func as the main handler (backward-compatible decorator)."""
        self._registry._commands[self._name] = func  # type: ignore[assignment]
        return func

    @property
    def before(self) -> Callable[[T], T]:
        """Decorator to register a before-hook for this command."""

        def decorator(func: T) -> T:
            self._registry._before_hooks.setdefault(self._name, []).append(func)  # type: ignore[arg-type]
            return func

        return decorator

    @property
    def after(self) -> Callable[[T], T]:
        """Decorator to register an after-hook for this command."""

        def decorator(func: T) -> T:
            self._registry._after_hooks.setdefault(self._name, []).append(func)  # type: ignore[arg-type]
            return func

        return decorator

after property

Decorator to register an after-hook for this command.

before property

Decorator to register a before-hook for this command.

__call__(func)

Register func as the main handler (backward-compatible decorator).

Source code in orche/stack.py
47
48
49
50
def __call__(self, func: T) -> T:
    """Register func as the main handler (backward-compatible decorator)."""
    self._registry._commands[self._name] = func  # type: ignore[assignment]
    return func