Skip to content

CommandRegistry & CommandHandle

Bases: Generic[T]

Registry for stack commands.

Source code in orche/stack.py
 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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")

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

    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 hook_commands(self) -> list[str]:
        """Return command names that have at least one hook."""
        return list(dict.fromkeys([*self._before_hooks, *self._after_hooks]))

    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.

rm property

Decorator for the 'rm' 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
133
134
135
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
129
130
131
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
145
146
147
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
141
142
143
def get_before_hooks(self, name: str) -> list[Callable[..., None]]:
    """Return before-hooks for the given command."""
    return self._before_hooks.get(name, [])

hook_commands()

Return command names that have at least one hook.

Source code in orche/stack.py
137
138
139
def hook_commands(self) -> list[str]:
    """Return command names that have at least one hook."""
    return list(dict.fromkeys([*self._before_hooks, *self._after_hooks]))

register(name)

Decorator to register a command.

Source code in orche/stack.py
100
101
102
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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).

        The parameters of ``func`` define the command-line options it accepts.

        Raises:
            ConfigError: If a parameter has an unsupported type or a reserved name.
        """
        build_option_specs(func)
        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).

The parameters of func define the command-line options it accepts.

Raises:

Type Description
ConfigError

If a parameter has an unsupported type or a reserved name.

Source code in orche/stack.py
59
60
61
62
63
64
65
66
67
68
69
def __call__(self, func: T) -> T:
    """Register func as the main handler (backward-compatible decorator).

    The parameters of ``func`` define the command-line options it accepts.

    Raises:
        ConfigError: If a parameter has an unsupported type or a reserved name.
    """
    build_option_specs(func)
    self._registry._commands[self._name] = func  # type: ignore[assignment]
    return func