|
1 | 1 | import logging |
2 | 2 | import os |
3 | | -from typing import Optional |
| 3 | +from typing import Dict, Optional |
4 | 4 |
|
5 | 5 | import requests |
6 | | -from dotenv import find_dotenv, load_dotenv |
| 6 | +from dotenv import dotenv_values, find_dotenv, load_dotenv |
7 | 7 |
|
8 | 8 | logger = logging.getLogger(__name__) |
9 | 9 |
|
| 10 | + |
| 11 | +def find_dotenv_path(search_path: Optional[str] = None) -> Optional[str]: |
| 12 | + """ |
| 13 | + Find the .env file path, searching .env.dev first, then .env. |
| 14 | +
|
| 15 | + Args: |
| 16 | + search_path: Directory to search from. If None, uses current working directory. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + Path to the .env file if found, otherwise None. |
| 20 | + """ |
| 21 | + # If a specific search path is provided, look there first |
| 22 | + if search_path: |
| 23 | + env_dev_path = os.path.join(search_path, ".env.dev") |
| 24 | + if os.path.isfile(env_dev_path): |
| 25 | + return env_dev_path |
| 26 | + env_path = os.path.join(search_path, ".env") |
| 27 | + if os.path.isfile(env_path): |
| 28 | + return env_path |
| 29 | + return None |
| 30 | + |
| 31 | + # Otherwise use find_dotenv to search up the directory tree |
| 32 | + env_dev_path = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True) |
| 33 | + if env_dev_path: |
| 34 | + return env_dev_path |
| 35 | + env_path = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True) |
| 36 | + if env_path: |
| 37 | + return env_path |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +def get_dotenv_values(search_path: Optional[str] = None) -> Dict[str, Optional[str]]: |
| 42 | + """ |
| 43 | + Get all key-value pairs from the .env file. |
| 44 | +
|
| 45 | + Args: |
| 46 | + search_path: Directory to search from. If None, uses current working directory. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Dictionary of environment variable names to values. |
| 50 | + """ |
| 51 | + dotenv_path = find_dotenv_path(search_path) |
| 52 | + if dotenv_path: |
| 53 | + return dotenv_values(dotenv_path) |
| 54 | + return {} |
| 55 | + |
| 56 | + |
10 | 57 | # --- Load .env files --- |
11 | 58 | # Attempt to load .env.dev first, then .env as a fallback. |
12 | 59 | # This happens when the module is imported. |
13 | 60 | # We use override=False (default) so that existing environment variables |
14 | 61 | # (e.g., set in the shell) are NOT overridden by .env files. |
15 | | -_ENV_DEV_PATH = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True) |
16 | | -if _ENV_DEV_PATH: |
17 | | - load_dotenv(dotenv_path=_ENV_DEV_PATH, override=False) |
18 | | - logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_DEV_PATH}") |
| 62 | +_DOTENV_PATH = find_dotenv_path() |
| 63 | +if _DOTENV_PATH: |
| 64 | + load_dotenv(dotenv_path=_DOTENV_PATH, override=False) |
| 65 | + logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_DOTENV_PATH}") |
19 | 66 | else: |
20 | | - _ENV_PATH = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True) |
21 | | - if _ENV_PATH: |
22 | | - load_dotenv(dotenv_path=_ENV_PATH, override=False) |
23 | | - logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_PATH}") |
24 | | - else: |
25 | | - logger.debug( |
26 | | - "eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables." |
27 | | - ) |
| 67 | + logger.debug( |
| 68 | + "eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables." |
| 69 | + ) |
28 | 70 | # --- End .env loading --- |
29 | 71 |
|
30 | 72 |
|
|
0 commit comments