Spaces:
Sleeping
Sleeping
File size: 1,563 Bytes
6b16661 | 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 | """
Simple dotenv implementation for loading environment variables from .env files.
This is a compatibility layer for the agent-zero framework.
"""
import os
import re
from typing import Optional, Dict
def load_dotenv(path: Optional[str] = None) -> bool:
"""
Load environment variables from a .env file.
Args:
path: Path to the .env file. If None, looks for .env in current directory.
Returns:
True if the file was loaded successfully, False otherwise.
"""
if path is None:
path = ".env"
if not os.path.exists(path):
return False
try:
with open(path, "r") as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith("#"):
continue
# Match KEY=VALUE or KEY="VALUE" or KEY='VALUE'
match = re.match(r'^([A-Za-z_][A-Za-z0-9_]*)=(.*)$', line)
if match:
key = match.group(1)
value = match.group(2)
# Remove quotes if present
if (value.startswith('"') and value.endswith('"')) or \
(value.startswith("'") and value.endswith("'")):
value = value[1:-1]
# Set environment variable
os.environ[key] = value
return True
except Exception:
return False
|