From: Christian Heller Date: Tue, 10 Dec 2024 22:06:02 +0000 (+0100) Subject: Reduce overkill in type definitions. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/error?a=commitdiff_plain;h=90ff7a0dfe73c86bbaed1ecf943d1de5811fb503;p=ytplom Reduce overkill in type definitions. --- diff --git a/src/ytplom/http.py b/src/ytplom/http.py index e726b7c..e6b2775 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -3,7 +3,7 @@ from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler from json import dumps as json_dumps, loads as json_loads from pathlib import Path from time import sleep -from typing import Any, Generator, Optional, TypeAlias +from typing import Any, Generator, Optional from urllib.parse import parse_qs, urlparse from urllib.request import urlretrieve from urllib.error import HTTPError @@ -11,7 +11,7 @@ from jinja2 import ( # type: ignore Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) from ytplom.db import Hash, DbConn from ytplom.misc import ( - FilterStr, FlagName, QueryId, QueryText, Tag, UrlStr, YoutubeId, + FilterStr, FlagName, QueryId, QueryText, Tag, YoutubeId, FILE_FLAGS, PATH_THUMBNAILS, YOUTUBE_URL_PREFIX, ensure_expected_dirs, Config, DownloadsManager, Player, QuotaLog, VideoFile, YoutubeQuery, @@ -19,14 +19,9 @@ from ytplom.misc import ( ) from ytplom.primitives import NotFoundException, PATH_APP_DATA -# type definitions for mypy -_PageNames: TypeAlias = dict[str, Path] -_ReqDict: TypeAlias = dict[str, list[str]] -_TemplateContext: TypeAlias = dict[str, Any] - # API expectations -_THUMBNAIL_URL_PREFIX = UrlStr('https://i.ytimg.com/vi/') -_THUMBNAIL_URL_SUFFIX = UrlStr('/default.jpg') +_THUMBNAIL_URL_PREFIX = 'https://i.ytimg.com/vi/' +_THUMBNAIL_URL_SUFFIX = '/default.jpg' # template paths _PATH_TEMPLATES = PATH_APP_DATA.joinpath('templates') @@ -38,7 +33,7 @@ _NAME_TEMPLATE_YT_VIDEO = Path('yt_result.tmpl') _NAME_TEMPLATE_PLAYLIST = Path('playlist.tmpl') # page names -PAGE_NAMES: _PageNames = { +PAGE_NAMES: dict[str, Path] = { 'download': Path('dl'), 'events': Path('events'), 'file': Path('file'), @@ -65,7 +60,7 @@ class _ReqMap: self.as_str = map_as_str @property - def as_dict(self) -> _ReqDict: + def as_dict(self) -> dict[str, list[str]]: """Return parsed to dictionary.""" if self.is_json: return json_loads(self.as_str) @@ -225,7 +220,7 @@ class _TaskHandler(BaseHTTPRequestHandler): def _send_rendered_template(self, tmpl_name: Path, - tmpl_ctx: _TemplateContext + tmpl_ctx: dict[str, Any] ) -> None: tmpl = self.server.jinja.get_template(str(tmpl_name)) tmpl_ctx['selected'] = tmpl_ctx.get('selected', '') diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 2b73b99..5848159 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -1,7 +1,7 @@ """Main ytplom lib.""" # included libs -from typing import NewType, Optional, Self, TypeAlias +from typing import NewType, Optional, Self from os import chdir, environ from random import shuffle from time import sleep @@ -42,9 +42,6 @@ FilterStr = NewType('FilterStr', str) FlagName = NewType('FlagName', str) FlagsInt = NewType('FlagsInt', int) Tag = NewType('Tag', str) -AmountDownloads = NewType('AmountDownloads', int) -UrlStr = NewType('UrlStr', str) -FilesWithIndex: TypeAlias = list[tuple[int, 'VideoFile']] # major expected directories PATH_DOWNLOADS = Path.home().joinpath('ytplom_downloads') @@ -61,7 +58,7 @@ YT_DL_PARAMS = {'paths': {'home': str(PATH_DOWNLOADS), 'format': YT_DOWNLOAD_FORMAT} # Youtube API expectations -YOUTUBE_URL_PREFIX = UrlStr('https://www.youtube.com/watch?v=') +YOUTUBE_URL_PREFIX = 'https://www.youtube.com/watch?v=' QUOTA_COST_YOUTUBE_SEARCH = QuotaCost(100) QUOTA_COST_YOUTUBE_DETAILS = QuotaCost(1)