-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathconfig.py
More file actions
58 lines (46 loc) · 1.5 KB
/
config.py
File metadata and controls
58 lines (46 loc) · 1.5 KB
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
# (C) 2024 GoodData Corporation
import os
from typing import Any, Optional, TypeVar
import attrs
from attrs import define
from cattrs import structure
from cattrs.errors import ClassValidationError
from dotenv import load_dotenv
T = TypeVar("T", bound="ConfigBase")
@define
class ConfigBase:
def to_dict(self) -> dict[str, str]:
return attrs.asdict(self)
@classmethod
def from_dict(cls: type[T], data: dict[str, Any]) -> T:
return structure(data, cls)
@classmethod
def can_structure(cls: type[T], data: dict[str, Any]) -> bool:
try:
cls.from_dict(data)
return True
except ClassValidationError:
return False
@define
class Profile(ConfigBase):
host: str
token: str
custom_headers: Optional[dict[str, str]] = None
extra_user_agent: Optional[str] = None
ssl_ca_cert: Optional[str] = None
def to_dict(self, use_env: bool = False) -> dict[str, str]:
load_dotenv()
if not use_env:
return attrs.asdict(self)
env_var = self.token[1:]
if env_var not in os.environ:
raise ValueError(f"Environment variable {env_var} not found")
return {**attrs.asdict(self), "token": os.environ[env_var]}
@define
class AacConfig(ConfigBase):
profiles: dict[str, Profile]
default_profile: str
access: dict[str, str]
def ds_credentials(self) -> dict[str, str]:
load_dotenv()
return {k: os.environ.get(v[1:], v) for k, v in self.access.items()}