Initial commit

This commit is contained in:
2026-11-16 16:29:32 +01:00
commit 97e5c9ef4d
37 changed files with 5932 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
"""Formatierungshelfer fuer Zeitangaben, Groessen und Pfade."""
from __future__ import annotations
from datetime import datetime, timezone
from pathlib import Path
def format_timestamp(seconds: float | int | None) -> str:
"""Formatiert Sekunden als HH:MM:SS bzw. MM:SS."""
if seconds is None:
return "--:--"
seconds = max(0, int(round(float(seconds))))
hours, remainder = divmod(seconds, 3600)
minutes, secs = divmod(remainder, 60)
if hours:
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
return f"{minutes:02d}:{secs:02d}"
def format_timestamp_verbose(seconds: float | int | None) -> str:
"""Formatiert Sekunden immer als HH:MM:SS (fuer Fundstellen in Hoerspielen)."""
if seconds is None:
return "--:--:--"
seconds = max(0, int(round(float(seconds))))
hours, remainder = divmod(seconds, 3600)
minutes, secs = divmod(remainder, 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def format_duration(seconds: float | int | None) -> str:
"""Menschlich lesbare Dauer, z. B. '2 h 14 min'."""
if seconds is None:
return "unbekannt"
total = max(0, int(round(float(seconds))))
hours, remainder = divmod(total, 3600)
minutes, secs = divmod(remainder, 60)
if hours:
return f"{hours} h {minutes:02d} min"
if minutes:
return f"{minutes} min {secs:02d} s"
return f"{secs} s"
def format_size(num_bytes: int | float | None) -> str:
"""Formatiert Bytes als B/KB/MB/GB."""
if num_bytes is None:
return "?"
value = float(num_bytes)
for unit in ("B", "KB", "MB", "GB", "TB"):
if value < 1024 or unit == "TB":
if unit == "B":
return f"{int(value)} {unit}"
return f"{value:.1f} {unit}"
value /= 1024
return f"{value:.1f} TB"
def format_score(score: float) -> str:
"""Aehnlichkeitswert als Prozentangabe (keine statistische Wahrscheinlichkeit)."""
return f"{score:.1f}%"
def format_rate(audio_seconds: float, elapsed_seconds: float) -> str:
"""Verarbeitungsgeschwindigkeit als Vielfaches der Echtzeit."""
if elapsed_seconds <= 0:
return "-"
return f"{audio_seconds / elapsed_seconds:.2f}x Echtzeit"
def format_datetime(iso_timestamp: str | None) -> str:
"""Formatiert einen ISO-String lesbar; unbekannte Formate werden durchgereicht."""
if not iso_timestamp:
return "-"
try:
dt = datetime.fromisoformat(iso_timestamp)
except ValueError:
return iso_timestamp
if dt.tzinfo is not None:
dt = dt.astimezone()
return dt.strftime("%Y-%m-%d %H:%M")
def utc_now_iso() -> str:
"""Aktueller Zeitpunkt als ISO-String in UTC (sekundengenau)."""
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
def shorten_path(path: str | Path, max_length: int = 80) -> str:
"""Kuerzt lange Pfade in der Mitte."""
text = str(path)
if len(text) <= max_length:
return text
keep = max(8, (max_length - 3) // 2)
return f"{text[:keep]}...{text[-keep:]}"