44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Plugin-Adapter fuer das Trainingsplan-Backend."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from a2wsgi import WSGIMiddleware
|
|
|
|
from core.loader import load_module
|
|
|
|
|
|
def _has_plans(path: Path) -> bool:
|
|
return any((path / "plans").glob("*.json"))
|
|
|
|
|
|
def _resolve_data_dir(default: Path) -> Path:
|
|
for candidate in (
|
|
Path("/home/michael/boehmitools/runtime/training/data/trainingsplan"),
|
|
Path("/home/michael/runtime/training/data/trainingsplan"),
|
|
):
|
|
if _has_plans(candidate):
|
|
return candidate.resolve()
|
|
if _has_plans(default):
|
|
return default
|
|
return default
|
|
|
|
|
|
def create_app(ctx):
|
|
# app.py liest DATA_DIR beim Import und legt dort den ersten Plan an.
|
|
previous = os.environ.get("DATA_DIR")
|
|
os.environ["DATA_DIR"] = str(_resolve_data_dir(ctx.data_dir))
|
|
try:
|
|
module = load_module(ctx.path("app.py"), f"btp_{ctx.id}_app")
|
|
finally:
|
|
if previous is None:
|
|
os.environ.pop("DATA_DIR", None)
|
|
else:
|
|
os.environ["DATA_DIR"] = previous
|
|
|
|
# Flask erzeugt seine URLs anhand von SCRIPT_NAME; a2wsgi uebernimmt dafuer
|
|
# den root_path des Mounts.
|
|
return WSGIMiddleware(module.app)
|