From: Christian Heller Date: Fri, 6 Dec 2024 16:17:05 +0000 (+0100) Subject: Greatly simplify template context type checking. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdb.prefix%7D%7D/%7B%7Byoutube_prefix%7D%7D%7B%7Bresult.id%7D%7D?a=commitdiff_plain;h=8131efeaef19c26d311f37ba99ee66ac904a512f;p=ytplom Greatly simplify template context type checking. --- diff --git a/src/ytplom/http.py b/src/ytplom/http.py index 1ea2bc2..f378da3 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -3,7 +3,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler from json import dumps as json_dumps from pathlib import Path from time import sleep -from typing import Generator, NewType, Optional, TypeAlias +from typing import Any, Generator, Optional, TypeAlias from urllib.parse import parse_qs, urlparse from urllib.request import urlretrieve from urllib.error import HTTPError @@ -11,8 +11,8 @@ from jinja2 import ( # type: ignore Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) from ytplom.db import Hash, DbConn from ytplom.misc import ( - FilesWithIndex, FilterStr, FlagName, PlayerUpdateId, QueryId, - QueryText, QuotaCost, Tag, UrlStr, YoutubeId, + FilterStr, FlagName, PlayerUpdateId, QueryId, QueryText, Tag, UrlStr, + YoutubeId, FILE_FLAGS, PATH_THUMBNAILS, YOUTUBE_URL_PREFIX, ensure_expected_dirs, Config, DownloadsManager, Player, QuotaLog, VideoFile, YoutubeQuery, @@ -22,18 +22,9 @@ from ytplom.primitives import NotFoundException, PATH_APP_DATA # type definitions for mypy -_ColorStr = NewType('_ColorStr', str) _PageNames: TypeAlias = dict[str, Path] _ReqDict: TypeAlias = dict[str, list[str]] -_PlayerState: TypeAlias = dict[str, bool | Path | str] -_TemplateContext: TypeAlias = dict[ - str, - None | bool - | _ColorStr | FilesWithIndex | _PlayerState | _PageNames | FilterStr - | Path | PlayerUpdateId | QueryText | QuotaCost | UrlStr | 'VideoFile' - | YoutubeId | 'YoutubeVideo' | list[FlagName] | set['Tag'] - | list['VideoFile'] | list['YoutubeVideo'] | list['YoutubeQuery'] -] +_TemplateContext: TypeAlias = dict[str, Any] # API expectations _THUMBNAIL_URL_PREFIX = UrlStr('https://i.ytimg.com/vi/') @@ -230,16 +221,15 @@ class _TaskHandler(BaseHTTPRequestHandler): tmpl_ctx: _TemplateContext ) -> None: tmpl = self.server.jinja.get_template(str(tmpl_name)) - tmpl_ctx['redir_target'] = Path(self.path) - tmpl_ctx['background_color'] = _ColorStr( - self.server.config.background_color) + tmpl_ctx['redir_target'] = self.path + tmpl_ctx['background_color'] = self.server.config.background_color tmpl_ctx['page_names'] = PAGE_NAMES tmpl_ctx['player_state'] = { - 'running': self.server.player.is_running, - 'paused': self.server.player.is_paused, - 'title': (self.server.player.current_file_cached.rel_path - if self.server.player.current_file_cached - else 'none')} + 'running': self.server.player.is_running, + 'paused': self.server.player.is_paused, + 'title': (self.server.player.current_file_cached.rel_path + if self.server.player.current_file_cached + else 'none')} html = tmpl.render(**tmpl_ctx) self._send_http(bytes(html, 'utf8'))