Skip to content

Commit

Permalink
fix: make enum.IntEnum work with pymysql
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Sep 21, 2024
1 parent 84c29c5 commit dd8ffb0
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 46 deletions.
45 changes: 42 additions & 3 deletions chii/db/const.py → chii/const/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import enum
from typing import Dict, NamedTuple

from chii.db._const import (
from chii.const._const import (
Staff,
staff_job_anime,
staff_job_book,
staff_job_game,
staff_job_music,
staff_job_real,
)
from chii.subject import SubjectType


class IntEnum(enum.IntEnum):
class IntEnum(enum.IntEnum): # noqa: TID251
"""helper class to make enum.IntEnum work with sqlalchemy and pymysql"""

def __str__(self):
"""
https://github.com/PyMySQL/PyMySQL/blob/ec27bade879ad05fda214188d035c1fe3f255a35/pymysql/converters.py#L49-L50
"""
return str(self.value)

def translate(self, _escape_table):
"""sqlalchemy method called inside pymysql or aiomysql to get real value,
so you can use `Table.column == SubjectType.book`
Expand All @@ -22,6 +29,38 @@ def translate(self, _escape_table):
return self.value


@enum.unique
class SubjectType(IntEnum):
"""条目类型
- `1` 为 书籍
- `2` 为 动画
- `3` 为 音乐
- `4` 为 游戏
- `6` 为 三次元
没有 `5`
"""

book = 1
anime = 2
music = 3
game = 4
real = 6

def str(self) -> str:
if self == self.book:
return "书籍"
if self == self.anime:
return "动画"
if self == self.music:
return "音乐"
if self == self.game:
return "游戏"
if self == self.real:
return "三次元"
raise ValueError(f"unexpected SubjectType {self}")


class BloodType(IntEnum):
a = 1
b = 2
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion chii/db/const_test.py → chii/const/const_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from chii.db.const import Gender, StaffMap
from chii.const.__init__ import Gender, StaffMap


def test_valid_type():
Expand Down
4 changes: 1 addition & 3 deletions chii/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class ChiiTimeline:
)
}
)
cat: int = field(
metadata={"sa": Column("tml_cat", SMALLINT(6), nullable=False, index=True)}
)
cat: int = field(metadata={"sa": Column("tml_cat", SMALLINT(6), nullable=False)})
type: int = field(
metadata={
"sa": Column(
Expand Down
5 changes: 3 additions & 2 deletions chii/permission/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import enum
from typing import Optional

from chii.const.__init__ import IntEnum

class UserGroup(enum.IntEnum):

class UserGroup(IntEnum):
admin = 1 # 管理员
bangumi_admin = 2 # Bangumi 管理猿
window_admin = 3 # 天窗管理猿
Expand Down
33 changes: 0 additions & 33 deletions chii/subject.py

This file was deleted.

3 changes: 1 addition & 2 deletions chii/timeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from pydantic import BaseModel

from chii.db.const import CollectionType, IntEnum
from chii.subject import SubjectType
from chii.const import CollectionType, IntEnum, SubjectType


class SubjectMemo(BaseModel):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ ignore = [
'PLR2004',
'PGH003',
]

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"enum.IntEnum".msg = "Always use chii.const.IntEnum"
3 changes: 1 addition & 2 deletions rpc/timeline_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from sqlalchemy import select

from api.v1.timeline_pb2 import HelloRequest
from chii.const import CollectionType, SubjectType
from chii.db import sa
from chii.db.const import CollectionType
from chii.db.tables import ChiiTimeline, ChiiTimeline_column_id, ChiiTimeline_column_uid
from chii.subject import SubjectType
from chii.timeline import SUBJECT_TYPE_MAP, TimelineCat
from rpc.timeline_service import TimeLineService

Expand Down

0 comments on commit dd8ffb0

Please sign in to comment.