forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
40 lines (32 loc) · 942 Bytes
/
config.py
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
34
35
36
37
38
39
40
import os
import toml
from dotenv import load_dotenv
load_dotenv()
config_str = ""
if os.path.exists("config.toml"):
with open("config.toml", "rb") as f:
config_str = f.read().decode("utf-8")
config = toml.loads(config_str)
def _get(key: str, default):
value = config.get(key, default)
if not value:
value = os.environ.get(key, default)
return value
def get_or_error(key: str):
"""
Get a key from the config, or raise an error if it doesn't exist.
"""
value = get_or_none(key)
if not value:
raise KeyError(f"Please set '{key}' in `config.toml` or `.env`.")
return value
def get_or_default(key: str, default):
"""
Get a key from the config, or return a default value if it doesn't exist.
"""
return _get(key, default)
def get_or_none(key: str):
"""
Get a key from the config, or return None if it doesn't exist.
"""
return _get(key, None)