Metadata-Version: 2.4
Name: pyrepair
Version: 1.0.0
Summary: Repair broken Python code before formatting it.
Author: Sandeep Kumar Mehta
License: MIT
Keywords: python,linter,formatter,repair,indentation,syntax
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: typer>=0.12
Requires-Dist: rich>=13.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: pytest-cov>=5.0; extra == "dev"
Dynamic: license-file

# PyRepair

> **Repair broken Python code before formatting it.**

Tools like Black, Ruff, and autopep8 require *valid* Python. PyRepair sits upstream — it fixes indentation errors, tab/space mixes, and missing block bodies so those tools can run.

```
Broken Python → PyRepair → Valid Python → Black/Ruff → Clean Python
```

---

## Installation

```bash
pip install pyrepair
```

Or install from source:

```bash
git clone https://github.com/sandeep-kumar-mehta/pyrepair.git
cd pyrepair
pip install -e ".[dev]"
```

---

## Quick Start

```bash
# Repair a single file in-place
pyrepair broken.py

# Preview changes without writing (safe)
pyrepair broken.py --dry-run

# Repair all .py files in a directory
pyrepair src/

# Create a backup before writing
pyrepair broken.py --backup

# Show detailed issue table
pyrepair broken.py --report
```

---

## What Gets Fixed

| Rule | What It Does |
|---|---|
| `tabs_to_spaces` | Converts leading `\t` to 4 spaces |
| `mixed_indent` | Fixes lines that mix tabs and spaces |
| `indentation_repair` | Indents body of `if`/`for`/`while`/`def`/`class` blocks |
| `missing_block` | Inserts `pass` when a compound statement has no body |

---

## CLI Reference

```
pyrepair [TARGET] [OPTIONS]

Arguments:
  TARGET    Python file or directory to repair.

Options:
  --dry-run     Show diff only. Do NOT write changes.
  --backup      Save .bak copy before overwriting.
  --report, -r  Print detailed issue table for each file.
  --help        Show this message and exit.
```

---

## Python API

```python
from pathlib import Path
from pyrepair import RepairEngine

engine = RepairEngine()

# Repair a file (returns RepairResult — does NOT write to disk)
result = engine.repair_file(Path("broken.py"))

print(result.status)           # RepairStatus.SUCCESS
print(result.total_issues)     # 5
print(result.total_fixed)      # 5
print(result.validation_passed)# True
print(result.repaired_source)  # fixed source code string

# Repair a raw string
result = engine.repair_source("if True:\nprint('hi')\n")
```

---

## Adding Custom Rules

```python
from pyrepair import RepairEngine
from pyrepair.repair_engine import RepairRule
from pyrepair.models import Issue, Severity

class RemoveDebugPrintsRule(RepairRule):
    name = "remove_debug_prints"
    description = "Remove debug print statements."

    def detect(self, source: str) -> list[Issue]:
        issues = []
        for i, line in enumerate(source.splitlines(), 1):
            if 'print("DEBUG' in line:
                issues.append(Issue(
                    rule_name=self.name,
                    description="Debug print found.",
                    line_number=i,
                    severity=Severity.WARNING,
                    original=line,
                ))
        return issues

    def repair(self, source: str) -> str:
        lines = [l for l in source.splitlines() if 'print("DEBUG' not in l]
        return "\n".join(lines)

engine = RepairEngine()
engine.add_rule(RemoveDebugPrintsRule())
```

---

## Architecture

```
pyrepair/
├── cli.py            ← Typer CLI (no business logic)
├── scanner.py        ← File discovery
├── repair_engine.py  ← Rule pipeline + RepairEngine
├── models.py         ← Pure data classes (Issue, RepairResult)
├── report.py         ← Rich terminal output
└── utils.py          ← Shared helpers (read/write/validate)
```

Layers are kept separate (SOLID). The CLI only calls the engine and reporter.

---

## Roadmap

**v1.0**  Indentation repair, tab conversion, missing block, AST validation

**v2.0** Unused/duplicate import detection, import sorting, dead code detection

**v3.0** VS Code extension, HTML reports

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md).

---

## License

MIT — see [LICENSE](LICENSE).
