From b93f95020dae6abf8c0f3d7395204d7431d57634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 14 Oct 2022 23:29:05 +0000 Subject: [PATCH] correct text file decoding --- api/advent22_api/dav_common.py | 8 +++++++- api/advent22_api/routers/_misc.py | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/api/advent22_api/dav_common.py b/api/advent22_api/dav_common.py index b01d898..ab602d6 100644 --- a/api/advent22_api/dav_common.py +++ b/api/advent22_api/dav_common.py @@ -1,5 +1,6 @@ import re -from io import BytesIO +from io import BytesIO, TextIOWrapper +from typing import TextIO from webdav3.client import Client as WebDAVclient @@ -29,3 +30,8 @@ async def dav_get_file(path: str) -> BytesIO: buffer.seek(0) return buffer + + +async def dav_get_textfile(path: str, encoding="utf-8") -> TextIO: + buffer = await dav_get_file(path) + return TextIOWrapper(buffer, encoding=encoding) diff --git a/api/advent22_api/routers/_misc.py b/api/advent22_api/routers/_misc.py index 3947a13..f1c98de 100644 --- a/api/advent22_api/routers/_misc.py +++ b/api/advent22_api/routers/_misc.py @@ -2,12 +2,12 @@ import itertools import random from typing import Any, Sequence -from ..dav_common import dav_get_file +from ..dav_common import dav_get_textfile async def get_loesungswort() -> str: - fp = await dav_get_file("loesungswort.txt") - return fp.read().decode("utf8").strip() + fp = await dav_get_textfile("loesungswort.txt") + return fp.read().strip() async def get_rnd(bonus_salt: Any = "") -> random.Random: