Skip to content

Commit 55b38fb

Browse files
committed
Load local OpenAI key from .env
1 parent 6270f76 commit 55b38fb

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
__pycache__/
22
*.pyc
3+
.env
4+
.env.*
5+
!.env.example

scripts/generate_ai_problem_notes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@
5353
""".strip()
5454

5555

56+
def load_local_env() -> None:
57+
env_path = ROOT / ".env"
58+
if not env_path.exists():
59+
return
60+
61+
for raw_line in env_path.read_text().splitlines():
62+
line = raw_line.strip()
63+
if not line or line.startswith("#") or "=" not in line:
64+
continue
65+
66+
key, value = line.split("=", 1)
67+
key = key.strip()
68+
value = value.strip().strip('"').strip("'")
69+
os.environ.setdefault(key, value)
70+
71+
5672
def extract_section_body(content: str, heading: str) -> str:
5773
pattern = rf"## {re.escape(heading)}\n(.*?)(?=\n## |\Z)"
5874
match = re.search(pattern, content, flags=re.S)
@@ -239,6 +255,7 @@ def parse_args() -> argparse.Namespace:
239255

240256

241257
def main() -> int:
258+
load_local_env()
242259
args = parse_args()
243260
api_key = os.getenv("OPENAI_API_KEY")
244261
if not api_key:

0 commit comments

Comments
 (0)