Skip to main content

Python tool for fixing triple-quoted string indentation

Project description

pytriple

A Python tool for automatically fixing triple-quoted string indentation while preserving relative indentation structure.

Features

  • 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

Installation

pip install pytriple

Usage

Fix a single file

pytriple fix-file example.py

Fix all files in a directory

pytriple fix-directory /path/to/project

Check a file without modifying

pytriple check example.py

Command Options

fix-file

  • --no-backup: Skip creating backup files
  • --dry-run: Preview changes without modifying files
  • --verbose, -v: Show detailed information about changes

fix-directory

  • --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

Architecture

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

Example

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
        """

How It Works

pytriple analyzes Python files using AST (Abstract Syntax Tree) parsing to find triple-quoted strings. It then:

  1. Identifies strings where the base indentation doesn't match Python conventions
  2. Calculates the minimum indentation level of content lines
  3. Adjusts only the base indentation to match the code context (parent indentation + 4 spaces)
  4. Preserves all relative indentation within the string content
  5. 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

Development

Setup

# 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 .

Running Tests

python -m pytest tests/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Martin Kalema

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytriple-1.0.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytriple-1.0.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file pytriple-1.0.0.tar.gz.

File metadata

  • Download URL: pytriple-1.0.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for pytriple-1.0.0.tar.gz
Algorithm Hash digest
SHA256 98bc678e4da9fdbd3fc61f83c1f534ce77f562ce983eb8492dab07c4ae340c79
MD5 7eb3af83656614978ed1161cc371524f
BLAKE2b-256 9b9af320e5e2f1fae3020aa2fdeb4230dd9c7bfcc9aec53c4c0a11e1f88ef4cd

See more details on using hashes here.

File details

Details for the file pytriple-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytriple-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for pytriple-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc0950aa4ce4c189d004a8dc15ec9af5aba583599f1b26618df99bf59c9a6049
MD5 627d7360ff9fb7fa8a5c189f13436e34
BLAKE2b-256 d0311c639b62190b8eeefcf36df3c8a7186ec7ec541bf850efd3b3218d53e845

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page