A Python tool for automatically fixing triple-quoted string indentation while preserving relative indentation structure.
- Automatic Detection: Uses AST parsing to accurately detect multiline strings
- Preserves Relative Indentation: Maintains the relative indentation structure within strings, ensuring that the hierarchical structure of SQL, JSON, HTML, YAML, etc. is preserved exactly as the developer intended
- Clean Architecture: Built with Domain-Driven Design principles
- Backup Support: Creates backups before modifying files (default behavior)
- CLI Interface: User-friendly command line interface
- Comprehensive: Handles all triple-quoted strings in assignments, returns, and function calls
pip install pytriplepytriple fix-file example.pypytriple fix-directory /path/to/projectpytriple check example.py--no-backup: Skip creating backup files--dry-run: Preview changes without modifying files--verbose,-v: Show detailed information about changes
--no-backup: Skip creating backup files--dry-run: Preview changes without modifying files--exclude: Exclude files matching pattern (can be used multiple times)--verbose,-v: Show detailed output for each file
The project follows clean architecture principles:
- Domain Layer: Core business entities and rules
- Application Layer: Use cases for fixing files and directories
- Infrastructure Layer: File system operations and AST parsing
- Presentation Layer: CLI interface
Before:
class DatabaseManager:
def __init__(self):
self.query = """
SELECT u.id,
u.username,
u.email
FROM users u
WHERE u.active = 1
"""After:
class DatabaseManager:
def __init__(self):
self.query = """
SELECT u.id,
u.username,
u.email
FROM users u
WHERE u.active = 1
"""pytriple analyzes Python files using AST (Abstract Syntax Tree) parsing to find triple-quoted strings. It then:
- Identifies strings where the base indentation doesn't match Python conventions
- Calculates the minimum indentation level of content lines
- Adjusts only the base indentation to match the code context (parent indentation + 4 spaces)
- Preserves all relative indentation within the string content
- Writes the corrected content back to the file
This approach ensures that:
- Python code follows consistent indentation practices
- The internal structure of SQL queries, JSON data, HTML templates, YAML configs, etc. remains exactly as intended
- Complex hierarchical content maintains its readability and structure
# Clone the repository
git clone https://github.com/MartinKalema/pytriple.git
cd pytriple
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e .python -m pytest tests/Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Martin Kalema