Files
personal_development/ServerManagerWebApp/backend/app/config.py
T
2026-01-09 19:24:41 +00:00

81 lines
2.6 KiB
Python

from pydantic_settings import BaseSettings, SettingsConfigDict
import subprocess
import logging
import shutil
import json
import os
logger = logging.getLogger(__name__)
# Configure logging early
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler() # Log to console
]
)
SETTINGS_FILE = "resources/config/app_settings.json"
def check_gpu_support():
try:
logger.debug("Checking ffmpeg for CUDA support...")
result = subprocess.run("ffmpeg -hwaccels", shell=True, capture_output=True, text=True, check=True)
if "cuda" in result.stdout:
logger.info("ffmpeg with CUDA support detected. Enabling GPU acceleration.")
return True
except (FileNotFoundError, subprocess.CalledProcessError) as e:
logger.debug(f"GPU detection failed: {e}")
pass
logger.info("No ffmpeg with CUDA support detected. Falling back to CPU-based processing.")
return False
class Settings(BaseSettings):
samba_server_ip: str = "127.0.0.1"
samba_username: str = "guest"
samba_password: str = ""
comicvine_api_key: str | None = None
qbittorrent_url: str | None = None
qbittorrent_username: str | None = None
qbittorrent_password: str | None = None
gpu_enabled: bool = check_gpu_support()
# Defaults
default_comics_path: str = "/comics/manga"
default_videos_path: str = "/videos"
# Stash Integration
stash_enabled: bool = False
stash_db_path: str = "stash.sqlite"
stash_remote_db_path: str = "/appdata/stashapp/config/stash-go.sqlite"
stash_generated_path: str = "/media/stashapp/generated"
stash_remote_base: str = "/media/videos"
stash_container_base: str = "/data"
stash_share: str = "main"
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
def load_from_json(self):
if os.path.exists(SETTINGS_FILE):
try:
with open(SETTINGS_FILE, 'r') as f:
data = json.load(f)
for key, value in data.items():
if hasattr(self, key):
setattr(self, key, value)
except Exception as e:
logger.error(f"Failed to load settings: {e}")
def save_to_json(self):
try:
os.makedirs(os.path.dirname(SETTINGS_FILE), exist_ok=True)
with open(SETTINGS_FILE, 'w') as f:
json.dump(self.model_dump(), f, indent=4)
except Exception as e:
logger.error(f"Failed to save settings: {e}")
settings = Settings()
settings.load_from_json()