This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
65 lines (47 loc) · 1.82 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
"""
Code related to .env config
"""
import os
from typing import get_type_hints, Union
from dotenv import load_dotenv
load_dotenv()
class AppConfigError(Exception):
"""
Exception for config errors.
"""
def _parse_bool(val: Union[str, bool]) -> bool: # pylint: disable=E1136
return val if isinstance(val, bool) else val.lower()
class AppConfig:
"""
Application configuration.
"""
SNCFCONNECT_COOKIE: str
"""
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
- Class field and environment variable name are the same
"""
def __init__(self, env):
for field in AppConfig.__annotations__:
if not field.isupper():
continue
# Raise AppConfigError if required field not supplied
default_value = getattr(self, field, None)
if default_value is None and env.get(field) is None:
raise AppConfigError(f'The {field} field is required in the .env file')
# Cast env var value to expected type and raise AppConfigError on failure
var_type = get_type_hints(AppConfig)[field]
try:
if var_type == bool:
value = _parse_bool(env.get(field, default_value))
else:
value = var_type(env.get(field, default_value))
self.__setattr__(field, value)
except ValueError:
raise AppConfigError(f'Unable to cast value of "{env[field]}" '
f'to type "{var_type}" for "{field}" field') from None
def __repr__(self):
return str(self.__dict__)
# Expose Config object for app to import
Config = AppConfig(os.environ)