-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
71 lines (63 loc) · 2.05 KB
/
config.py
File metadata and controls
71 lines (63 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# python
"""
Project configuration: centralize filesystem base directory.
- Reads `BASE_SAVE_DIR` from environment or .env file (if python-dotenv installed).
- Exposes `PROJECT_ROOT` and `BASE_SAVE_DIR` as pathlib.Path objects.
- Falls back to a writable system temp directory if the configured location can't be created.
"""
from pathlib import Path
import os
import tempfile
import logging
logging.getLogger(__name__).addHandler(logging.NullHandler())
# Project root (directory containing this file)
PROJECT_ROOT = Path(__file__).resolve().parent
# Always try to load .env if present
dotenv_path = PROJECT_ROOT / ".env"
try:
from dotenv import load_dotenv # type: ignore
if dotenv_path.exists():
load_dotenv(dotenv_path)
except Exception:
pass
# Use BASE_SAVE_DIR from environment (set by .env if loaded), else fallback
_env_base = os.getenv("BASE_SAVE_DIR")
if _env_base:
candidate = Path(_env_base).expanduser()
else:
candidate = PROJECT_ROOT / "storage"
try:
BASE_SAVE_DIR = candidate.resolve(strict=False)
except Exception:
BASE_SAVE_DIR = candidate
try:
BASE_SAVE_DIR.mkdir(parents=True, exist_ok=True)
except Exception as exc:
fallback = Path(tempfile.gettempdir()) / "supersl2_storage"
try:
fallback.mkdir(parents=True, exist_ok=True)
BASE_SAVE_DIR = fallback
logging.warning(
"Could not create configured BASE_SAVE_DIR '%s' (%s). Using fallback '%s'.",
candidate,
exc,
BASE_SAVE_DIR,
)
except Exception as exc2:
BASE_SAVE_DIR = Path(tempfile.gettempdir())
logging.warning(
"Could not create fallback storage '%s' (%s). Using system temp '%s'.",
fallback,
exc2,
BASE_SAVE_DIR,
)
BASE_SAVE_DIR = Path(BASE_SAVE_DIR)
def is_base_dir_writable() -> bool:
try:
test_file = BASE_SAVE_DIR / ".write_test"
with open(test_file, "w") as f:
f.write("x")
test_file.unlink(missing_ok=True)
return True
except Exception:
return False