config.py
This commit is contained in:
parent
78d9f7d194
commit
65922626bb
6 changed files with 41 additions and 17 deletions
16
api/advent22_api/config.py
Normal file
16
api/advent22_api/config.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
import tomli
|
||||
from pydantic import BaseModel
|
||||
|
||||
from .dav_common import dav_get_textfile_content
|
||||
from .settings import SETTINGS
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
admin_password: str
|
||||
solution: str
|
||||
|
||||
|
||||
async def get_config() -> Config:
|
||||
txt = await dav_get_textfile_content(path=SETTINGS.config_filename)
|
||||
|
||||
return Config.parse_obj(tomli.loads(txt))
|
|
@ -2,18 +2,12 @@ import itertools
|
|||
import random
|
||||
from typing import Any, Sequence
|
||||
|
||||
from advent22_api.settings import SETTINGS
|
||||
|
||||
from ..dav_common import dav_get_textfile_content
|
||||
|
||||
|
||||
async def get_loesungswort() -> str:
|
||||
return await dav_get_textfile_content(SETTINGS.solution_filename)
|
||||
from ..config import get_config
|
||||
|
||||
|
||||
async def get_rnd(bonus_salt: Any = "") -> random.Random:
|
||||
loesungswort = await get_loesungswort()
|
||||
return random.Random(f"{loesungswort}{bonus_salt}")
|
||||
cfg = await get_config()
|
||||
return random.Random(f"{cfg.solution}{bonus_salt}")
|
||||
|
||||
|
||||
async def set_length(seq: Sequence, length: int) -> list:
|
||||
|
|
|
@ -6,26 +6,27 @@ from fastapi import APIRouter, Depends
|
|||
from fastapi.responses import StreamingResponse
|
||||
from PIL import ImageFont
|
||||
|
||||
from ..config import Config, get_config
|
||||
from ..dav_common import dav_get_file, dav_list_files
|
||||
from ._image import AdventImage
|
||||
from ._misc import get_loesungswort, get_rnd, set_length, shuffle
|
||||
from ._misc import get_rnd, set_length, shuffle
|
||||
|
||||
router = APIRouter(prefix="/days", tags=["days"])
|
||||
|
||||
|
||||
@router.on_event("startup")
|
||||
async def startup() -> None:
|
||||
loesungswort = await get_loesungswort()
|
||||
print(loesungswort)
|
||||
print("".join(await shuffle(loesungswort)))
|
||||
cfg = await get_config()
|
||||
print(cfg.solution)
|
||||
print("".join(await shuffle(cfg.solution)))
|
||||
|
||||
|
||||
@router.get("/letter/{index}")
|
||||
async def get_letter(
|
||||
index: int,
|
||||
loesungswort: str = Depends(get_loesungswort),
|
||||
cfg: Config = Depends(get_config),
|
||||
) -> str:
|
||||
return (await shuffle(loesungswort))[index]
|
||||
return (await shuffle(cfg.solution))[index]
|
||||
|
||||
|
||||
# @router.get("/date")
|
||||
|
|
|
@ -52,7 +52,7 @@ class Settings(BaseSettings):
|
|||
webdav: DavSettings = DavSettings()
|
||||
|
||||
cache_ttl: int = 30
|
||||
solution_filename: str = "loesungswort.txt"
|
||||
config_filename: str = "config.toml"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
|
14
api/poetry.lock
generated
14
api/poetry.lock
generated
|
@ -233,6 +233,14 @@ anyio = ">=3.4.0,<5"
|
|||
[package.extras]
|
||||
full = ["itsdangerous", "jinja2", "python-multipart", "pyyaml", "requests"]
|
||||
|
||||
[[package]]
|
||||
name = "tomli"
|
||||
version = "2.0.1"
|
||||
description = "A lil' TOML parser"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.4.0"
|
||||
|
@ -324,7 +332,7 @@ python-versions = ">=3.7"
|
|||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.10"
|
||||
content-hash = "d37ea64eccdac520a3649119169642395eaeeb42ab20668c444774f2baccede3"
|
||||
content-hash = "b79fd848be37a72a7fb711d7e34b33dc22dc6cdea6cb2cfe1943359767728782"
|
||||
|
||||
[metadata.files]
|
||||
anyio = [
|
||||
|
@ -671,6 +679,10 @@ starlette = [
|
|||
{file = "starlette-0.20.4-py3-none-any.whl", hash = "sha256:c0414d5a56297d37f3db96a84034d61ce29889b9eaccf65eb98a0b39441fcaa3"},
|
||||
{file = "starlette-0.20.4.tar.gz", hash = "sha256:42fcf3122f998fefce3e2c5ad7e5edbf0f02cf685d646a83a08d404726af5084"},
|
||||
]
|
||||
tomli = [
|
||||
{file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"},
|
||||
{file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},
|
||||
]
|
||||
typing-extensions = [
|
||||
{file = "typing_extensions-4.4.0-py3-none-any.whl", hash = "sha256:16fa4864408f655d35ec496218b85f79b3437c829e93320c7c9215ccfd92489e"},
|
||||
{file = "typing_extensions-4.4.0.tar.gz", hash = "sha256:1511434bb92bf8dd198c12b1cc812e800d4181cfcb867674e0f8279cc93087aa"},
|
||||
|
|
|
@ -16,6 +16,7 @@ Pillow = "^9.2.0"
|
|||
numpy = "^1.23.3"
|
||||
webdavclient3 = "3.14.5"
|
||||
async-cache = "^1.1.1"
|
||||
tomli = "^2.0.1"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
|
||||
|
|
Loading…
Reference in a new issue