home
·
contact
·
privacy
projects
/
ytplom
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
50a56e0
)
Greatly simplify template context type checking.
master
author
Christian Heller
<c.heller@plomlompom.de>
Fri, 6 Dec 2024 16:17:05 +0000
(17:17 +0100)
committer
Christian Heller
<c.heller@plomlompom.de>
Fri, 6 Dec 2024 16:17:05 +0000
(17:17 +0100)
src/ytplom/http.py
patch
|
blob
|
history
diff --git
a/src/ytplom/http.py
b/src/ytplom/http.py
index 1ea2bc202e30fa84fb966fcf6f251972c6097d34..f378da3aeeae902a8aeef647aec1d21d2cd7bd04 100644
(file)
--- 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 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 (
- Fil
esWithIndex, FilterStr, FlagName, PlayerUpdateId, QueryId
,
-
QueryText, QuotaCost, Tag, UrlStr,
YoutubeId,
+ Fil
terStr, 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'))