home · contact · privacy
Greatly simplify template context type checking. master
authorChristian Heller <c.heller@plomlompom.de>
Fri, 6 Dec 2024 16:17:05 +0000 (17:17 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 6 Dec 2024 16:17:05 +0000 (17:17 +0100)
src/ytplom/http.py

index 1ea2bc202e30fa84fb966fcf6f251972c6097d34..f378da3aeeae902a8aeef647aec1d21d2cd7bd04 100644 (file)
@@ -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 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
 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 (
         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,
         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
 
 
 # type definitions for mypy
 
-_ColorStr = NewType('_ColorStr', str)
 _PageNames: TypeAlias = dict[str, Path]
 _ReqDict: TypeAlias = dict[str, list[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/')
 
 # 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: _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'] = {
         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'))
 
         html = tmpl.render(**tmpl_ctx)
         self._send_http(bytes(html, 'utf8'))