Files
video-render/video_render/utils.py
2025-10-20 17:56:36 -03:00

39 lines
1.1 KiB
Python

from __future__ import annotations
import re
import unicodedata
from pathlib import Path
from typing import Iterable
def sanitize_filename(name: str) -> str:
normalized = unicodedata.normalize("NFKD", name)
ascii_text = normalized.encode("ASCII", "ignore").decode()
ascii_text = ascii_text.lower()
ascii_text = ascii_text.replace(" ", "_")
ascii_text = re.sub(r"[^a-z0-9_\-\.]", "", ascii_text)
ascii_text = re.sub(r"_+", "_", ascii_text)
return ascii_text.strip("_") or "video"
def ensure_workspace(root: Path, folder_name: str) -> Path:
workspace = root / folder_name
workspace.mkdir(parents=True, exist_ok=True)
return workspace
def remove_paths(paths: Iterable[Path]) -> None:
for path in paths:
if not path.exists():
continue
if path.is_file() or path.is_symlink():
path.unlink(missing_ok=True)
else:
for child in sorted(path.rglob("*"), reverse=True):
if child.is_file() or child.is_symlink():
child.unlink(missing_ok=True)
elif child.is_dir():
child.rmdir()
path.rmdir()