File size: 1,201 Bytes
070061f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Environment loader for HicXAI agent
Loads configuration from .env file securely
"""

import os
from pathlib import Path

def _load_env_file(path: Path) -> bool:
    if not path.exists():
        return False
    with open(path, 'r') as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith('#') and '=' in line:
                key, value = line.split('=', 1)
                k = key.strip()
                v = value.strip()
                # Do NOT override variables already set in the process env
                # This preserves values set by entrypoints (e.g., app_v1.py sets HICXAI_VERSION=v1)
                if k not in os.environ:
                    os.environ[k] = v
    return True


def load_env() -> bool:
    """Load environment variables from .env.local (preferred) and .env files."""
    root = Path(__file__).parent.parent
    loaded_any = False
    # Prefer .env.local for developer-specific overrides
    loaded_any = _load_env_file(root / '.env.local') or loaded_any
    # Then load .env as the shared defaults
    loaded_any = _load_env_file(root / '.env') or loaded_any
    return loaded_any

# Load .env on import
load_env()