Skip to content

runtime.context

Context

Bases: collections.UserDict[str, t.Any]

Context is a dict-like object that serves as the shared execution state for all arc component

Source code in /home/runner/work/arc/arc/arc/runtime/context.py
class Context(collections.UserDict[str, t.Any]):
    """Context is a dict-like object that serves as
    the shared execution state for all `arc` component
    """

    @property
    def root(self) -> Command:
        """The root command object. Alias for `ctx["arc.root"]`"""
        return self["arc.root"]

    @property
    def command(self) -> Command:
        """The command object being executed. Alias for `ctx["arc.command"]`"""
        return self["arc.command"]

    @property
    def state(self) -> dict[str, t.Any]:
        """The state object passed in. Alias for `ctx["arc.state"]`"""
        return self["arc.state"]

    @property
    def logger(self) -> Logger:
        """The arc logger. Alias for `ctx["arc.logger"]`"""
        return self["arc.logger"]

    @property
    def app(self) -> App:
        """The arc app. Alias for `ctx["arc.app"]`"""
        return self["arc.app"]

    @property
    def config(self) -> Config:
        """The configuration for this app. Alias for `ctx["arc.config"]`"""
        return self["arc.config"]

    @property
    def prompt(self) -> Prompt:
        """The prompt object congigured. Alias for `ctx["arc.config"].prompt`"""
        return self.config.prompt

    def execute(self, command: Command, **kwargs: t.Any) -> t.Any:
        """Execute a command within the context of another command
        ```py
        import arc

        @arc.command
        def command(ctx: arc.Context):
            ctx.execute(sub, val=2)

        @command.subcommand
        def sub(val: int):
            print(sub)
        ```

        """
        return self.app.execute(command, **kwargs)

    def set_origin(self, param_name: str, origin: str) -> None:
        """Sets the origin of a parameter"""
        origins = self.setdefault("arc.args.origins", {})
        origins[param_name] = origin

    @t.overload
    def get_origin(self, param_name: str) -> str | None:
        ...

    @t.overload
    def get_origin(self, param_name: str, default: T) -> str | T:
        ...

    def get_origin(self, param_name: str, default: T | None = None) -> str | T | None:
        """Gets the origin of a paramter"""
        origins: dict[str, str] | None = self.get("arc.args.origins")

        if not origins:
            return default

        return origins.get(param_name, default)

    @classmethod
    def __depends__(cls, ctx: Context) -> Context:
        """Makes the context a dependency"""
        return ctx

app: App property

The arc app. Alias for ctx["arc.app"]

command: Command property

The command object being executed. Alias for ctx["arc.command"]

config: Config property

The configuration for this app. Alias for ctx["arc.config"]

logger: Logger property

The arc logger. Alias for ctx["arc.logger"]

prompt: Prompt property

The prompt object congigured. Alias for ctx["arc.config"].prompt

root: Command property

The root command object. Alias for ctx["arc.root"]

state: dict[str, t.Any] property

The state object passed in. Alias for ctx["arc.state"]

__depends__(ctx) classmethod

Makes the context a dependency

Source code in /home/runner/work/arc/arc/arc/runtime/context.py
@classmethod
def __depends__(cls, ctx: Context) -> Context:
    """Makes the context a dependency"""
    return ctx

execute(command, **kwargs)

Execute a command within the context of another command

import arc

@arc.command
def command(ctx: arc.Context):
    ctx.execute(sub, val=2)

@command.subcommand
def sub(val: int):
    print(sub)

Source code in /home/runner/work/arc/arc/arc/runtime/context.py
def execute(self, command: Command, **kwargs: t.Any) -> t.Any:
    """Execute a command within the context of another command
    ```py
    import arc

    @arc.command
    def command(ctx: arc.Context):
        ctx.execute(sub, val=2)

    @command.subcommand
    def sub(val: int):
        print(sub)
    ```

    """
    return self.app.execute(command, **kwargs)

get_origin(param_name, default=None)

Gets the origin of a paramter

Source code in /home/runner/work/arc/arc/arc/runtime/context.py
def get_origin(self, param_name: str, default: T | None = None) -> str | T | None:
    """Gets the origin of a paramter"""
    origins: dict[str, str] | None = self.get("arc.args.origins")

    if not origins:
        return default

    return origins.get(param_name, default)

set_origin(param_name, origin)

Sets the origin of a parameter

Source code in /home/runner/work/arc/arc/arc/runtime/context.py
def set_origin(self, param_name: str, origin: str) -> None:
    """Sets the origin of a parameter"""
    origins = self.setdefault("arc.args.origins", {})
    origins[param_name] = origin