Skip to content

Commit

Permalink
feat(type): use dataclass instead of pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
JamzumSum committed Mar 11, 2023
1 parent eab2cc0 commit 2c6de32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aioqzone-feed"
version = "0.13.0.dev1"
version = "0.13.1.dev1"
description = "aioqzone plugin providing higher level api for processing feed."
authors = ["aioqzone <[email protected]>"]
license = "AGPL-3.0"
Expand Down Expand Up @@ -66,4 +66,4 @@ line_length = 99

[tool.black]
line-length = 99
target-version = ['py39']
target-version = ['py38']
46 changes: 20 additions & 26 deletions src/aioqzone_feed/type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from dataclasses import dataclass, field
from functools import singledispatchmethod
from typing import List, Optional, Union, cast
from typing import List, Optional, Union

from aioqzone.type.entity import ConEntity
from aioqzone.type.internal import LikeData
Expand All @@ -9,7 +10,6 @@
from aioqzone.utils.entity import split_entities
from aioqzone.utils.html import HtmlContent, HtmlInfo
from aioqzone.utils.time import approx_ts
from pydantic import BaseModel, Field, HttpUrl
from typing_extensions import Self

if sys.version_info < (3, 9):
Expand All @@ -25,15 +25,13 @@ def _register(self, cls, method=None):
singledispatchmethod.register = _register


class VisualMedia(BaseModel):
@dataclass
class VisualMedia:
height: int
width: int
thumbnail: Optional[HttpUrl] = None
raw: HttpUrl
raw: str
is_video: bool

class Config:
keep_untouched = (singledispatchmethod,)
thumbnail: Optional[str] = None

@singledispatchmethod
def from_pic(cls, pic):
Expand All @@ -49,8 +47,8 @@ def _(cls, pic: PicRep):
return cls(
height=pic.height,
width=pic.width,
thumbnail=cast(HttpUrl, pic.thumb),
raw=cast(HttpUrl, pic.raw),
thumbnail=pic.thumb,
raw=pic.raw,
is_video=False,
)

Expand Down Expand Up @@ -99,11 +97,12 @@ def _(cls, video: FeedVideo):
)


class BaseFeed(BaseModel):
@dataclass
class BaseFeed:
"""FeedModel is a model for storing a feed, with the info to hashing and retrieving the feed."""

appid: int
typeid: int = 0
typeid: int
fid: str
"""Feed id, a hex string with 24/32 chars, or a much shorter placeholder.
Expand All @@ -117,9 +116,9 @@ class BaseFeed(BaseModel):
"""Feed owner uin. (hostuin)"""
nickname: str
"""Feed owner nickname."""
curkey: Optional[Union[HttpUrl, str]] = None
curkey: Optional[str] = None
"""The identifier to this feed. May be a url, or just a identifier string."""
unikey: Optional[Union[HttpUrl, str]] = None
unikey: Optional[str] = None
"""The identifier to the original content. May be a url in all kinds
(sometimes not strictly in a correct format, but it is from the meaning)"""
topicId: str = ""
Expand All @@ -129,10 +128,6 @@ class BaseFeed(BaseModel):
.. versionadded:: 0.9.2a1
"""

class Config:
orm_mode = True
keep_untouched = (singledispatchmethod,)

def __hash__(self) -> int:
return hash((self.uin, self.abstime))

Expand Down Expand Up @@ -189,14 +184,12 @@ def _(cls, obj: FeedData, **kwds):
)


class BaseDetail(BaseModel):
entities: List[ConEntity] = Field(default_factory=list)
forward: Union[HttpUrl, str, BaseFeed, None] = None
@dataclass
class BaseDetail:
entities: List[ConEntity] = field(default_factory=list)
forward: Union["FeedContent", str, None] = None
"""unikey to the feed, or the content itself."""
media: List[VisualMedia] = Field(default_factory=list)

class Config:
keep_untouched = (singledispatchmethod,)
media: List[VisualMedia] = field(default_factory=list)

@singledispatchmethod
def set_detail(self, obj) -> None:
Expand Down Expand Up @@ -275,7 +268,8 @@ def set_fromhtml(self, obj: HtmlContent):
self.media = [VisualMedia.from_pic(i) for i in obj.pic]


class FeedContent(BaseFeed, BaseDetail):
@dataclass
class FeedContent(BaseDetail, BaseFeed):
"""FeedContent is feed with contents. This might be the common structure to
represent a feed as what it's known."""

Expand Down

0 comments on commit 2c6de32

Please sign in to comment.