"""Package initialization for the alt text generator app.""" from pathlib import Path from flask import Flask from .cache import AltTextCache from .config import load_settings from .immich import ImmichClient from .openai_client import AltTextGenerator from .mastodon import MastodonClient def create_app() -> Flask: """Create and configure the Flask application.""" package_path = Path(__file__).resolve().parent project_root = package_path.parent app = Flask( __name__, template_folder=str(project_root / "templates"), static_folder=str(project_root / "static"), ) settings = load_settings() app.config.update(settings) secret_key = app.config.get("SECRET_KEY") or "dev-secret-key" app.config["SECRET_KEY"] = secret_key instance_dir = Path(app.instance_path) instance_dir.mkdir(parents=True, exist_ok=True) db_path = app.config.get("STATION_DB") if not db_path: old_db = instance_dir / "alt_text_cache.db" new_db = instance_dir / "station_announcer.db" if old_db.exists() and not new_db.exists(): old_db.rename(new_db) db_path = str(new_db) app.config["STATION_DB"] = db_path app.immich_client = ImmichClient( base_url=app.config["IMMICH_API_URL"], api_key=app.config["IMMICH_API_KEY"], ) app.alt_text_cache = AltTextCache(db_path) app.alt_text_generator = AltTextGenerator( api_key=app.config["OPENAI_API_KEY"], model=app.config.get("OPENAI_MODEL", "gpt-4o-mini"), ) mastodon_token = app.config.get("MASTODON_ACCESS_TOKEN") if mastodon_token: app.mastodon_client = MastodonClient( base_url=app.config.get("MASTODON_BASE_URL", "http://localhost:3000"), access_token=mastodon_token, ) else: app.mastodon_client = None from . import routes # pragma: no cover app.register_blueprint(routes.bp) return app