ovdashboard/api/ovdashboard_api/settings.py

101 lines
2.7 KiB
Python
Raw Normal View History

2022-08-28 23:59:57 +00:00
"""
Configuration definition.
Converts per-run (environment) variables and config files into the
"python world" using `pydantic`.
Pydantic models might have convenience methods attached.
"""
2022-09-08 00:24:36 +00:00
from typing import Any, Optional
2022-08-28 23:59:57 +00:00
2022-09-05 14:39:34 +00:00
from pydantic import BaseModel, BaseSettings, root_validator
class DavSettings(BaseModel):
"""
Connection to a DAV server.
"""
2022-09-08 00:24:36 +00:00
protocol: Optional[str] = None
host: Optional[str] = None
username: Optional[str] = None
password: Optional[str] = None
path: Optional[str] = None
2022-09-05 14:39:34 +00:00
@property
def url(self) -> str:
"""
Combined DAV URL.
"""
return f"{self.protocol}://{self.host}{self.path}"
2022-08-28 23:59:57 +00:00
class Settings(BaseSettings):
"""
2022-09-05 14:39:34 +00:00
Per-run settings.
2022-08-28 23:59:57 +00:00
"""
production_mode: bool = False
2022-09-07 11:58:43 +00:00
log_level: str = "INFO" if production_mode else "DEBUG"
2022-09-09 02:41:15 +00:00
cache_time: int = 30
2022-09-07 00:23:31 +00:00
cache_size: int = 30
2022-09-09 02:21:52 +00:00
# doesn't even have to be reachable
2022-09-09 02:41:15 +00:00
ping_host: str = "10.0.0.0"
2022-09-09 02:21:52 +00:00
ping_port: int = 1
2022-08-28 23:59:57 +00:00
openapi_url: str = "/openapi.json"
2022-09-07 11:58:43 +00:00
docs_url: Optional[str] = None if production_mode else "/docs"
redoc_url: Optional[str] = None if production_mode else "/redoc"
2022-08-29 00:36:13 +00:00
2022-09-05 14:39:34 +00:00
webdav: DavSettings = DavSettings()
2022-09-09 02:56:22 +00:00
webdav_disable_check: bool = False
2022-09-07 00:23:31 +00:00
webdav_retries: int = 20
2022-09-05 20:17:27 +00:00
webdav_prefix: str = "/ovdashboard"
2022-09-06 22:55:23 +00:00
config_path: str = "config.txt"
2022-08-29 00:36:13 +00:00
2022-09-07 00:23:31 +00:00
caldav: DavSettings = DavSettings()
2022-08-29 19:20:03 +00:00
2022-09-05 14:39:34 +00:00
class Config:
2022-09-08 00:24:36 +00:00
env_file = ".env"
env_file_encoding = "utf-8"
2022-09-05 14:39:34 +00:00
env_nested_delimiter = "__"
2022-09-05 12:54:02 +00:00
2022-09-05 14:39:34 +00:00
@root_validator(pre=True)
@classmethod
2022-09-08 00:24:36 +00:00
def validate_dav_settings(cls, values: dict[str, Any]) -> dict[str, Any]:
2022-09-05 14:39:34 +00:00
# ensure both settings dicts are created
for key in ("webdav", "caldav"):
if key not in values:
values[key] = {}
2022-08-28 23:59:57 +00:00
2022-09-09 03:16:26 +00:00
default_dav = DavSettings(
protocol="https",
host="example.com",
username="ovdashboard",
password="secret",
).dict()
2022-09-05 14:39:34 +00:00
for key in default_dav:
# if "webdav" value is not specified, use default
2022-09-09 03:16:26 +00:00
if key not in values["webdav"] or values["webdav"][key] is None:
2022-09-05 14:39:34 +00:00
values["webdav"][key] = default_dav[key]
# if "caldav" value is not specified, use "webdav" value
2022-09-09 03:16:26 +00:00
if key not in values["caldav"] or values["caldav"][key] is None:
2022-09-05 14:39:34 +00:00
values["caldav"][key] = values["webdav"][key]
# add default "path"s if None
if values["webdav"]["path"] is None:
values["webdav"]["path"] = "/remote.php/webdav"
if values["caldav"]["path"] is None:
values["caldav"]["path"] = "/remote.php/dav"
2022-09-05 12:54:02 +00:00
2022-09-05 14:39:34 +00:00
return values
2022-08-28 23:59:57 +00:00
2022-09-08 00:24:36 +00:00
SETTINGS = Settings()