advent22/api/advent22_api/core/config.py

165 lines
3.8 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-11-03 14:40:44 +00:00
from markdown import markdown
from pydantic import BaseModel, ConfigDict, 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):
2023-10-31 21:36:22 +00:00
# unverändert
KEEP = "KEEP"
2023-10-28 21:46:13 +00:00
# Leerzeichen an Anfang und Ende entfernen
STRIP = "STRIP"
2023-11-21 21:56:22 +00:00
# whitespace durch Leerzeichen ersetzen
SPACE = "SPACE"
2023-10-28 21:46:13 +00:00
# whitespace entfernen
2023-11-21 21:56:22 +00:00
REMOVE = "REMOVE"
2023-10-28 21:46:13 +00:00
2023-11-21 22:20:22 +00:00
class __SpecialChars(str, Enum):
# unverändert
KEEP = "KEEP"
# Sonderzeichen entfernen
REMOVE = "REMOVE"
2023-11-03 14:40:44 +00:00
2023-10-28 21:46:13 +00:00
class __Case(str, Enum):
2023-10-31 21:36:22 +00:00
# unverändert
KEEP = "KEEP"
2023-10-28 21:46:13 +00:00
# GROSSBUCHSTABEN
UPPER = "UPPER"
# kleinbuchstaben
LOWER = "LOWER"
# ZuFÄllIg
RANDOM = "RANDOM"
value: str
2023-11-21 21:56:22 +00:00
whitespace: __Whitespace = __Whitespace.REMOVE
2023-11-21 22:20:22 +00:00
special_chars: __SpecialChars = __SpecialChars.REMOVE
2023-10-31 21:36:22 +00:00
case: __Case = __Case.UPPER
2023-10-28 21:46:13 +00:00
@field_validator("whitespace", "case", mode="before")
2023-10-31 21:36:22 +00:00
def transform_from_str(cls, v) -> str:
return str(v).upper()
2023-10-28 21:46:13 +00:00
@property
def clean(self) -> str:
result = self.value
2023-11-21 21:56:22 +00:00
# Whitespace verarbeiten
if self.whitespace is TransformedString.__Whitespace.STRIP:
2023-10-28 21:46:13 +00:00
result = result.strip()
2023-11-21 21:56:22 +00:00
elif self.whitespace is TransformedString.__Whitespace.SPACE:
result = re.sub(string=result, pattern=r"\s+", repl=" ")
elif self.whitespace is TransformedString.__Whitespace.REMOVE:
2023-10-28 21:46:13 +00:00
result = re.sub(string=result, pattern=r"\s+", repl="")
2023-11-21 22:20:22 +00:00
# Sonderzeichen verarbeiten
if self.special_chars is TransformedString.__SpecialChars.REMOVE:
result = re.sub(string=result, pattern=r"[^a-zA-Z0-9\s]+", repl="")
2023-10-28 21:46:13 +00:00
# Groß-/Kleinschreibung verarbeiten
2023-11-21 21:56:22 +00:00
if self.case is TransformedString.__Case.UPPER:
2023-10-28 21:46:13 +00:00
result = result.upper()
2023-11-21 21:56:22 +00:00
elif self.case is TransformedString.__Case.LOWER:
2023-10-28 21:46:13 +00:00
result = result.lower()
2023-11-21 21:56:22 +00:00
elif self.case is TransformedString.__Case.RANDOM:
2023-10-28 21:46:13 +00:00
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-11-03 14:40:44 +00:00
class Site(BaseModel):
model_config = ConfigDict(validate_default=True)
2023-09-21 11:49:28 +00:00
# Titel
title: str
2023-11-03 14:40:44 +00:00
# Untertitel
subtitle: str
# Inhalt der Seite
content: str
# Fußzeile der Seite
footer: str = "**Advent22** by [Lenaisten e.V.](//www.lenaisten.de)"
@field_validator("content", "footer", mode="after")
def parse_md(cls, v) -> str:
return markdown(v)
class Puzzle(BaseModel):
2023-09-14 23:38:35 +00:00
# Tag, an dem der Kalender startet
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-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
close_after: int = 90
2023-09-07 19:34:11 +00:00
2023-09-21 11:49:28 +00:00
class Image(BaseModel):
# Quadrat, Seitenlänge in px
size: int = 1000
# Rand in px, wo keine Buchstaben untergebracht werden
border: int = 60
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
2023-11-03 14:40:44 +00:00
site: Site
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-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))