advent22/api/advent22_api/core/config.py

146 lines
3.1 KiB
Python
Raw Normal View History

2023-10-28 21:46:13 +00:00
import re
import tomllib
2023-10-28 21:46:13 +00:00
from enum import Enum
from random import Random
2023-10-28 21:46:13 +00:00
from pydantic import BaseModel, field_validator
2022-11-04 18:49:31 +00:00
2023-10-29 16:08:16 +00:00
from .dav.webdav import WebDAV
from .settings import SETTINGS
2022-11-04 18:49:31 +00:00
2022-11-15 22:17:32 +00:00
class User(BaseModel):
name: str
password: str
2023-10-28 21:46:13 +00:00
class TransformedString(BaseModel):
class __Whitespace(str, Enum):
# Leerzeichen an Anfang und Ende entfernen
STRIP = "STRIP"
# whitespace entfernen
IGNORE = "IGNORE"
class __Case(str, Enum):
# GROSSBUCHSTABEN
UPPER = "UPPER"
# kleinbuchstaben
LOWER = "LOWER"
# ZuFÄllIg
RANDOM = "RANDOM"
value: str
whitespace: __Whitespace | None = __Whitespace.IGNORE
case: __Case | None = __Case.UPPER
@field_validator("whitespace", "case", mode="before")
def transform_from_str(cls, v) -> str | None:
if (result := str(v).upper()) != "KEEP":
return result
@property
def clean(self) -> str:
result = self.value
# Whitespace bearbeiten
if self.whitespace is self.__Whitespace.STRIP:
result = result.strip()
elif self.whitespace is self.__Whitespace.IGNORE:
result = re.sub(string=result, pattern=r"\s+", repl="")
# Groß-/Kleinschreibung verarbeiten
if self.case is self.__Case.UPPER:
result = result.upper()
elif self.case is self.__Case.LOWER:
result = result.lower()
elif self.case is self.__Case.RANDOM:
rnd = Random(self.value)
def randomcase(c: str) -> str:
if rnd.choice((True, False)):
return c.upper()
return c.lower()
result = "".join(randomcase(c) for c in result)
return result
2023-09-21 11:49:28 +00:00
class Puzzle(BaseModel):
# Titel
2023-09-26 22:23:23 +00:00
# TODO penner neue Route GET /user/title
2023-09-21 11:49:28 +00:00
title: str
2023-09-14 23:38:35 +00:00
# Tag, an dem der Kalender startet
# TODO penner
2023-09-20 16:14:58 +00:00
begin_day: int = 1
2023-09-14 23:38:35 +00:00
# Monat, in dem der Kalender startet
2023-09-08 10:23:43 +00:00
# TODO penner
2023-09-20 16:14:58 +00:00
begin_month: int = 12
2023-09-07 16:44:44 +00:00
2023-09-20 16:14:58 +00:00
# Kalender so viele Tage nach der letzten Türöffnung schließen
2023-09-08 10:23:43 +00:00
# TODO penner
2023-09-20 16:14:58 +00:00
close_after: int = 90
2023-09-07 19:34:11 +00:00
2023-09-21 11:49:28 +00:00
class TTFont(BaseModel):
# Dateiname (in "/files")
file: str
2023-09-12 20:45:57 +00:00
2023-09-21 11:49:28 +00:00
# Schriftgröße für den Font
size: int = 50
2023-09-07 16:44:44 +00:00
2023-09-07 21:05:00 +00:00
2023-09-21 11:49:28 +00:00
class Image(BaseModel):
# Quadrat, Seitenlänge in px
# TODO penner
size: int = 1000
# Rand in px, wo keine Buchstaben untergebracht werden
# TODO penner
border: int = 60
# Schriftarten
# TODO
fonts: list[TTFont]
2023-09-07 21:05:00 +00:00
2023-09-07 16:44:44 +00:00
2022-11-04 18:49:31 +00:00
class Config(BaseModel):
2023-10-31 19:18:18 +00:00
# Login-Daten für Admin-Modus
2022-11-15 22:17:32 +00:00
admin: User
2023-10-31 19:18:18 +00:00
# Lösungswort
solution: TransformedString
# Weitere Einstellungen
2022-11-18 01:39:05 +00:00
puzzle: Puzzle
2023-09-21 11:49:28 +00:00
image: Image
# Kalenderdefinition
calendar: str = "default.toml"
# Serverseitiger zusätzlicher "random" seed
random_seed: str = ""
2023-09-26 22:23:23 +00:00
# Fußzeile der Seite
footer: str = (
'<strong>Advent22</strong> by <a href="//www.lenaisten.de">Lenaisten e.V.</a>'
)
2023-09-14 23:38:35 +00:00
async def get_config() -> Config:
"""
Globale Konfiguration lesen
"""
2023-09-10 02:59:57 +00:00
txt = await WebDAV.read_str(path=SETTINGS.webdav.config_filename)
return Config.model_validate(tomllib.loads(txt))