forked from ggozad/oterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
104 lines (81 loc) · 3.06 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import os
from pathlib import Path
from typing import Union, get_type_hints
from dotenv import load_dotenv
from oterm.utils import get_default_data_dir
load_dotenv()
class EnvConfigError(Exception):
pass
def _parse_bool(val: Union[str, bool]) -> bool:
return val if isinstance(val, bool) else val.lower() in ["true", "yes", "1"]
class EnvConfig:
"""
Map environment variables to class fields according to these rules:
- Field won't be parsed unless it has a type annotation
- Field will be skipped if not in all caps
"""
ENV: str = "development"
OLLAMA_HOST: str = "0.0.0.0:11434"
OLLAMA_URL: str = ""
OTERM_VERIFY_SSL: bool = True
OTERM_DATA_DIR: Path = get_default_data_dir()
def __init__(self, env):
for field in self.__annotations__:
if not field.isupper():
continue
# Raise EnvConfigError if required field not supplied
default_value = getattr(self, field, None)
if default_value is None and env.get(field) is None:
raise EnvConfigError("The {} field is required".format(field))
# Cast env var value to expected type and raise AppConfigError on failure
try:
var_type = get_type_hints(EnvConfig)[field]
if var_type == bool:
value = _parse_bool(env.get(field, default_value))
elif var_type == list[str]:
value = env.get(field)
if value is None:
value = default_value
else:
value = json.loads(value)
else:
value = var_type(env.get(field, default_value))
self.__setattr__(field, value)
except ValueError:
raise EnvConfigError(
'Unable to cast value of "{}" to type "{}" for "{}" field'.format(
env[field], var_type, field # type: ignore
)
)
if self.OLLAMA_URL == "":
self.OLLAMA_URL = f"http://{self.OLLAMA_HOST}"
def __repr__(self):
return str(self.__dict__)
# Expose EnvConfig object for app to import
envConfig = EnvConfig(os.environ)
class AppConfig:
def __init__(self, path: Path | None = None):
if path is None:
path = envConfig.OTERM_DATA_DIR / "config.json"
self._path = path
self._data = {
"theme": "dark",
}
try:
with open(self._path, "r") as f:
saved = json.load(f)
self._data = self._data | saved
except FileNotFoundError:
Path.mkdir(self._path.parent, parents=True, exist_ok=True)
self.save()
def set(self, key, value):
self._data[key] = value
self.save()
def get(self, key):
return self._data.get(key)
def save(self):
with open(self._path, "w") as f:
json.dump(self._data, f)
# Expose AppConfig object for app to import
appConfig = AppConfig()