Skip to content

Commit

Permalink
fix: 修复 Store 里的插件实例构造问题
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOrangeQWQ committed Aug 27, 2024
1 parent b2db73a commit 51d137b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
29 changes: 13 additions & 16 deletions src/utils/store_test/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import abc
from datetime import datetime
from typing import Any, Literal
from zoneinfo import ZoneInfo
Expand Down Expand Up @@ -26,14 +27,8 @@ def color_hex(self) -> str:
return self.color.as_hex()


class StorePlugin(BaseModel):
"""NoneBot 仓库中的插件数据"""

module_name: str
project_link: str
author: str
class TagModel(BaseModel):
tags: list[Tag]
is_official: bool

@field_validator("tags", mode="before")
@classmethod
Expand All @@ -44,6 +39,16 @@ def tags_validator(cls, v: list[dict[str, Any]]):
]


class StorePlugin(TagModel):
"""NoneBot 仓库中的插件数据"""

module_name: str
project_link: str
author: str
tags: list[Tag]
is_official: bool


class Metadata(BaseModel):
"""插件元数据"""

Expand All @@ -55,7 +60,7 @@ class Metadata(BaseModel):
supported_adapters: list[str] | None = None


class Plugin(BaseModel):
class Plugin(TagModel):
"""NoneBot 商店插件数据"""

module_name: str
Expand All @@ -82,14 +87,6 @@ def metadata(self) -> Metadata:
supported_adapters=self.supported_adapters,
)

@field_validator("tags", mode="before")
@classmethod
def tags_validator(cls, v: list[dict[str, Any]]):
return [
Tag.model_construct(label=tag["label"], color=Color(tag["color"]))
for tag in v
]


class TestResult(BaseModel):
time: str = Field(
Expand Down
14 changes: 7 additions & 7 deletions src/utils/store_test/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ async def worker():
if new_plugin:
new_plugins[key] = new_plugin

# try:
click.echo(f"{i}/{limit} 正在测试插件 {key} ...")
await worker() # TODO: 修改为并行
i += 1
# except Exception as e:
# click.echo(e)
# continue
try:
click.echo(f"{i}/{limit} 正在测试插件 {key} ...")
await worker() # TODO: 修改为并行
i += 1
except Exception as e:
click.echo(e)
continue

return new_results, new_plugins

Expand Down
10 changes: 9 additions & 1 deletion src/utils/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
Field,
ValidationInfo,
ValidatorFunctionWrapHandler,
field_serializer,
field_validator,
model_validator,
)
from pydantic_extra_types.color import Color
from pydantic_core import PydanticCustomError
from src.utils.store_test.models import Metadata, Tag


from .constants import (
NAME_MAX_LENGTH,
PLUGIN_VALID_TYPE,
Expand Down Expand Up @@ -53,6 +54,13 @@ class ValidationDict(BaseModel):
data: dict[str, Any]
errors: list[ErrorDetails]

@field_validator("data")
@classmethod
def data_validator(cls, v: dict[str, Any] | BaseModel) -> dict[str, Any]:
if isinstance(v, BaseModel):
return v.model_dump()
return v


class PyPIMixin(BaseModel):
module_name: str
Expand Down

0 comments on commit 51d137b

Please sign in to comment.