settings.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. import os
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. def _load_env_file(path: Path) -> None:
  6. if not path.exists():
  7. return
  8. for raw_line in path.read_text(encoding="utf-8").splitlines():
  9. line = raw_line.strip()
  10. if not line or line.startswith("#"):
  11. continue
  12. if line.startswith("export "):
  13. line = line[len("export ") :].lstrip()
  14. if "=" not in line:
  15. continue
  16. key, value = line.split("=", 1)
  17. key = key.strip()
  18. value = value.strip()
  19. if not key or key in os.environ:
  20. continue
  21. if len(value) >= 2 and value[0] == value[-1] and value[0] in {'"', "'"}:
  22. value = value[1:-1]
  23. os.environ[key] = value
  24. @dataclass(frozen=True)
  25. class AppSettings:
  26. config_path: Path
  27. cache_dir: Path
  28. refresh_interval_seconds: int
  29. host: str
  30. port: int
  31. @classmethod
  32. def from_env(cls) -> "AppSettings":
  33. project_root = Path(__file__).resolve().parents[1]
  34. _load_env_file(project_root / ".env")
  35. return cls(
  36. config_path=Path(
  37. os.environ.get("OPENCLAW_CONFIG_PATH", "/home/lucky/.openclaw/openclaw.json")
  38. ),
  39. cache_dir=Path(os.environ.get("MODEL_SELECTOR_CACHE_DIR", "data/model-cache")),
  40. refresh_interval_seconds=int(os.environ.get("MODEL_SELECTOR_REFRESH_INTERVAL", "1800")),
  41. host=os.environ.get("MODEL_SELECTOR_HOST", "0.0.0.0"),
  42. port=int(os.environ.get("MODEL_SELECTOR_PORT", "3300")),
  43. )