File size: 1,093 Bytes
945aae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pathlib
import logging

logger = logging.getLogger("qkernel.env")

_ENV_CANDIDATES = [
    pathlib.Path(__file__).resolve().parent.parent.parent / ".env",
    pathlib.Path(__file__).resolve().parent.parent.parent.parent / ".env",
]


def load_env() -> None:
    for env_file in _ENV_CANDIDATES:
        if not env_file.exists():
            continue
        try:
            for raw in env_file.read_text(encoding="utf-8").splitlines():
                line = raw.strip()
                if not line or line.startswith("#") or "=" not in line:
                    continue
                key, _, value = line.partition("=")
                key = key.strip()
                value = value.strip().strip('"').strip("'")
                if not key:
                    continue
                os.environ.setdefault(key, value)
            logger.info("Loaded env from %s", env_file)
            return
        except OSError as e:
            logger.warning("Could not read env %s: %s", env_file, e)
    logger.info("No .env file found; using process environment")


load_env()