|
1 | 1 | from dotenv import load_dotenv
|
2 |
| -from pathlib import Path |
3 |
| - |
4 |
| -from pydantic import BaseSettings, Field, validator, HttpUrl, Extra |
5 |
| - |
6 |
| -from ixmp4.core.exceptions import InvalidCredentials |
7 |
| -from .credentials import Credentials |
8 |
| -from .toml import TomlConfig |
9 |
| -from .manager import ManagerConfig |
10 |
| -from .auth import ManagerAuth |
11 |
| -from .user import local_user |
12 |
| -from .base import PlatformInfo as PlatformInfo |
13 |
| - |
14 |
| - |
15 |
| -class Settings(BaseSettings): |
16 |
| - mode: str = "production" |
17 |
| - storage_directory: Path = Field("~/.local/share/ixmp4/", env="ixmp4_dir") |
18 |
| - secret_hs256: str = "default_secret_hs256" |
19 |
| - migration_db_uri: str = "sqlite:///./run/db.sqlite" |
20 |
| - manager_url: HttpUrl = Field("https://api.manager.ece.iiasa.ac.at/v1") |
21 |
| - |
22 |
| - class Config: |
23 |
| - env_prefix = "ixmp4_" |
24 |
| - extra = Extra.allow |
25 |
| - |
26 |
| - def __init__(self, *args, **kwargs) -> None: |
27 |
| - super().__init__(*args, **kwargs) |
28 |
| - |
29 |
| - self.storage_directory.mkdir(parents=True, exist_ok=True) |
30 |
| - |
31 |
| - database_dir = self.storage_directory / "databases" |
32 |
| - database_dir.mkdir(exist_ok=True) |
33 |
| - self.load_credentials() |
34 |
| - self.load_manager_config() |
35 |
| - self.load_toml_config() |
36 |
| - |
37 |
| - def load_credentials(self): |
38 |
| - credentials_config = self.storage_directory / "credentials.toml" |
39 |
| - credentials_config.touch() |
40 |
| - self.credentials = Credentials(credentials_config) |
41 |
| - |
42 |
| - self.default_credentials = None |
43 |
| - self.default_auth = None |
44 |
| - try: |
45 |
| - self.default_credentials = self.credentials.get("default") |
46 |
| - except KeyError: |
47 |
| - # TODO: WARNING: No default credentials provided. |
48 |
| - pass |
49 |
| - |
50 |
| - if self.default_credentials is not None: |
51 |
| - try: |
52 |
| - username, password = self.default_credentials |
53 |
| - self.default_auth = ManagerAuth(username, password, self.manager_url) |
54 |
| - except InvalidCredentials: |
55 |
| - # TODO: WARNING: Default credentials invalid. |
56 |
| - pass |
57 |
| - |
58 |
| - def load_manager_config(self): |
59 |
| - self.manager = None |
60 |
| - if self.default_auth is not None: |
61 |
| - self.manager = ManagerConfig( |
62 |
| - self.manager_url, self.default_auth, remote=True |
63 |
| - ) |
64 |
| - |
65 |
| - def load_toml_config(self): |
66 |
| - if self.default_auth is not None: |
67 |
| - toml_user = self.default_auth.get_user() |
68 |
| - else: |
69 |
| - toml_user = local_user |
70 |
| - toml_config = self.storage_directory / "platforms.toml" |
71 |
| - toml_config.touch() |
72 |
| - self.toml = TomlConfig(toml_config, toml_user) |
73 |
| - |
74 |
| - @validator("storage_directory") |
75 |
| - def expand_user(cls, v): |
76 |
| - # translate ~/asdf into /home/user/asdf |
77 |
| - return Path.expanduser(v) |
78 |
| - |
| 2 | +from ixmp4.conf.settings import Settings |
79 | 3 |
|
80 | 4 | load_dotenv()
|
81 | 5 | # strict typechecking fails due to a bug
|
|
0 commit comments