|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Python-snap7 is a Python wrapper for the Snap7 library, providing Ethernet communication with Siemens S7 PLCs. The library supports Python 3.9+ and runs on Windows, Linux, and macOS. |
| 8 | + |
| 9 | +## Key Architecture |
| 10 | + |
| 11 | +- **snap7/client.py**: Main Client class for connecting to S7 PLCs |
| 12 | +- **snap7/server.py**: Server implementation for PLC simulation |
| 13 | +- **snap7/logo.py**: Logo PLC communication |
| 14 | +- **snap7/partner.py**: Partner connection functionality |
| 15 | +- **snap7/util/**: Utility functions for data conversion (getters.py, setters.py, db.py) |
| 16 | +- **snap7/protocol.py**: Low-level protocol bindings to the native Snap7 library |
| 17 | +- **snap7/type.py**: Type definitions and enums (Area, Block, WordLen, etc.) |
| 18 | +- **snap7/common.py**: Common utilities including library loading |
| 19 | +- **snap7/error.py**: Error handling and exceptions |
| 20 | + |
| 21 | +The library uses ctypes to interface with the native Snap7 C library (libsnap7.so/snap7.dll/libsnap7.dylib). |
| 22 | + |
| 23 | +## Essential Commands |
| 24 | + |
| 25 | +### Testing |
| 26 | +```bash |
| 27 | +# Run all tests |
| 28 | +pytest |
| 29 | + |
| 30 | +# Run specific test categories using markers |
| 31 | +pytest -m "server or util or client or mainloop" |
| 32 | + |
| 33 | +# Run tests for specific component |
| 34 | +pytest tests/test_client.py |
| 35 | +``` |
| 36 | + |
| 37 | +### Code Quality |
| 38 | +```bash |
| 39 | +# Type checking |
| 40 | +mypy snap7 tests example |
| 41 | + |
| 42 | +# Linting and formatting check |
| 43 | +ruff check snap7 tests example |
| 44 | +ruff format --diff snap7 tests example |
| 45 | + |
| 46 | +# Auto-format code |
| 47 | +ruff format snap7 tests example |
| 48 | +ruff check --fix snap7 tests example |
| 49 | +``` |
| 50 | + |
| 51 | +### Development with tox |
| 52 | +```bash |
| 53 | +# Run all tox environments (mypy, lint, py39-py313) |
| 54 | +tox |
| 55 | + |
| 56 | +# Run specific environments |
| 57 | +tox -e mypy |
| 58 | +tox -e lint-ruff |
| 59 | +tox -e py39 |
| 60 | +``` |
| 61 | + |
| 62 | +### Using Makefile |
| 63 | +```bash |
| 64 | +# Setup development environment |
| 65 | +make setup |
| 66 | + |
| 67 | +# Run tests |
| 68 | +make test |
| 69 | + |
| 70 | +# Type checking |
| 71 | +make mypy |
| 72 | + |
| 73 | +# Linting |
| 74 | +make check |
| 75 | + |
| 76 | +# Format code |
| 77 | +make format |
| 78 | +``` |
| 79 | + |
| 80 | +### Building and Installation |
| 81 | +```bash |
| 82 | +# Install in development mode |
| 83 | +pip install -e . |
| 84 | + |
| 85 | +# Install with test dependencies |
| 86 | +pip install -e ".[test]" |
| 87 | + |
| 88 | +# Install with CLI tools |
| 89 | +pip install -e ".[cli]" |
| 90 | +``` |
| 91 | + |
| 92 | +## Test Markers |
| 93 | + |
| 94 | +Tests are organized with pytest markers: |
| 95 | +- `client`: Client functionality tests |
| 96 | +- `server`: Server functionality tests |
| 97 | +- `util`: Utility function tests |
| 98 | +- `logo`: Logo PLC tests |
| 99 | +- `partner`: Partner connection tests |
| 100 | +- `mainloop`: Main loop tests |
| 101 | +- `common`: Common functionality tests |
| 102 | + |
| 103 | +## Python Version Compatibility |
| 104 | + |
| 105 | +**Fully tested and supported Python versions:** |
| 106 | +- **Python 3.9** (EOL: October 2025) ✅ |
| 107 | +- **Python 3.10** (EOL: October 2026) ✅ |
| 108 | +- **Python 3.11** (EOL: October 2027) ✅ |
| 109 | +- **Python 3.12** (EOL: October 2028) ✅ |
| 110 | +- **Python 3.13** (EOL: October 2029) ✅ |
| 111 | + |
| 112 | +All versions pass the complete test suite (188 tests) and have been verified for type checking, linting, and functionality. |
| 113 | + |
| 114 | +## Cross-Platform Development |
| 115 | + |
| 116 | +This library supports **Windows, Linux, and macOS** through ctypes bindings: |
| 117 | + |
| 118 | +- **Windows**: Uses `windll` from ctypes for library loading |
| 119 | +- **Linux/macOS**: Uses `cdll` from ctypes for library loading |
| 120 | +- **Library files**: Includes platform-specific Snap7 libraries (snap7.dll, libsnap7.so, libsnap7.dylib) |
| 121 | + |
| 122 | +### Platform-Specific Notes |
| 123 | +- The conditional import in `snap7/common.py` handles platform differences automatically |
| 124 | +- No `# type: ignore` comments needed for platform-specific imports in modern mypy |
| 125 | +- Cross-platform compatibility is maintained through the ctypes interface |
| 126 | + |
| 127 | +## Code Quality Standards |
| 128 | + |
| 129 | +### Expected Quality Tool Results |
| 130 | +```bash |
| 131 | +# MyPy should show clean results |
| 132 | +mypy snap7 tests example |
| 133 | +# Expected: "Success: no issues found in 27 source files" |
| 134 | + |
| 135 | +# Ruff should pass all checks |
| 136 | +ruff check snap7 tests example |
| 137 | +# Expected: "All checks passed!" |
| 138 | + |
| 139 | +# Tests should pass with high coverage |
| 140 | +pytest tests/ |
| 141 | +# Expected: "188 passed, 4 skipped" |
| 142 | +``` |
| 143 | + |
| 144 | +### Common Code Quality Issues and Fixes |
| 145 | + |
| 146 | +1. **Print statements in production code**: Replace with `logger.info()` or appropriate log level |
| 147 | +2. **Unused type ignore comments**: Remove if not needed, or make platform-specific |
| 148 | +3. **Formatting inconsistencies**: Run `ruff format` to auto-fix |
| 149 | +4. **Type annotation issues**: Use strict mypy checking to catch early |
| 150 | + |
| 151 | +### Development Best Practices |
| 152 | + |
| 153 | +- **Always use logging instead of print()** for debug output (except CLI error messages) |
| 154 | +- **Test across Python versions** when making changes that might affect compatibility |
| 155 | +- **Use existing patterns** for ctypes interactions and error handling |
| 156 | +- **Follow the established project structure** when adding new functionality |
| 157 | +- **Maintain cross-platform compatibility** - test platform-specific code paths |
| 158 | + |
| 159 | +## Configuration Notes |
| 160 | + |
| 161 | +- **pyproject.toml**: Main project configuration with build, dependencies, and tool settings |
| 162 | +- **tox.ini**: Multi-environment testing configuration |
| 163 | +- **.pre-commit-config.yaml**: Pre-commit hooks for code quality |
| 164 | +- **Ruff**: Line length set to 130, targets Python 3.9+ |
| 165 | +- **MyPy**: Strict mode enabled with specific error code exceptions |
| 166 | +- **Protocol exclusion**: snap7/protocol.py is excluded from some linting due to generated bindings |
| 167 | + |
| 168 | +## Library Architecture Notes |
| 169 | + |
| 170 | +### Key Design Patterns |
| 171 | +- **Error wrapping**: Uses `@error_wrap` decorator for consistent error handling |
| 172 | +- **Type safety**: Extensive use of ctypes with proper type annotations |
| 173 | +- **Platform abstraction**: Single codebase works across Windows/Linux/macOS |
| 174 | +- **Modular design**: Clear separation between client, server, utilities, and protocol layers |
| 175 | + |
| 176 | +### Common Development Tasks |
| 177 | +- **Adding new PLC operations**: Extend client.py with proper error handling and logging |
| 178 | +- **Utility functions**: Add to appropriate modules in snap7/util/ following existing patterns |
| 179 | +- **Type definitions**: Update snap7/type.py for new enums or structures |
| 180 | +- **Cross-platform testing**: Use tox environments or manual virtual environment testing |
0 commit comments