Skip to content

Developer Guide - Command Processor Module¤

Development Environment Setup¤

Prerequisites¤

  • Python 3.8+
  • PANTHER framework dependencies
  • pytest for testing
  • pylint for code quality

Local Development Setup¤

# Navigate to PANTHER root
cd /path/to/PANTHER

# Install development dependencies
pip install -e .[dev]

# Verify installation
python -c "from panther.core.command_processor import CommandProcessor; print('OK')"

Project Structure¤

command_processor/
├── __init__.py              # Public API exports
├── builders/                # Command builder implementations
├── core/                    # Core interfaces and processor
├── mixins/                  # Reusable functionality mixins
├── models/                  # Data models and constants
├── utils/                   # Utility functions
├── README.md               # Architecture documentation
├── DEVELOPER_GUIDE.md      # This file
└── api_reference.md        # API documentation

Running Tests¤

Unit Tests¤

# Run all command processor tests
pytest panther/core/command_processor/tests/ -v

# Run specific test categories
pytest -k "test_command_processing" -v
pytest -k "test_shell_utils" -v
pytest -k "test_builders" -v

Integration Tests¤

# Test with actual shell commands
pytest panther/core/command_processor/tests/integration/ -v

# Test environment adapters
pytest -k "test_environment_adapter" -v

Test Coverage¤

# Generate coverage report
pytest --cov=panther.core.command_processor --cov-report=html

# View coverage report
open htmlcov/index.html

Code Quality¤

Linting¤

# Run pylint on the module
pylint panther/core/command_processor/

# Run with specific rcfile if available
pylint --rcfile=.pylintrc panther/core/command_processor/

Code Formatting¤

# Format with black (if configured)
black panther/core/command_processor/

# Check formatting
black --check panther/core/command_processor/

Type Checking¤

# Run mypy if configured
mypy panther/core/command_processor/

Debugging¤

Enable Debug Logging¤

import logging
logging.basicConfig(level=logging.DEBUG)

from panther.core.command_processor import CommandProcessor
processor = CommandProcessor()
# Debug logs will now show detailed processing info

Common Debug Scenarios¤

Command Processing Issues¤

# Enable detailed command logging
processor.logger.setLevel(logging.DEBUG)

# Process with debugging
result = processor.process_commands({
    "run_cmd": {"command_args": "echo test"}
})

Shell Command Detection¤

from panther.core.command_processor.models import ShellCommand

# Test command detection
cmd = ShellCommand.from_string("if [ -f file ]; then echo found; fi")
print(f"Multiline: {cmd.metadata.is_multiline}")
print(f"Control structure: {cmd.metadata.is_control_structure}")

Builder Pattern Debugging¤

from panther.core.command_processor.builders import CommandBuilder

# Step through builder process
builder = CommandBuilder()
builder.set_command("echo test")
builder.set_working_directory("/tmp")
command = builder.build()
print(command.to_dict())

Development Workflows¤

Adding New Features¤

  1. Design Phase
  2. Update interfaces if needed (core/interfaces.py)
  3. Consider impact on existing processors
  4. Design for extensibility

  5. Implementation

  6. Add core logic to appropriate module
  7. Implement tests alongside code
  8. Update builders if command structure changes

  9. Integration

  10. Update init.py exports
  11. Add integration tests
  12. Update documentation

  13. Validation

  14. Run full test suite
  15. Check lint compliance
  16. Verify no regressions

Bug Fixes¤

  1. Reproduce Issue

    # Create minimal reproduction
    from panther.core.command_processor import CommandProcessor
    processor = CommandProcessor()
    # Add failing case
    

  2. Add Test Case

    def test_bug_reproduction():
        # Test case that fails before fix
        # Should pass after fix
        pass
    

  3. Implement Fix

  4. Make minimal changes
  5. Ensure test passes
  6. Check for side effects

  7. Regression Testing

  8. Run full test suite
  9. Test edge cases
  10. Verify performance impact

Performance Optimization¤

Profiling Command Processing¤

import cProfile
from panther.core.command_processor import CommandProcessor

def profile_processing():
    processor = CommandProcessor()
    # Your test commands here
    commands = {"run_cmd": {"command_args": "complex command"}}
    processor.process_commands(commands)

cProfile.run('profile_processing()')

Memory Usage Analysis¤

import tracemalloc
tracemalloc.start()

# Your command processing code
processor = CommandProcessor()
result = processor.process_commands(large_command_set)

current, peak = tracemalloc.get_traced_memory()
print(f"Current memory usage: {current / 1024 / 1024:.2f} MB")
print(f"Peak memory usage: {peak / 1024 / 1024:.2f} MB")

Pull Request Workflow¤

Before Creating PR¤

  1. Code Quality Check

    # Run all quality checks
    pylint panther/core/command_processor/
    pytest panther/core/command_processor/tests/ -v
    black --check panther/core/command_processor/
    

  2. Documentation Update

  3. Update README.md if architecture changes
  4. Update this DEVELOPER_GUIDE.md if workflow changes
  5. Update api_reference.md if public API changes

  6. Integration Testing

    # Test with other PANTHER components
    pytest tests/integration/ -k command_processor
    

PR Description Template¤

## Summary
Brief description of changes

## Changes Made
- List specific changes
- Include any new features
- Note any breaking changes

## Testing
- [ ] Unit tests pass
- [ ] Integration tests pass
- [ ] Manual testing completed
- [ ] Performance impact assessed

## Documentation
- [ ] README.md updated if needed
- [ ] API reference updated if needed
- [ ] Code comments added for complex logic

Troubleshooting¤

Common Issues¤

Circular Import Errors¤

# Use lazy imports in models
def get_shell_utils():
    from panther.core.command_processor.utils.shell_utils import ...
    return ...

Memory Leaks in Command Processing¤

  • Check for proper cleanup in command lists
  • Ensure ShellCommand objects are garbage collected
  • Monitor command metadata growth

Performance Degradation¤

  • Profile command detection logic
  • Check for inefficient string operations
  • Monitor regex compilation overhead

Debug Environment Variables¤

# Enable detailed logging
export PANTHER_LOG_LEVEL=DEBUG

# Enable command processor specific logging
export PANTHER_COMMAND_PROCESSOR_DEBUG=1

# Disable command optimization for debugging
export PANTHER_DISABLE_COMMAND_OPTIMIZATION=1

Contributing Guidelines¤

Code Style¤

  • Follow PEP 8 conventions
  • Use type hints for all public methods
  • Add docstrings following Google style
  • Keep functions focused and small

Testing Requirements¤

  • Minimum 80% code coverage
  • Test edge cases and error conditions
  • Include integration tests for new features
  • Mock external dependencies

Documentation Standards¤

  • Update API documentation for public changes
  • Include examples in docstrings
  • Keep README.md current with architecture
  • Document performance implications