-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_schema.py
85 lines (65 loc) · 2.67 KB
/
config_schema.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
from enum import StrEnum
from pathlib import Path
import yaml
from pydantic import BaseModel, ConfigDict, Field, SecretStr
class Environment(StrEnum):
DEVELOPMENT = "development"
PRODUCTION = "production"
TESTING = "testing"
class SettingsEntityModel(BaseModel):
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
class MusicRoom(SettingsEntityModel):
"""InNoHassle-MusicRoom integration settings"""
api_url: str
"URL of the Music Room API"
api_key: SecretStr
"API key for the Music Room API"
class Sport(SettingsEntityModel):
"""Innopolis Sport integration settings"""
api_url: str = "https://sport.innopolis.university/api"
"URL of the Sport API"
class Accounts(SettingsEntityModel):
"""InNoHassle Accounts integration settings"""
api_url: str = "https://api.innohassle.ru/accounts/v0"
"URL of the Accounts API"
api_jwt_token: SecretStr
"JWT token for accessing the Accounts API as a service"
class Settings(SettingsEntityModel):
"""Settings for the application. Get settings from `settings.yaml` file."""
app_root_path: str = ""
"Prefix for the API path (e.g. '/api/v0')"
environment: Environment = Environment.DEVELOPMENT
"App environment"
db_url: SecretStr = Field(
examples=[
"postgresql+asyncpg://postgres:postgres@localhost:5432/postgres",
"postgresql+asyncpg://postgres:postgres@db:5432/postgres",
],
)
"PostgreSQL database connection URL"
cors_allow_origin_regex: str = ".*"
"Allowed origins for CORS: from which domains requests to the API are allowed. Specify as a regex: `https://.*.innohassle.ru`"
predefined_dir: Path = Path("./predefined")
"Path to the directory with predefined data"
accounts: Accounts
"InNoHassle Accounts integration settings"
music_room: MusicRoom | None = None
"InNoHassle MusicRoom integration settings"
sport: Sport = Sport()
"Innopolis Sport integration settings"
@classmethod
def from_yaml(cls, path: Path) -> "Settings":
with open(path, encoding="utf-8") as f:
yaml_config: dict = yaml.safe_load(f)
yaml_config.pop("$schema", None)
return cls.parse_obj(yaml_config)
@classmethod
def save_schema(cls, path: Path) -> None:
with open(path, "w", encoding="utf-8") as f:
schema = {"$schema": "https://json-schema.org/draft-07/schema", **cls.model_json_schema()}
schema["properties"]["$schema"] = {
"description": "Path to the schema file",
"title": "Schema",
"type": "string",
}
yaml.dump(schema, f, sort_keys=False)