26 lines
746 B
Python
26 lines
746 B
Python
"""Helpers for assigning stable, collision-free output filenames."""
|
|
|
|
import os
|
|
from typing import Dict, Optional
|
|
|
|
|
|
def resolve_name(name: str, content: bytes, seen: Dict[str, bytes]) -> Optional[str]:
|
|
"""Return a unique name, or None when the same name and content were seen.
|
|
|
|
Name collisions with different content receive numeric suffixes before
|
|
the extension, while exact duplicates are omitted.
|
|
"""
|
|
if name not in seen:
|
|
seen[name] = content
|
|
return name
|
|
if seen[name] == content:
|
|
return None
|
|
|
|
base, ext = os.path.splitext(name)
|
|
index = 2
|
|
while f"{base}_{index}{ext}" in seen:
|
|
index += 1
|
|
resolved = f"{base}_{index}{ext}"
|
|
seen[resolved] = content
|
|
return resolved
|