mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-06-04 00:16:55 -05:00
BREAKING CHANGE: Major architectural refactoring of CLI system
New Professional Architecture:
- Clean separation of concerns with proper package structure
- Command pattern implementation with base classes
- Centralized configuration management
- Proper exception hierarchy and error handling
- Logging framework integration
- Type hints throughout
- Dependency injection pattern
Structure:
tinytorch/cli/
├── __init__.py # Package initialization
├── main.py # Professional CLI entry point
├── core/ # Core CLI functionality
│ ├── __init__.py
│ ├── config.py # Configuration management
│ ├── console.py # Centralized console output
│ └── exceptions.py # Exception hierarchy
├── commands/ # Command implementations
│ ├── __init__.py
│ ├── base.py # Base command class
│ └── notebooks.py # Notebooks command
└── tools/ # CLI tools
├── __init__.py
└── py_to_notebook.py # Conversion tool
Features Added:
- Proper entry points in pyproject.toml
- Professional logging with file output
- Environment validation with detailed error messages
- Dry-run mode for notebooks command
- Force rebuild option
- Timeout protection for subprocess calls
- Backward compatibility wrapper (bin/tito)
- Extensible command registration system
Benefits:
- Maintainable: Single responsibility per module
- Testable: Clean interfaces and dependency injection
- Extensible: Easy to add new commands
- Professional: Industry-standard patterns
- Robust: Proper error handling and validation
- Installable: Proper package structure with entry points
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
"""
|
|
Base command class for TinyTorch CLI.
|
|
"""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from argparse import ArgumentParser, Namespace
|
|
from typing import Optional
|
|
import logging
|
|
|
|
from ..core.config import CLIConfig
|
|
from ..core.console import get_console
|
|
from ..core.exceptions import TinyTorchCLIError
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class BaseCommand(ABC):
|
|
"""Base class for all CLI commands."""
|
|
|
|
def __init__(self, config: CLIConfig):
|
|
"""Initialize the command with configuration."""
|
|
self.config = config
|
|
self.console = get_console()
|
|
|
|
@property
|
|
@abstractmethod
|
|
def name(self) -> str:
|
|
"""Return the command name."""
|
|
pass
|
|
|
|
@property
|
|
@abstractmethod
|
|
def description(self) -> str:
|
|
"""Return the command description."""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
|
"""Add command-specific arguments to the parser."""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def run(self, args: Namespace) -> int:
|
|
"""Execute the command and return exit code."""
|
|
pass
|
|
|
|
def validate_args(self, args: Namespace) -> None:
|
|
"""Validate command arguments. Override in subclasses if needed."""
|
|
pass
|
|
|
|
def execute(self, args: Namespace) -> int:
|
|
"""Execute the command with error handling."""
|
|
try:
|
|
self.validate_args(args)
|
|
return self.run(args)
|
|
except TinyTorchCLIError as e:
|
|
logger.error(f"Command failed: {e}")
|
|
self.console.print(f"[red]❌ {e}[/red]")
|
|
return 1
|
|
except Exception as e:
|
|
logger.exception(f"Unexpected error in command {self.name}")
|
|
self.console.print(f"[red]❌ Unexpected error: {e}[/red]")
|
|
return 1 |