A pluggable Python library for creating semantic and line-by-line diffs for various file formats. Designed for use in backend systems working with file storages like S3/MinIO.
- Strategy Pattern: Choose the best diff algorithm based on file type (
.md,.json, etc.). - Extensible: Easily add new strategies for custom formats (e.g.,
.py,.yaml,.xml). - Storage Agnostic: Works with any async storage client that implements a simple protocol.
- Modern: Built with Python 3.10+, Pydantic, and async support in mind.
From source:
pip install .Or to install in editable mode for development:
pip install -e .[dev]The library is designed to be integrated into a service that has access to a file storage.
import asyncio
from diff_engine import StorageDiffProvider, IAsyncStorageClient
# 1. Create a storage client adapter that matches the protocol
# This is an example for a local file system. In a real app,
# this would be your MinIO or S3 client wrapper.
class MockStorageClient(IAsyncStorageClient):
async def get_object_as_text(self, path: str) -> str:
with open(path, 'r', encoding='utf-8') as f:
return f.read()
async def main():
# 2. Instantiate the provider with your storage client
storage_client = MockStorageClient()
diff_provider = StorageDiffProvider(storage_client)
# 3. Get a diff between two files
# The engine will automatically select the TextDiffStrategy for .md
md_patch = await diff_provider.get_diff("tests/fixtures/sample_v1.md", "tests/fixtures/sample_v2.md")
print("--- Markdown Diff ---")
print(f"Lines Added: {md_patch.added_lines}, Removed: {md_patch.removed_lines}")
print(md_patch.diff_text)
# The engine will automatically select the JsonDiffStrategy for .json
json_patch = await diff_provider.get_diff("tests/fixtures/sample_v1.json", "tests/fixtures/sample_v2.json")
print("\n--- JSON Diff ---")
print(json_patch.diff_text)
if __name__ == "__main__":
asyncio.run(main())To run tests:
pytestTo format code:
black .
isort .
(Файлы для tests/ и exceptions.py опущены для краткости, но их структура очевидна из контекста)