DAV settings rework

This commit is contained in:
Jörn-Michael Miehe 2022-09-05 14:39:34 +00:00
parent 1d62e59052
commit 8eeb3a61a7
5 changed files with 72 additions and 34 deletions

View file

@ -48,7 +48,7 @@ class Config(BaseModel):
Load the configuration instance from the server using `TOML`. Load the configuration instance from the server using `TOML`.
""" """
dav_file = DavFile("config.txt") dav_file = DavFile("ovdashboard/config.txt")
try: try:
return cls.parse_obj( return cls.parse_obj(

View file

@ -14,9 +14,9 @@ from .async_helpers import run_in_executor
from .settings import SETTINGS from .settings import SETTINGS
_WEBDAV_CLIENT = WebDAVclient({ _WEBDAV_CLIENT = WebDAVclient({
"webdav_hostname": SETTINGS.webdav_url, "webdav_hostname": SETTINGS.webdav.url,
"webdav_login": SETTINGS.dav_username, "webdav_login": SETTINGS.webdav.username,
"webdav_password": SETTINGS.dav_password, "webdav_password": SETTINGS.webdav.password,
}) })
@ -39,9 +39,9 @@ def webdav_list(remote_path: str) -> list[str]:
_CALDAV_CLIENT = CalDAVclient( _CALDAV_CLIENT = CalDAVclient(
url=SETTINGS.caldav_url, url=SETTINGS.caldav.url,
username=SETTINGS.dav_username, username=SETTINGS.caldav.username,
password=SETTINGS.dav_password, password=SETTINGS.caldav.password,
) )

View file

@ -62,7 +62,7 @@ async def get_image(
prefix: str, prefix: str,
name: str = Depends(_unique), name: str = Depends(_unique),
) -> str: ) -> str:
dav_file = DavFile(f"{_lister.remote_path}/{name}") dav_file = DavFile(f"ovdashboard/{_lister.remote_path}/{name}")
img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB") img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB")
img_buffer = BytesIO() img_buffer = BytesIO()

View file

@ -22,7 +22,7 @@ from ._common import FileNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/text", tags=["text"]) router = APIRouter(prefix="/text", tags=["text"])
_lister = FileNameLister( _lister = FileNameLister(
remote_path="text", remote_path="ovdashboard/text",
re=re.compile( re=re.compile(
r"\.(txt|md)$", r"\.(txt|md)$",
flags=re.IGNORECASE, flags=re.IGNORECASE,
@ -34,7 +34,7 @@ _unique = PrefixUnique(_finder)
async def get_ticker_lines() -> Iterator[str]: async def get_ticker_lines() -> Iterator[str]:
ticker = await DavFile("text/ticker.txt").string ticker = await DavFile("ovdashboard/text/ticker.txt").string
return ( return (
line.strip() line.strip()
@ -104,7 +104,7 @@ async def find_texts(
async def get_text_content( async def get_text_content(
name: str = Depends(_unique), name: str = Depends(_unique),
) -> str: ) -> str:
return await DavFile(f"{_lister.remote_path}/{name}").string return await DavFile(f"ovdashboard/{_lister.remote_path}/{name}").string
@router.get( @router.get(

View file

@ -9,12 +9,42 @@ Pydantic models might have convenience methods attached.
from typing import Optional from typing import Optional
from pydantic import BaseSettings from pydantic import BaseModel, BaseSettings, root_validator
class DavSettings(BaseModel):
"""
Connection to a DAV server.
"""
protocol: Optional[str]
host: Optional[str]
username: Optional[str]
password: Optional[str]
path: Optional[str]
@property
def url(self) -> str:
"""
Combined DAV URL.
"""
return f"{self.protocol}://{self.host}{self.path}"
@classmethod
@property
def default(cls) -> "DavSettings":
return cls(
protocol="https",
host="example.com",
username="ovdashboard",
password="secret",
)
class Settings(BaseSettings): class Settings(BaseSettings):
""" """
Per-run settings Per-run settings.
""" """
production_mode: bool = False production_mode: bool = False
@ -23,33 +53,41 @@ class Settings(BaseSettings):
docs_url: Optional[str] = "/docs" docs_url: Optional[str] = "/docs"
redoc_url: Optional[str] = "/redoc" redoc_url: Optional[str] = "/redoc"
caldav_base_url: str = "/remote.php/dav" webdav: DavSettings = DavSettings()
webdav_base_url: str = "/remote.php/webdav/ovdashboard" caldav: DavSettings = DavSettings()
dav_protocol: str = "https"
dav_host: str = "example.com"
dav_username: str = "ovdashboard"
dav_password: str = "changeme"
dav_path: str = "ovdashboard"
cache_seconds: int = 30 cache_seconds: int = 30
@property class Config:
def caldav_url(self) -> str: env_nested_delimiter = "__"
"""
Combined CalDAV URL.
"""
return f"{self.dav_protocol}://" + \ @root_validator(pre=True)
f"{self.dav_host}{self.caldav_base_url}" @classmethod
def validate_dav_settings(cls, values):
# ensure both settings dicts are created
for key in ("webdav", "caldav"):
if key not in values:
values[key] = {}
@property default_dav = DavSettings.default.dict()
def webdav_url(self) -> str:
"""
Combined WebDAV URL.
"""
return f"{self.dav_protocol}://" + \ for key in default_dav:
f"{self.dav_host}{self.webdav_base_url}" # if "webdav" value is not specified, use default
if key not in values["webdav"]:
values["webdav"][key] = default_dav[key]
# if "caldav" value is not specified, use "webdav" value
if key not in values["caldav"]:
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"
return values
SETTINGS = Settings(_env_file=".env") SETTINGS = Settings(_env_file=".env")