-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_schema.py
104 lines (83 loc) · 2.92 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from enum import StrEnum
from pathlib import Path
import yaml
from pydantic import BaseModel, ConfigDict, Field, model_validator
class Environment(StrEnum):
DEVELOPMENT = "development"
PRODUCTION = "production"
class SettingBaseModel(BaseModel):
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
class LegendEntry(SettingBaseModel):
legend_id: str
"ID of the legend"
color: str | None = None
"Color of the legend"
legend: str | None = None
"Description of the legend (may contain multiple lines)"
@model_validator(mode="after")
def set_legend_as_legend_id(self):
if self.legend is None:
self.legend = self.legend_id
return self
class Area(SettingBaseModel):
svg_polygon_id: str | None = None
"ID of the polygon in the SVG"
title: str | None = None
"Title of the area"
ru_title: str | None = None
"Title in Russian"
legend_id: str | None = None
"ID of the legend (if any)"
description: str | None = None
"Description of the area"
people: list[str] = []
"List of people for this area"
prioritized: bool = False
"Priority for multi-floor areas"
room_booking_id: str | None = None
"ID of the room in Room Booking API (if any)"
class Scene(SettingBaseModel):
scene_id: str
"ID of the scene"
title: str
"Title of the scene"
svg_file: str
"Path to the SVG file in /static"
legend: list[LegendEntry] = []
"Legend of the scene"
areas: list[Area] = []
"Areas of the scene"
class Accounts(SettingBaseModel):
"""InNoHassle Accounts integration settings"""
api_url: str = "https://api.innohassle.ru/accounts/v0"
"URL of the Accounts API"
class Settings(SettingBaseModel):
"""Settings for the application."""
schema_: str = Field(None, alias="$schema")
environment: Environment = Environment.DEVELOPMENT
"App environment flag"
app_root_path: str = ""
'Prefix for the API path (e.g. "/api/v0")'
scenes: list[Scene] = []
"List of scenes"
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`
"""
static_mount_path: str = "/static"
"Path to mount static files"
static_directory: Path = Path("static")
"Path to the directory with static files"
accounts: Accounts = Accounts()
"InNoHassle-Accounts integration settings"
@classmethod
def from_yaml(cls, path: Path) -> "Settings":
with open(path, encoding="utf-8") as f:
yaml_config = yaml.safe_load(f)
return cls.model_validate(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()}
yaml.dump(schema, f, sort_keys=False)