让 .env 文件可以在系统 config 文件夹内盛放
This commit is contained in:
@ -15,7 +15,9 @@ console = Console(
|
||||
|
||||
SCRIPT_DIR = Path(__file__).parent
|
||||
DATA_DIR = SCRIPT_DIR.parent / "data"
|
||||
ENV_FILE = SCRIPT_DIR / ".env"
|
||||
GLOBAL_ENV_DIR = Path.home() / ".config" / "phomework"
|
||||
GLOBAL_ENV_FILE = GLOBAL_ENV_DIR / ".env"
|
||||
LOCAL_ENV_FILE = SCRIPT_DIR / ".env"
|
||||
|
||||
|
||||
def setup_logging(name: str, verbose: bool) -> logging.Logger:
|
||||
@ -31,11 +33,23 @@ def setup_logging(name: str, verbose: bool) -> logging.Logger:
|
||||
|
||||
|
||||
def load_env() -> dict[str, str]:
|
||||
"""Load environment variables from .env file."""
|
||||
if ENV_FILE.exists():
|
||||
load_dotenv(ENV_FILE)
|
||||
"""Load environment variables from .env file.
|
||||
|
||||
Priority: scripts/.env > ~/.config/phomework/.env
|
||||
"""
|
||||
env_loaded = False
|
||||
|
||||
if LOCAL_ENV_FILE.exists():
|
||||
load_dotenv(LOCAL_ENV_FILE)
|
||||
env_loaded = True
|
||||
elif GLOBAL_ENV_FILE.exists():
|
||||
load_dotenv(GLOBAL_ENV_FILE)
|
||||
env_loaded = True
|
||||
console.print(f"[dim]Using config from {GLOBAL_ENV_FILE}[/dim]")
|
||||
else:
|
||||
console.print(f"[yellow]Warning: .env file not found at {ENV_FILE}[/yellow]")
|
||||
console.print(
|
||||
f"[yellow]Warning: .env not found at {LOCAL_ENV_FILE} or {GLOBAL_ENV_FILE}[/yellow]"
|
||||
)
|
||||
|
||||
api_endpoint = os.environ.get("IMG2TYP_API_ENDPOINT", "")
|
||||
api_key = os.environ.get("IMG2TYP_API_KEY", "")
|
||||
|
||||
Reference in New Issue
Block a user