From b8834cb70d9126a7d7a598001557edda27d30bef Mon Sep 17 00:00:00 2001 From: jstzwj <1103870790@qq.com> Date: Mon, 14 Oct 2024 03:50:02 +0800 Subject: [PATCH] updates --- olah/configs.py | 14 +++++++------- olah/mirror/meta.py | 2 +- olah/mirror/repos.py | 4 ++-- olah/proxy/meta.py | 10 +++++----- olah/proxy/pathsinfo.py | 8 ++++---- olah/proxy/tree.py | 8 ++++---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/olah/configs.py b/olah/configs.py index c979fae..eb1b640 100644 --- a/olah/configs.py +++ b/olah/configs.py @@ -24,14 +24,14 @@ class OlahRule(object): - def __init__(self) -> None: - self.repo = "" - self.type = "*" - self.allow = False - self.use_re = False + def __init__(self, repo: str = "", type: str = "*", allow: bool = False, use_re: bool = False) -> None: + self.repo = repo + self.type = type + self.allow = allow + self.use_re = use_re @staticmethod - def from_dict(data) -> "OlahRule": + def from_dict(data: Dict[str, Any]) -> "OlahRule": out = OlahRule() if "repo" in data: out.repo = data["repo"] @@ -59,7 +59,7 @@ def __init__(self) -> None: self.rules: List[OlahRule] = [] @staticmethod - def from_list(data) -> "OlahRuleList": + def from_list(data: List[Dict[str, Any]]) -> "OlahRuleList": out = OlahRuleList() for item in data: out.rules.append(OlahRule.from_dict(item)) diff --git a/olah/mirror/meta.py b/olah/mirror/meta.py index 5c03ba8..e099df5 100644 --- a/olah/mirror/meta.py +++ b/olah/mirror/meta.py @@ -25,7 +25,7 @@ def __init__(self) -> None: self.siblings = None self.createdAt = None - def to_dict(self): + def to_dict(self) -> Dict[str, Any]: return { "_id": self._id, "id": self.id, diff --git a/olah/mirror/repos.py b/olah/mirror/repos.py index 5ada704..0122dfd 100644 --- a/olah/mirror/repos.py +++ b/olah/mirror/repos.py @@ -66,7 +66,7 @@ def _get_description(self, commit: Commit) -> str: readme = self._get_readme(commit) return self._remove_card(readme) - def _get_tree_filepaths_recursive(self, tree, include_dir=False) -> List[str]: + def _get_tree_filepaths_recursive(self, tree: Tree, include_dir: bool = False) -> List[str]: out_paths = [] for entry in tree: if entry.type == "tree": @@ -80,7 +80,7 @@ def _get_tree_filepaths_recursive(self, tree, include_dir=False) -> List[str]: def _get_commit_filepaths_recursive(self, commit: Commit) -> List[str]: return self._get_tree_filepaths_recursive(commit.tree) - def _get_path_info(self, entry: IndexObjUnion, expand: bool=False) -> Dict[str, Union[int, str]]: + def _get_path_info(self, entry: IndexObjUnion, expand: bool = False) -> Dict[str, Union[int, str]]: lfs = False if entry.type != "tree": t = "file" diff --git a/olah/proxy/meta.py b/olah/proxy/meta.py index 6f0f904..bcb6a7d 100644 --- a/olah/proxy/meta.py +++ b/olah/proxy/meta.py @@ -8,7 +8,7 @@ import os import shutil import tempfile -from typing import Dict, Literal, Optional +from typing import Dict, Literal, Optional, AsyncGenerator, Union from urllib.parse import urljoin from fastapi import FastAPI, Request @@ -20,7 +20,7 @@ from olah.utils.repo_utils import get_org_repo from olah.utils.file_utils import make_dirs -async def _meta_cache_generator(save_path: str): +async def _meta_cache_generator(save_path: str) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: cache_rq = await read_cache_request(save_path) yield cache_rq["headers"] yield cache_rq["content"] @@ -33,7 +33,7 @@ async def _meta_proxy_generator( method: str, allow_cache: bool, save_path: str, -): +) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: async with httpx.AsyncClient(follow_redirects=True) as client: content_chunks = [] async with client.stream( @@ -61,7 +61,7 @@ async def _meta_proxy_generator( save_path, response_status_code, response_headers, bytes(content) ) -# TODO: remove param `request` + async def meta_generator( app: FastAPI, repo_type: Literal["models", "datasets", "spaces"], @@ -71,7 +71,7 @@ async def meta_generator( override_cache: bool, method: str, authorization: Optional[str], -): +) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: headers = {} if authorization is not None: headers["authorization"] = authorization diff --git a/olah/proxy/pathsinfo.py b/olah/proxy/pathsinfo.py index 1e9115e..e705c6f 100644 --- a/olah/proxy/pathsinfo.py +++ b/olah/proxy/pathsinfo.py @@ -7,7 +7,7 @@ import json import os -from typing import Dict, List, Literal, Optional +from typing import Dict, List, Literal, Optional, Tuple from urllib.parse import quote, urljoin from fastapi import FastAPI, Request @@ -20,7 +20,7 @@ from olah.utils.file_utils import make_dirs -async def _pathsinfo_cache(save_path: str): +async def _pathsinfo_cache(save_path: str) -> Tuple[int, Dict[str, str], bytes]: cache_rq = await read_cache_request(save_path) return cache_rq["status_code"], cache_rq["headers"], cache_rq["content"] @@ -33,7 +33,7 @@ async def _pathsinfo_proxy( path: str, allow_cache: bool, save_path: str, -): +) -> Tuple[int, Dict[str, str], bytes]: headers = {k: v for k, v in headers.items()} if "content-length" in headers: headers.pop("content-length") @@ -67,7 +67,7 @@ async def pathsinfo_generator( override_cache: bool, method: str, authorization: Optional[str], -): +) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: headers = {} if authorization is not None: headers["authorization"] = authorization diff --git a/olah/proxy/tree.py b/olah/proxy/tree.py index 22e4467..92eb338 100644 --- a/olah/proxy/tree.py +++ b/olah/proxy/tree.py @@ -6,7 +6,7 @@ # https://opensource.org/licenses/MIT. import os -from typing import Dict, Literal, Mapping, Optional +from typing import Dict, Literal, Mapping, Optional, AsyncGenerator, Union from urllib.parse import urljoin from fastapi import FastAPI, Request @@ -19,7 +19,7 @@ from olah.utils.file_utils import make_dirs -async def _tree_cache_generator(save_path: str): +async def _tree_cache_generator(save_path: str) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: cache_rq = await read_cache_request(save_path) yield cache_rq["status_code"] yield cache_rq["headers"] @@ -33,7 +33,7 @@ async def _tree_proxy_generator( params: Mapping[str, str], allow_cache: bool, save_path: str, -): +) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: async with httpx.AsyncClient(follow_redirects=True) as client: content_chunks = [] async with client.stream( @@ -77,7 +77,7 @@ async def tree_generator( override_cache: bool, method: str, authorization: Optional[str], -): +) -> AsyncGenerator[Union[int, Dict[str, str], bytes], None]: headers = {} if authorization is not None: headers["authorization"] = authorization