Context Module API Reference¶
This section documents the internals of the context
module in Bijux CLI.
bijux_cli.core.context ¶
Provides a concrete implementation for request-scoped context management.
This module defines the Context
class, which implements the ContextProtocol
. It uses Python's contextvars
to provide a thread-safe and async-safe mechanism for storing and retrieving key-value data associated with a specific command execution or request. This allows state to be passed through the application's call stack without explicit argument passing.
Context ¶
Context(di: DIContainer)
Bases: ContextProtocol
Provides thread-safe, request-scoped storage for CLI commands.
This class uses contextvars
to manage a dictionary of data that is isolated to the current task or thread. It is intended to be used as both a synchronous and asynchronous context manager.
Attributes:
-
_di
(DIContainer
) –The dependency injection container.
-
_log
(ObservabilityProtocol
) –The logging service.
-
_data
(dict[str, Any]
) –The dictionary storing the context's data.
-
_token
(Token | None
) –The token for resetting the
ContextVar
.
Initializes a new Context instance.
Parameters:
-
di
(DIContainer
) –The dependency injection container used to resolve the logging service.
Source code in src/bijux_cli/core/context.py
clear ¶
Removes all values from the context's data.
current_data classmethod
¶
Returns the dictionary for the currently active CLI context.
This provides direct access to the data stored in the underlying contextvar
for the current execution scope.
Returns:
-
dict[str, Any]
–dict[str, Any]: The active context data dictionary.
Source code in src/bijux_cli/core/context.py
get ¶
Gets a value from the current context's data.
Parameters:
-
key
(str
) –The key of the value to retrieve.
Returns:
-
Any
(Any
) –The value associated with the key.
Raises:
-
KeyError
–If the key is not found in the context.
Source code in src/bijux_cli/core/context.py
set ¶
Sets a value in the current context's data.
Parameters:
-
key
(str
) –The key for the value.
-
value
(Any
) –The value to store.
Returns:
-
None
(None
) –
Source code in src/bijux_cli/core/context.py
set_current_data classmethod
¶
Sets the dictionary for the currently active CLI context.
Parameters:
-
data
(dict[str, Any]
) –The data to use for the active context.
Returns:
-
None
(None
) –
Source code in src/bijux_cli/core/context.py
use_context classmethod
¶
Temporarily replaces the current context data within a with
block.
Parameters:
-
data
(dict[str, Any]
) –The dictionary to use as the context for the duration of the
with
block.
Yields:
-
None
(None
) –