"""Small shared helpers.""" from __future__ import annotations import logging import os from datetime import date, datetime, timezone from . import config def utcnow() -> datetime: return datetime.now(timezone.utc) def utcnow_iso() -> str: return utcnow().replace(microsecond=0).isoformat() def today() -> date: return utcnow().date() def apply_umask() -> None: """Ensure files land group-writable so Jellyfin's group can read them.""" os.umask(config.UMASK) def setup_logging(verbose: bool = False) -> None: logging.basicConfig( level=logging.DEBUG if verbose else logging.INFO, format="%(asctime)s %(levelname)-7s %(name)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) # yt-dlp and urllib are noisy at debug level and we drive them deliberately. logging.getLogger("urllib3").setLevel(logging.WARNING) def human_bytes(value: int | None) -> str: size = float(value or 0) for unit in ("B", "KB", "MB", "GB", "TB"): if size < 1024 or unit == "TB": return f"{size:.0f} {unit}" if unit == "B" else f"{size:.1f} {unit}" size /= 1024 return f"{size:.1f} TB"