mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 08:13:01 +00:00
141 lines
3 KiB
Python
141 lines
3 KiB
Python
import re
|
|
import tomllib
|
|
from enum import Enum
|
|
from random import Random
|
|
|
|
from pydantic import BaseModel, field_validator
|
|
|
|
from .dav.webdav import WebDAV
|
|
from .settings import SETTINGS
|
|
|
|
|
|
class User(BaseModel):
|
|
name: str
|
|
password: str
|
|
|
|
|
|
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
|
|
|
|
|
|
class Puzzle(BaseModel):
|
|
# Titel
|
|
title: str
|
|
|
|
# Tag, an dem der Kalender startet
|
|
begin_day: int = 1
|
|
|
|
# Monat, in dem der Kalender startet
|
|
begin_month: int = 12
|
|
|
|
# Kalender so viele Tage nach der letzten Türöffnung schließen
|
|
close_after: int = 90
|
|
|
|
|
|
class TTFont(BaseModel):
|
|
# Dateiname (in "/files")
|
|
file: str
|
|
|
|
# Schriftgröße für den Font
|
|
size: int = 50
|
|
|
|
|
|
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]
|
|
|
|
|
|
class Config(BaseModel):
|
|
# Login-Daten für Admin-Modus
|
|
admin: User
|
|
|
|
# Lösungswort
|
|
solution: TransformedString
|
|
|
|
# Weitere Einstellungen
|
|
puzzle: Puzzle
|
|
image: Image
|
|
|
|
# Kalenderdefinition
|
|
calendar: str = "default.toml"
|
|
|
|
# Serverseitiger zusätzlicher "random" seed
|
|
random_seed: str = ""
|
|
|
|
# Fußzeile der Seite
|
|
footer: str = (
|
|
'<strong>Advent22</strong> by <a href="//www.lenaisten.de">Lenaisten e.V.</a>'
|
|
)
|
|
|
|
|
|
async def get_config() -> Config:
|
|
"""
|
|
Globale Konfiguration lesen
|
|
"""
|
|
|
|
txt = await WebDAV.read_str(path=SETTINGS.webdav.config_filename)
|
|
return Config.model_validate(tomllib.loads(txt))
|