Spaces:
Sleeping
Sleeping
Add dotenv.py helper to fix ImportError
Browse files- python/helpers/dotenv.py +50 -0
python/helpers/dotenv.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Simple dotenv implementation for loading environment variables from .env files.
|
| 3 |
+
This is a compatibility layer for the agent-zero framework.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import re
|
| 8 |
+
from typing import Optional, Dict
|
| 9 |
+
|
| 10 |
+
def load_dotenv(path: Optional[str] = None) -> bool:
|
| 11 |
+
"""
|
| 12 |
+
Load environment variables from a .env file.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
path: Path to the .env file. If None, looks for .env in current directory.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
True if the file was loaded successfully, False otherwise.
|
| 19 |
+
"""
|
| 20 |
+
if path is None:
|
| 21 |
+
path = ".env"
|
| 22 |
+
|
| 23 |
+
if not os.path.exists(path):
|
| 24 |
+
return False
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
with open(path, "r") as f:
|
| 28 |
+
for line in f:
|
| 29 |
+
line = line.strip()
|
| 30 |
+
# Skip empty lines and comments
|
| 31 |
+
if not line or line.startswith("#"):
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
# Match KEY=VALUE or KEY="VALUE" or KEY='VALUE'
|
| 35 |
+
match = re.match(r'^([A-Za-z_][A-Za-z0-9_]*)=(.*)$', line)
|
| 36 |
+
if match:
|
| 37 |
+
key = match.group(1)
|
| 38 |
+
value = match.group(2)
|
| 39 |
+
|
| 40 |
+
# Remove quotes if present
|
| 41 |
+
if (value.startswith('"') and value.endswith('"')) or \
|
| 42 |
+
(value.startswith("'") and value.endswith("'")):
|
| 43 |
+
value = value[1:-1]
|
| 44 |
+
|
| 45 |
+
# Set environment variable
|
| 46 |
+
os.environ[key] = value
|
| 47 |
+
|
| 48 |
+
return True
|
| 49 |
+
except Exception:
|
| 50 |
+
return False
|