Spaces:
Running
Running
File size: 2,475 Bytes
58de40c bc5091e 58de40c bc5091e 58de40c 605e635 58de40c 605e635 5a007ca 58de40c d4b2b49 58de40c d1ed6b1 e9bcee8 d1ed6b1 58de40c d1ed6b1 e9bcee8 49be7fc d1ed6b1 0e508c8 605e635 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
"""
config.py
Global configuration and logger setup for the project.
Key Features:
- Loads environment variables
- Configures the logger for consistent logging across all modules.
- Dynamically sets the logging level based on the DEBUG environment variable.
"""
# Standard Library Imports
import atexit
import logging
import os
import shutil
# Third-Party Library Imports
from dotenv import load_dotenv
# Load environment variables
load_dotenv(override=True)
# Enable debugging mode based on an environment variable
debug_raw = os.getenv("DEBUG", "false").lower()
if debug_raw not in {"true", "false"}:
print(f'Warning: Invalid DEBUG value "{debug_raw}". Defaulting to "false".')
DEBUG = debug_raw == "true"
# Configure the logger
logging.basicConfig(
level=logging.DEBUG if DEBUG else logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger: logging.Logger = logging.getLogger("tts_arena")
logger.info(f'Debug mode is {"enabled" if DEBUG else "disabled"}.')
if DEBUG:
logger.debug(f"DEBUG mode enabled.")
# Define the directory for audio files relative to the project root
AUDIO_DIR = os.path.join(os.getcwd(), "static", "audio")
os.makedirs(AUDIO_DIR, exist_ok=True)
logger.info(f"Audio directory set to {AUDIO_DIR}")
def cleanup_audio_directory_contents() -> None:
"""
Delete all audio files within AUDIO_DIR, leaving the directory intact.
This function is intended to be registered to run when the application exits.
It assumes that AUDIO_DIR contains only audio files (or symbolic links), and no subdirectories.
"""
if not os.path.exists(AUDIO_DIR):
logger.info(
"Audio directory %s does not exist. Nothing to clean up.", AUDIO_DIR
)
return
# Use os.scandir for efficient directory iteration.
with os.scandir(AUDIO_DIR) as entries:
for entry in entries:
if entry.is_file() or entry.is_symlink():
try:
os.unlink(entry.path)
logger.info("Deleted file: %s", entry.path)
except Exception as exc:
logger.error(
"Failed to delete file %s. Reason: %s", entry.path, exc
)
else:
logger.warning("Skipping non-file entry: %s", entry.path)
# Register the cleanup function to be called on normal program termination.
atexit.register(cleanup_audio_directory_contents)
|