Spaces:
Paused
Paused
File size: 1,524 Bytes
c97431f | 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 | import os
from pathlib import Path
try:
import tomllib
except ImportError:
try:
import tomli as tomllib # type: ignore[no-redef]
except ImportError:
try:
import tomlkit as tomllib # type: ignore[no-redef]
except ImportError:
raise ImportError(
"No TOML library found. Please run on Python 3.11+, "
"or run 'pip install tomli' to support Python 3.10."
)
try:
from dotenv import load_dotenv
except ImportError:
load_dotenv = None
BACKEND_DIR = Path(__file__).resolve().parent.parent
REPO_ROOT = BACKEND_DIR.parent.parent
if load_dotenv is not None:
load_dotenv(BACKEND_DIR / ".env")
def load_config() -> dict:
config_path = os.getenv("VISUALIZER_CONFIG")
if not config_path:
return {}
path = Path(config_path).expanduser()
if not path.is_absolute():
path = BACKEND_DIR / path
if not path.exists():
raise RuntimeError(f"VISUALIZER_CONFIG does not exist: {path}")
with path.open("rb") as config_file:
return tomllib.load(config_file)
CONFIG = load_config()
def config_value(env_name: str, section: str, key: str, default):
if env_name in os.environ:
return os.environ[env_name]
return CONFIG.get(section, {}).get(key, default)
def config_bool(env_name: str, section: str, key: str, default: str = "0") -> bool:
return str(config_value(env_name, section, key, default)).lower() in {
"1", "true", "yes", "on",
}
|