Metadata-Version: 2.4
Name: difflib-parser
Version: 1.2.3
Summary: Lightweight Python package for parsing Python difflib's diff results
Project-URL: Homepage, https://github.com/git-mastery/difflib-parser.git
Project-URL: Repository, https://github.com/git-mastery/difflib-parser.git
Project-URL: Issues, https://github.com/git-mastery/difflib-parser/issues
Author-email: "Jiahao, Woo" <woojiahao1234@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Jiahao
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# difflib-parser

Parser for Python's `difflib` output.

Built on top of <https://github.com/yebrahim/difflibparser/blob/master/difflibparser.py>

Key changes from above library:

1. Using generator pattern instead of using iterator pattern when iterating over diffs
2. Using `@dataclass` over generic dictionaries to enforce strict typing
3. Using type annotations for strict typing

## Getting started

```sh
pip install difflib-parser
```

```py
from difflib_parser import difflib_parser

parser = difflib_parser.DiffParser(["hello world"], ["hello world!"])
for diff in parser.iter_diffs():
  print(diff)
```

### `Diff` structure

```py
class DiffCode(Enum):
    SAME = 0
    RIGHT_ONLY = 1
    LEFT_ONLY = 2
    CHANGED = 3


@dataclass
class Diff:
    code: DiffCode
    line: str
    left_changes: List[int] | None = None
    right_changes: List[int] | None = None
    newline: str | None = None
```

## What is `difflib`?

A `difflib` output might look something like this:

```python
>>> import difflib
>>> print("\n".join(list(difflib.ndiff(["hello world"], ["hola world"]))))
- hello world
?  ^ ^^

+ hola world
?  ^ ^
```

The specifics of diff interpretation can be found in the [documentation](https://docs.python.org/3/library/difflib.html).

## Parsing `difflib`

There are concretely four types of changes we are interested in:

1. No change
2. A new line is added
3. An existing line is removed
4. An existing line is edited

Given that the last two cases operate on existing lines, they will always be preceded by `- `. As such, we need to handle them delicately.

If an existing line is removed, it will not have any follow-up lines.

If an existing line is edited, it will have several follow-up lines that provide details on the values that have been changed.

From these follow-up lines, we can further case the changes made to a line:

1. Only additions made (i.e. `"Hello world"` -> `"Hello world!"`)
2. Only removals made (i.e. `"Hello world"` -> `"Hllo world"`)
3. Both additions and removals made (i.e. `"Hello world"` -> `"Hola world!"`)

Each of them have their unique follow-up lines:

1. `-`, `+`, `?`

```python
>>> print("\n".join(list(difflib.ndiff(["hello world"], ["hello world!"]))))
- hello world
+ hello world!
?            +
```

2. `-`, `?`, `+`

```python
>>> print("\n".join(list(difflib.ndiff(["hello world"], ["hllo world"]))))
- hello world
?  -

+ hllo world
```

3. `-`, `?`, `+`, `?`

```python
>>> print("\n".join(list(difflib.ndiff(["hello world"], ["helo world!"]))))
- hello world
?    -

+ helo world!
?           +
```

As such, we have included them as separate patterns to process.
