Files
2026-05-23 16:39:35 +08:00

35 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""加载 manual_trading_hub/.envWindows 直接 python hub.py 时也需要)。"""
from __future__ import annotations
import os
from pathlib import Path
HUB_DIR = Path(__file__).resolve().parent
def load_hub_dotenv() -> None:
path = HUB_DIR / ".env"
if not path.is_file():
return
raw_bytes = path.read_bytes()
text = ""
for enc in ("utf-8-sig", "utf-16", "utf-16-le", "utf-16-be"):
try:
text = raw_bytes.decode(enc)
break
except Exception:
continue
if not text:
text = raw_bytes.decode("utf-8", errors="ignore")
text = text.replace("\x00", "")
for line in text.splitlines():
raw = line.strip()
if not raw or raw.startswith("#") or "=" not in raw:
continue
key, value = raw.split("=", 1)
clean_key = key.strip().lstrip("\ufeff")
if not clean_key.replace("_", "").isalnum():
continue
clean_value = value.strip().strip('"').strip("'")
os.environ[clean_key] = clean_value