|
5 | 5 | Modules import from this file to ensure consistent behavior across the engine. |
6 | 6 | """ |
7 | 7 |
|
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
8 | 11 | from pathlib import Path |
9 | 12 |
|
10 | 13 | # --------------------------------------------------------------------------- |
|
14 | 17 | # Root directory of the app package |
15 | 18 | APP_DIR = Path(__file__).parent |
16 | 19 |
|
17 | | -# Persistent data storage directory |
18 | | -DATASTORE_DIR = APP_DIR / "datastore" |
| 20 | +def _writable_datastore_dir() -> Path: |
| 21 | + """ |
| 22 | + Resolve a writable datastore directory. |
| 23 | +
|
| 24 | + - Dev mode: using `engine/datastore/` is fine. |
| 25 | + - Installed mode (e.g. under Program Files): we must not write into the install dir. |
| 26 | +
|
| 27 | + Order: |
| 28 | + 1) SENTRACORE_DATA_DIR (explicit override) |
| 29 | + 2) Local package datastore if it can be created/written |
| 30 | + 3) %LOCALAPPDATA%/SentraCore/datastore (Windows) |
| 31 | + 4) ~/.local/share/SentraCore/datastore (fallback) |
| 32 | + """ |
| 33 | + override = os.environ.get("SENTRACORE_DATA_DIR") |
| 34 | + if override: |
| 35 | + return Path(override) |
| 36 | + |
| 37 | + local = APP_DIR / "datastore" |
| 38 | + try: |
| 39 | + local.mkdir(parents=True, exist_ok=True) |
| 40 | + test = local / ".write_test" |
| 41 | + test.write_text("ok", encoding="utf-8") |
| 42 | + test.unlink(missing_ok=True) |
| 43 | + return local |
| 44 | + except OSError: |
| 45 | + pass |
| 46 | + |
| 47 | + local_appdata = os.environ.get("LOCALAPPDATA") |
| 48 | + if local_appdata: |
| 49 | + return Path(local_appdata) / "SentraCore" / "datastore" |
| 50 | + |
| 51 | + return Path.home() / ".local" / "share" / "SentraCore" / "datastore" |
| 52 | + |
| 53 | + |
| 54 | +# Persistent data storage directory (writable) |
| 55 | +DATASTORE_DIR = _writable_datastore_dir() |
19 | 56 |
|
20 | 57 | # Baseline model persistence file |
21 | 58 | BASELINE_FILE = DATASTORE_DIR / "baseline.json" |
|
0 commit comments