Spaces:
Sleeping
Sleeping
File size: 952 Bytes
5db30ac | 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 | from __future__ import annotations
import os
from pathlib import Path
def get_api_key(env_name: str | None) -> str:
if not env_name:
return ""
value = os.getenv(env_name, "").strip()
if value:
return value
return _read_key_file(env_name)
def _read_key_file(env_name: str) -> str:
for directory in [Path.cwd(), *Path.cwd().parents]:
key_path = directory / "key.txt"
if not key_path.exists():
continue
for raw_line in key_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
key, value = line.split("=", 1)
if key.strip() == env_name:
return value.strip().strip('"').strip("'")
continue
if env_name == "INNOSPARK_API_KEY":
return line
return ""
|