paths.py 944 B

1234567891011121314151617181920212223242526
  1. """Shared path resolution helpers.
  2. Centralises the DATA_DIR fallback used by ``auth.py`` (``.jwt_secret``) and
  3. ``encryption.py`` (``.mfa_encryption_key``) so both modules read the
  4. environment variable fresh on every call. Reading fresh — instead of caching
  5. the value at module import — is required so test fixtures can override
  6. ``DATA_DIR`` per-test via ``monkeypatch.setenv`` and have the override take
  7. effect immediately.
  8. """
  9. from __future__ import annotations
  10. import os
  11. from pathlib import Path
  12. def resolve_data_dir() -> Path:
  13. """Return the data directory, reading ``DATA_DIR`` fresh from env on each call.
  14. Falls back to ``<project_root>/data`` when ``DATA_DIR`` is not set, matching
  15. the behaviour of ``backend/app/core/auth.py:_get_jwt_secret``.
  16. """
  17. data_dir_env = os.environ.get("DATA_DIR")
  18. if data_dir_env:
  19. return Path(data_dir_env)
  20. return Path(__file__).parent.parent.parent.parent / "data"