/
domahes
/
audio_visualizer_improved
Обзор
Документация
Войти
/
domahes
/
audio_visualizer_improved
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
availability.py
107 строк
3 KB
Евгений Харченко
Refactor visualizer: split audio_visualizer_improved.py into modular helper files (overlays, position, palette, models, utils) and add .gitignore
02 июн 2026, 09:36
02 июн 2026, 09:36
dad7485
Код
Авторство
О чём код?
""" Availability flags, session temp dir, and base exceptions. Extracted in Phase 1 of the refactor. Kept minimal and side-effect free where possible. """ from __future__ import annotations import os import sys import tempfile import shutil import atexit from typing import Any, Dict, Tuple # --- Third-party optional detection (must not crash the module) --- try: # numpy is hard requirement import numpy as np # noqa: F401 # re-exported for convenience NUMPY_AVAILABLE = True except Exception as e: # pragma: no cover np = None # type: ignore NUMPY_AVAILABLE = False _numpy_error = e try: import cv2 # for drawing and VideoWriter CV2_AVAILABLE = True except Exception: cv2 = None # type: ignore CV2_AVAILABLE = False try: import librosa # audio analysis LIBROSA_AVAILABLE = True except Exception: librosa = None # type: ignore LIBROSA_AVAILABLE = False # Pillow for Unicode/Emoji text rendering (optional, falls back to cv2) try: from PIL import Image, ImageDraw, ImageFont PIL_AVAILABLE = True except Exception: Image = ImageDraw = ImageFont = None # type: ignore PIL_AVAILABLE = False # Optional metadata reader try: from mutagen import File as MutagenFile # type: ignore MUTAGEN_AVAILABLE = True except Exception: MutagenFile = None # type: ignore MUTAGEN_AVAILABLE = False # Tkinter is optional — we *must not* crash if it's missing try: import tkinter as tk from tkinter import filedialog, messagebox, ttk TK_AVAILABLE = True except Exception: tk = None # type: ignore filedialog = messagebox = ttk = None # type: ignore TK_AVAILABLE = False class RenderCancelled(Exception): """Raised when rendering is cancelled via stop_event.""" pass # Session-scoped temp directory (avoids collisions between concurrent runs) # Created once per python process that imports this module. _SESSION_TMPDIR = tempfile.mkdtemp(prefix="audioviz_") atexit.register(lambda: shutil.rmtree(_SESSION_TMPDIR, ignore_errors=True)) # Convenience re-exports (so other modules can do `from availability import cv2` safely) __all__ = [ "np", "cv2", "librosa", "Image", "ImageDraw", "ImageFont", "MutagenFile", "tk", "filedialog", "messagebox", "ttk", "NUMPY_AVAILABLE", "CV2_AVAILABLE", "LIBROSA_AVAILABLE", "PIL_AVAILABLE", "MUTAGEN_AVAILABLE", "TK_AVAILABLE", "RenderCancelled", "_SESSION_TMPDIR", ] def clamp(v: float, lo: float, hi: float) -> float: """Simple numeric clamp. Also re-exported from utils in later phases.""" return max(lo, min(hi, v)) # Fail fast for the truly required dependency if not NUMPY_AVAILABLE: raise RuntimeError("numpy is required: pip install numpy") from _numpy_error # type: ignore[name-defined]