The Shell class is an abstract base class (ABC) that provides a framework for executing shell commands
with consistent logging, context management, and error handling. It implements the Template Method pattern for
command execution and allows different shell implementations to define their own command formatting strategies.
ShellABC (Abstract Base Class)| Parameter | Type | Description | Default |
|---|---|---|---|
context |
Optional[SessionContext] | Session context containing working directory and environment variables | SessionContext() |
logger |
Optional[Logger] | Logger instance for recording operations | NullLogger() |
default_timeout |
float | Default timeout for command execution in seconds | 30.0 |
| Attribute | Type | Description |
|---|---|---|
context |
SessionContext | The session context for command execution |
logger |
Logger | Logger instance for recording operations |
default_timeout |
float | Default timeout value for command execution |
_format_command(executable: str, args: list[str]) -> list[str]
Abstract method that must be implemented by child classes to define how the final command list should be formatted (Strategy pattern).
executable (str): The executable/command to runargs (list[str]): List of command argumentslist[str]: Formatted command list ready for execution__enter__() -> ShellContext manager entry method. Logs the start of a shell session.
Shell: The shell instance__exit__(exc_type, exc_val, exc_tb)Context manager exit method. Logs session termination, including any errors that occurred.
exc_type: Exception type if an error occurredexc_val: Exception value if an error occurredexc_tb: Exception traceback if an error occurredNone)
run(command, timeout: Optional[float] = None) -> CommandResultTemplate method that coordinates the command execution process.
command: Command object containing executable and argumentstimeout (Optional[float]): Custom timeout in seconds. If not provided, uses default_timeoutCommandResult: Object containing command execution results_format_command() to prepare the final command listsubprocess.run() with:
CommandResult objectCommandNotFoundError: Raised when the executable is not found in PATHsubprocess.TimeoutExpired: Raised when command execution exceeds timeout
# Create a concrete shell implementation
class BashShell(Shell):
def _format_command(self, executable: str, args: list[str]) -> list[str]:
return [executable] + args
# Use the shell
with BashShell(logger=my_logger, default_timeout=60.0) as shell:
result = shell.run(my_command, timeout=30.0)
if result.is_success():
print(f"Output: {result.standard_output}")
Provides a common interface for different shell implementations
The run() method defines the skeleton of the command execution algorithm
The _format_command() abstract method allows different shell implementations to define their own formatting strategies
subprocess: For command executionshutil: For binary path checkingtime: For execution time measurementabc: For abstract base class functionality