🎨 采取Sourcery建议

This commit is contained in:
AkashiCoin 2024-09-03 16:43:53 +08:00
parent 4707dfc08c
commit 4eaacb9db0
2 changed files with 22 additions and 19 deletions

View File

@ -11,9 +11,8 @@ from zhenxun.models.plugin_info import PluginInfo
from zhenxun.utils.image_utils import RowStyle, BuildImage, ImageTemplate from zhenxun.utils.image_utils import RowStyle, BuildImage, ImageTemplate
from zhenxun.builtin_plugins.auto_update.config import REQ_TXT_FILE_STRING from zhenxun.builtin_plugins.auto_update.config import REQ_TXT_FILE_STRING
from zhenxun.builtin_plugins.plugin_store.models import ( from zhenxun.builtin_plugins.plugin_store.models import (
FileInfo, BaseAPI,
RepoInfo, RepoInfo,
TreesInfo,
PackageApi, PackageApi,
StorePluginInfo, StorePluginInfo,
) )
@ -214,7 +213,7 @@ class ShopManage:
): ):
package_api: PackageApi package_api: PackageApi
files: list[str] files: list[str]
package_info: FileInfo | TreesInfo package_info: BaseAPI
repo_info = RepoInfo.parse_github_url(github_url) repo_info = RepoInfo.parse_github_url(github_url)
logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理") logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理")
for package_api in PackageApi: for package_api in PackageApi:

View File

@ -63,6 +63,10 @@ class RepoInfo(BaseModel):
@classmethod @classmethod
@cached() @cached()
async def get_fastest_format(cls) -> str: async def get_fastest_format(cls) -> str:
return await cls._get_fastest_format()
@classmethod
async def _get_fastest_format(cls) -> str:
"""获取最快下载地址格式""" """获取最快下载地址格式"""
raw_format = "https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}" raw_format = "https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}"
patterns: dict[str, str] = { patterns: dict[str, str] = {
@ -90,24 +94,24 @@ class FileType(StrEnum):
PACKAGE = "gh" PACKAGE = "gh"
class BaseInfo(BaseModel, ABC): class BaseAPI(BaseModel, ABC):
"""基础信息类""" """基础接口"""
@classmethod @classmethod
@abstractmethod @abstractmethod
@cached(ttl=CACHED_API_TTL) @cached(ttl=CACHED_API_TTL)
async def parse_repo_info(cls, repo_info: RepoInfo) -> "BaseInfo": ... async def parse_repo_info(cls, repo_info: RepoInfo) -> "BaseAPI": ...
@abstractmethod @abstractmethod
def get_files(cls, module_path: str, is_dir) -> list[str]: ... def get_files(cls, module_path: str, is_dir) -> list[str]: ...
class FileInfo(BaseInfo): class JsdelivrAPI(BaseAPI):
"""文件信息""" """jsdelivr接口"""
type: FileType type: FileType
name: str name: str
files: list["FileInfo"] = [] files: list["JsdelivrAPI"] = []
def recurrence_files(self, dir_path: str, is_dir: bool = True) -> list[str]: def recurrence_files(self, dir_path: str, is_dir: bool = True) -> list[str]:
""" """
@ -136,7 +140,7 @@ class FileInfo(BaseInfo):
paths.append(dir_path) paths.append(dir_path)
return paths return paths
def full_files_path(self, module_path: str, is_dir: bool = True) -> "FileInfo": def full_files_path(self, module_path: str, is_dir: bool = True) -> "JsdelivrAPI":
""" """
获取文件路径 获取文件路径
@ -150,7 +154,7 @@ class FileInfo(BaseInfo):
paths: list[str] = module_path.split("/") paths: list[str] = module_path.split("/")
if not is_dir: if not is_dir:
paths = paths[:-1] paths = paths[:-1]
cur_file: FileInfo = self cur_file: JsdelivrAPI = self
for path in paths: for path in paths:
for file in cur_file.files: for file in cur_file.files:
@ -163,7 +167,7 @@ class FileInfo(BaseInfo):
@classmethod @classmethod
@cached(ttl=CACHED_API_TTL) @cached(ttl=CACHED_API_TTL)
async def parse_repo_info(cls, repo_info: RepoInfo) -> "FileInfo": async def parse_repo_info(cls, repo_info: RepoInfo) -> "JsdelivrAPI":
"""解析仓库信息""" """解析仓库信息"""
"""获取插件包信息 """获取插件包信息
@ -180,7 +184,7 @@ class FileInfo(BaseInfo):
res = await AsyncHttpx.get(url=jsd_package_url) res = await AsyncHttpx.get(url=jsd_package_url)
if res.status_code != 200: if res.status_code != 200:
raise ValueError(f"下载错误, code: {res.status_code}") raise ValueError(f"下载错误, code: {res.status_code}")
return FileInfo(**res.json()) return JsdelivrAPI(**res.json())
def get_files(self, module_path: str, is_dir: bool = True) -> list[str]: def get_files(self, module_path: str, is_dir: bool = True) -> list[str]:
"""获取文件路径""" """获取文件路径"""
@ -211,8 +215,8 @@ class Tree(BaseModel):
url: str url: str
class TreesInfo(BaseInfo): class GitHubAPI(BaseAPI):
"""树信息""" """github接口"""
sha: str sha: str
url: str url: str
@ -228,7 +232,7 @@ class TreesInfo(BaseInfo):
@classmethod @classmethod
@cached(ttl=CACHED_API_TTL) @cached(ttl=CACHED_API_TTL)
async def parse_repo_info(cls, repo_info: RepoInfo) -> "TreesInfo": async def parse_repo_info(cls, repo_info: RepoInfo) -> "GitHubAPI":
"""获取仓库树 """获取仓库树
参数: 参数:
@ -243,7 +247,7 @@ class TreesInfo(BaseInfo):
res = await AsyncHttpx.get(url=git_tree_url) res = await AsyncHttpx.get(url=git_tree_url)
if res.status_code != 200: if res.status_code != 200:
raise ValueError(f"下载错误, code: {res.status_code}") raise ValueError(f"下载错误, code: {res.status_code}")
return TreesInfo(**res.json()) return GitHubAPI(**res.json())
def get_files(self, module_path: str, is_dir: bool = True) -> list[str]: def get_files(self, module_path: str, is_dir: bool = True) -> list[str]:
"""获取文件路径""" """获取文件路径"""
@ -253,5 +257,5 @@ class TreesInfo(BaseInfo):
class PackageApi(Enum): class PackageApi(Enum):
"""插件包接口""" """插件包接口"""
GITHUB = TreesInfo GITHUB = GitHubAPI
JSDELIVR = FileInfo JSDELIVR = JsdelivrAPI