From: Christian Heller Date: Sat, 30 Nov 2024 18:44:13 +0000 (+0100) Subject: In http module, mark with underscore constants not used outside. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/ledger?a=commitdiff_plain;h=f61cd9644a685ccf054b8334d41308d71be19c35;p=ytplom In http module, mark with underscore constants not used outside. --- diff --git a/src/ytplom/http.py b/src/ytplom/http.py index cd66699..c1c427a 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -19,32 +19,32 @@ from ytplom.misc import ( ) # type definitions for mypy -PageNames: TypeAlias = dict[str, Path] -ParamsStr = NewType('ParamsStr', str) -TemplateContext: TypeAlias = dict[ +_PageNames: TypeAlias = dict[str, Path] +_ParamsStr = NewType('_ParamsStr', str) +_TemplateContext: TypeAlias = dict[ str, None | bool - | FilesWithIndex | PageNames | ParamsStr | Path | PlayerUpdateId + | FilesWithIndex | _PageNames | _ParamsStr | Path | PlayerUpdateId | QueryText | QuotaCost | UrlStr | 'VideoFile' | YoutubeId | 'YoutubeVideo' | list[FlagName] | list['VideoFile'] | list['YoutubeVideo'] | list['YoutubeQuery'] ] # API expectations -THUMBNAIL_URL_PREFIX = UrlStr('https://i.ytimg.com/vi/') -THUMBNAIL_URL_SUFFIX = UrlStr('/default.jpg') +_THUMBNAIL_URL_PREFIX = UrlStr('https://i.ytimg.com/vi/') +_THUMBNAIL_URL_SUFFIX = UrlStr('/default.jpg') # template paths -PATH_TEMPLATES = PATH_APP_DATA.joinpath('templates') -NAME_TEMPLATE_QUERIES = Path('yt_queries.tmpl') -NAME_TEMPLATE_RESULTS = Path('yt_results.tmpl') -NAME_TEMPLATE_FILES = Path('files.tmpl') -NAME_TEMPLATE_FILE_DATA = Path('file_data.tmpl') -NAME_TEMPLATE_YT_VIDEO = Path('yt_result.tmpl') -NAME_TEMPLATE_PLAYLIST = Path('playlist.tmpl') +_PATH_TEMPLATES = PATH_APP_DATA.joinpath('templates') +_NAME_TEMPLATE_QUERIES = Path('yt_queries.tmpl') +_NAME_TEMPLATE_RESULTS = Path('yt_results.tmpl') +_NAME_TEMPLATE_FILES = Path('files.tmpl') +_NAME_TEMPLATE_FILE_DATA = Path('file_data.tmpl') +_NAME_TEMPLATE_YT_VIDEO = Path('yt_result.tmpl') +_NAME_TEMPLATE_PLAYLIST = Path('playlist.tmpl') # page names -PAGE_NAMES: PageNames = { +PAGE_NAMES: _PageNames = { 'download': Path('dl'), 'file': Path('file'), 'files': Path('files'), @@ -65,7 +65,7 @@ class Server(HTTPServer): super().__init__((config.host, config.port), _TaskHandler, *args, **kwargs) self.config = config - self.jinja = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES)) + self.jinja = JinjaEnv(loader=JinjaFSLoader(_PATH_TEMPLATES)) self.player = Player() self.downloads = DownloadsManager() self.downloads.clean_unfinished() @@ -172,7 +172,7 @@ class _TaskHandler(BaseHTTPRequestHandler): self._send_or_download_video(YoutubeId(toks_url[2])) elif PAGE_NAMES['files'] == page_name: params = parse_qs(url.query) - filter_ = ParamsStr(params.get('filter', [''])[0]) + filter_ = _ParamsStr(params.get('filter', [''])[0]) show_absent = params.get('show_absent', [False])[0] self._send_files_index(filter_, bool(show_absent)) elif PAGE_NAMES['file'] == page_name: @@ -194,7 +194,7 @@ class _TaskHandler(BaseHTTPRequestHandler): def _send_rendered_template(self, tmpl_name: Path, - tmpl_ctx: TemplateContext + tmpl_ctx: _TemplateContext ) -> None: tmpl = self.server.jinja.get_template(str(tmpl_name)) tmpl_ctx['page_names'] = PAGE_NAMES @@ -206,7 +206,7 @@ class _TaskHandler(BaseHTTPRequestHandler): path_thumbnail = PATH_THUMBNAILS.joinpath(filename) if not path_thumbnail.exists(): video_id = filename.stem - url = f'{THUMBNAIL_URL_PREFIX}{video_id}{THUMBNAIL_URL_SUFFIX}' + url = f'{_THUMBNAIL_URL_PREFIX}{video_id}{_THUMBNAIL_URL_SUFFIX}' try: urlretrieve(url, PATH_THUMBNAILS.joinpath(f'{video_id}.jpg')) except HTTPError as e: @@ -239,7 +239,7 @@ class _TaskHandler(BaseHTTPRequestHandler): results = YoutubeVideo.get_all_for_query(conn, query_id) conn.commit_close() self._send_rendered_template( - NAME_TEMPLATE_RESULTS, + _NAME_TEMPLATE_RESULTS, {'query': query.text, 'videos': results}) def _send_yt_queries_index_and_search(self) -> None: @@ -249,8 +249,8 @@ class _TaskHandler(BaseHTTPRequestHandler): conn.commit_close() queries_data.sort(key=lambda q: q.retrieved_at, reverse=True) self._send_rendered_template( - NAME_TEMPLATE_QUERIES, {'queries': queries_data, - 'quota_count': quota_count}) + _NAME_TEMPLATE_QUERIES, + {'queries': queries_data, 'quota_count': quota_count}) def _send_yt_result(self, video_id: YoutubeId) -> None: conn = DbConnection() @@ -266,7 +266,7 @@ class _TaskHandler(BaseHTTPRequestHandler): file_path = None conn.commit_close() self._send_rendered_template( - NAME_TEMPLATE_YT_VIDEO, + _NAME_TEMPLATE_YT_VIDEO, {'video_data': video_data, 'is_temp': video_id in self.server.downloads.ids_unfinished, 'file_path': file_path, @@ -278,10 +278,13 @@ class _TaskHandler(BaseHTTPRequestHandler): file = VideoFile.get_by_b64(conn, rel_path_b64) conn.commit_close() self._send_rendered_template( - NAME_TEMPLATE_FILE_DATA, + _NAME_TEMPLATE_FILE_DATA, {'file': file, 'flag_names': list(FILE_FLAGS)}) - def _send_files_index(self, filter_: ParamsStr, show_absent: bool) -> None: + def _send_files_index(self, + filter_: _ParamsStr, + show_absent: bool + ) -> None: conn = DbConnection() files = [f for f in VideoFile.get_all(conn) if filter_.lower() in str(f.rel_path).lower() @@ -289,7 +292,7 @@ class _TaskHandler(BaseHTTPRequestHandler): conn.commit_close() files.sort(key=lambda t: t.rel_path) self._send_rendered_template( - NAME_TEMPLATE_FILES, + _NAME_TEMPLATE_FILES, {'files': files, 'filter': filter_, 'show_absent': show_absent}) @@ -310,7 +313,7 @@ class _TaskHandler(BaseHTTPRequestHandler): if self.server.player.empty: self.server.player.load_files() self._send_rendered_template( - NAME_TEMPLATE_PLAYLIST, + _NAME_TEMPLATE_PLAYLIST, {'last_update': self.server.player.last_update, 'running': self.server.player.is_running, 'paused': self.server.player.is_paused,