File size: 1,380 Bytes
5c32957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path
from typing import Dict

def load_env_file(env_path: Path | str) -> Dict[str, str]:
    """
    Load environment variables from a .env file.
    
    Args:
        env_path: Path to the .env file
        
    Returns:
        Dict of environment variables loaded
    
    Raises:
        FileNotFoundError: If the .env file doesn't exist
    """
    env_vars = {}
    
    with open(env_path) as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith('#'):
                key, value = line.split('=', 1)
                key = key.strip()
                value = value.strip()
                os.environ[key] = value
                env_vars[key] = value
                
    return env_vars


def init_environment() -> Dict[str, str]:
    """
    Initialize environment variables from .env file in project root.
    
    Returns:
        Dict of environment variables loaded
        
    Raises:
        FileNotFoundError: If the .env file doesn't exist
    """
    root_dir = Path(__file__).parent.parent.parent
    env_file = root_dir / ".env"
    
    if not env_file.exists():
        raise FileNotFoundError(
            f"Environment file not found at {env_file}. "
            "Please create a .env file in the project root directory."
        )
        
    return load_env_file(env_file)