From 73bfc16bfdec3c8d1e5d7ba608bdab00ea182adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 3 Sep 2023 16:23:01 +0000 Subject: [PATCH 1/3] minor hiccups --- api/advent22_api/routers/_image.py | 5 +++-- api/advent22_api/routers/_misc.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/advent22_api/routers/_image.py b/api/advent22_api/routers/_image.py index 2ab3285..f238321 100644 --- a/api/advent22_api/routers/_image.py +++ b/api/advent22_api/routers/_image.py @@ -1,5 +1,6 @@ import colorsys from dataclasses import dataclass +from typing import Self import numpy as np from PIL import Image, ImageDraw, ImageFont @@ -10,7 +11,7 @@ class AdventImage: img: Image.Image @classmethod - async def load_standard(cls, fp) -> "AdventImage": + async def load_standard(cls, fp) -> Self: """ Bild laden und einen quadratischen Ausschnitt aus der Mitte nehmen @@ -33,7 +34,7 @@ class AdventImage: img = img.resize( size=(500, 500), - resample=Image.ANTIALIAS, + resample=Image.LANCZOS, ) # Farbmodell festlegen diff --git a/api/advent22_api/routers/_misc.py b/api/advent22_api/routers/_misc.py index 54a25eb..8437969 100644 --- a/api/advent22_api/routers/_misc.py +++ b/api/advent22_api/routers/_misc.py @@ -1,7 +1,7 @@ import itertools import random import re -from typing import Any, Sequence +from typing import Any, Self, Sequence from fastapi import Depends from PIL import ImageFont @@ -17,7 +17,7 @@ from ._image import AdventImage class Random(random.Random): @classmethod - async def get(cls, bonus_salt: Any = "") -> "Random": + async def get(cls, bonus_salt: Any = "") -> Self: cfg = await get_config() return cls(f"{cfg.puzzle.solution}{bonus_salt}") From 48f58a79539e2df05f427f17affee7b2580d6373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 3 Sep 2023 16:37:43 +0000 Subject: [PATCH 2/3] remove some deprecated values --- api/.devcontainer/devcontainer.json | 16 +++------------ api/.vscode/settings.json | 31 ++++++++++++++--------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/api/.devcontainer/devcontainer.json b/api/.devcontainer/devcontainer.json index d62dcd9..48a679e 100644 --- a/api/.devcontainer/devcontainer.json +++ b/api/.devcontainer/devcontainer.json @@ -20,25 +20,15 @@ "vscode": { // Set *default* container specific settings.json values on container create. "settings": { - "python.defaultInterpreterPath": "/usr/local/bin/python", - "python.linting.enabled": true, - "python.linting.pylintEnabled": true, - "python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8", - "python.formatting.blackPath": "/usr/local/py-utils/bin/black", - "python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf", - "python.linting.banditPath": "/usr/local/py-utils/bin/bandit", - "python.linting.flake8Path": "/usr/local/py-utils/bin/flake8", - "python.linting.mypyPath": "/usr/local/py-utils/bin/mypy", - "python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle", - "python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle", - "python.linting.pylintPath": "/usr/local/py-utils/bin/pylint" + "python.defaultInterpreterPath": "/usr/local/bin/python" }, // Add the IDs of extensions you want installed when the container is created. "extensions": [ "be5invis.toml", "mhutchie.git-graph", "ms-python.python", - "ms-python.flake8", + "ms-python.black-formatter", + // "ms-python.flake8", "ms-python.isort", "ms-python.vscode-pylance" ] diff --git a/api/.vscode/settings.json b/api/.vscode/settings.json index 3edcec7..9254a19 100644 --- a/api/.vscode/settings.json +++ b/api/.vscode/settings.json @@ -1,17 +1,16 @@ { - "python.testing.pytestArgs": [ - "tests" - ], - "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true, - "python.linting.enabled": true, - "python.linting.pylintEnabled": false, - "python.linting.flake8Enabled": true, - "python.languageServer": "Pylance", - "editor.formatOnSave": true, - "editor.codeActionsOnSave": { - "source.organizeImports": true - }, - "git.closeDiffOnOperation": true, - "python.analysis.typeCheckingMode": "basic" - } \ No newline at end of file + "python.linting.enabled": true, + "python.linting.pylintEnabled": false, + "python.linting.flake8Enabled": true, + "python.languageServer": "Pylance", + "editor.formatOnSave": true, + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "editor.codeActionsOnSave": { + "source.organizeImports": true + }, + "git.closeDiffOnOperation": true, + "python.analysis.typeCheckingMode": "basic", + "python.analysis.diagnosticMode": "workspace", +} \ No newline at end of file From 5a07ca5dfd2eff6014d19638fb7ebe9ba00ae170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 3 Sep 2023 16:44:18 +0000 Subject: [PATCH 3/3] python "black" formatting --- api/.flake8 | 4 ++++ api/advent22_api/dav_common.py | 20 +++++++++----------- api/advent22_api/routers/_image.py | 24 +++++++++++------------- api/advent22_api/routers/_misc.py | 1 + api/advent22_api/routers/abspacken.py | 8 +++++--- api/advent22_api/routers/user.py | 4 +--- 6 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 api/.flake8 diff --git a/api/.flake8 b/api/.flake8 new file mode 100644 index 0000000..e4e4892 --- /dev/null +++ b/api/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 80 +select = C,E,F,W,B,B950 +extend-ignore = E203, E501 diff --git a/api/advent22_api/dav_common.py b/api/advent22_api/dav_common.py index 30714e5..7903fe5 100644 --- a/api/advent22_api/dav_common.py +++ b/api/advent22_api/dav_common.py @@ -6,22 +6,20 @@ from webdav3.client import Client as WebDAVclient from .settings import SETTINGS -_WEBDAV_CLIENT = WebDAVclient({ - "webdav_hostname": SETTINGS.webdav.url, - "webdav_login": SETTINGS.webdav.username, - "webdav_password": SETTINGS.webdav.password, - "disable_check": SETTINGS.webdav.disable_check, -}) +_WEBDAV_CLIENT = WebDAVclient( + { + "webdav_hostname": SETTINGS.webdav.url, + "webdav_login": SETTINGS.webdav.username, + "webdav_password": SETTINGS.webdav.password, + "disable_check": SETTINGS.webdav.disable_check, + } +) @AsyncTTL(time_to_live=SETTINGS.cache_ttl) async def dav_list_files(regex: re.Pattern, directory: str = "") -> list[str]: ls = _WEBDAV_CLIENT.list(directory) - return [ - f"{directory}/{path}" - for path in ls - if regex.search(path) - ] + return [f"{directory}/{path}" for path in ls if regex.search(path)] @AsyncTTL(time_to_live=SETTINGS.cache_ttl) diff --git a/api/advent22_api/routers/_image.py b/api/advent22_api/routers/_image.py index f238321..94783c3 100644 --- a/api/advent22_api/routers/_image.py +++ b/api/advent22_api/routers/_image.py @@ -25,12 +25,14 @@ class AdventImage: square = min(width, height) # Bild zuschneiden und skalieren - img = img.crop(box=( - int((width - square)/2), - int((height - square)/2), - int((width + square)/2), - int((height + square)/2), - )) + img = img.crop( + box=( + int((width - square) / 2), + int((height - square) / 2), + int((width + square) / 2), + int((height + square) / 2), + ) + ) img = img.resize( size=(500, 500), @@ -80,7 +82,7 @@ class AdventImage: """ pixel_data = self.img.crop(box).getdata() - mean_color: np.ndarray = np.mean(pixel_data, axis=0) + mean_color: np.ndarray = np.mean(a=pixel_data, axis=0) return tuple(mean_color.astype(int).tolist()) @@ -99,11 +101,7 @@ class AdventImage: # betroffenen Bildbereich bestimmen text_box = await self.get_text_box( - xy=xy, - text=text, - font=font, - anchor=anchor, - **text_kwargs + xy=xy, text=text, font=font, anchor=anchor, **text_kwargs ) if text_box is not None: @@ -132,5 +130,5 @@ class AdventImage: font=font, fill=text_color, anchor=anchor, - **text_kwargs + **text_kwargs, ) diff --git a/api/advent22_api/routers/_misc.py b/api/advent22_api/routers/_misc.py index 8437969..97f6484 100644 --- a/api/advent22_api/routers/_misc.py +++ b/api/advent22_api/routers/_misc.py @@ -36,6 +36,7 @@ async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list: # Elemente mischen return rnd.sample(seq, len(seq)) + ######### # IMAGE # ######### diff --git a/api/advent22_api/routers/abspacken.py b/api/advent22_api/routers/abspacken.py index acb9fb4..84fbbdf 100644 --- a/api/advent22_api/routers/abspacken.py +++ b/api/advent22_api/routers/abspacken.py @@ -22,8 +22,10 @@ async def uwe( ) -> str: kurix = await get_kurix(kgs) - return f"UWE hat bei {firma} einen beachtlichen Haufen von " \ + return ( + f"UWE hat bei {firma} einen beachtlichen Haufen von " f"{kgs} Kg ({kurix:.3f} Kurix) auf den Läufer geschissen." + ) @router.get("/torsten/{ding}") @@ -41,8 +43,8 @@ async def torsten(ding: str) -> str: status.HTTP_418_IM_A_TEAPOT: { "description": "Commit Sudoku", "content": None, - } - } + }, + }, ) async def kys() -> None: """ diff --git a/api/advent22_api/routers/user.py b/api/advent22_api/routers/user.py index 0e4a466..d9f412a 100644 --- a/api/advent22_api/routers/user.py +++ b/api/advent22_api/routers/user.py @@ -13,9 +13,7 @@ async def user_is_admin( credentials: HTTPBasicCredentials = Depends(security), config: Config = Depends(get_config), ) -> bool: - username_correct = secrets.compare_digest( - credentials.username, config.admin.name - ) + username_correct = secrets.compare_digest(credentials.username, config.admin.name) password_correct = secrets.compare_digest( credentials.password, config.admin.password