mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-01 21:07:01 +08:00
refactor(types): declare media server model query results
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any, List, Optional, Self
|
||||
|
||||
from sqlalchemy import JSON, Index, Select, String, delete, or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -51,17 +51,21 @@ class MediaServerItem(Base):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_by_itemid(cls, db: Session, item_id: str):
|
||||
return db.execute(select(cls).where(cls.item_id == item_id)).scalars().first()
|
||||
def get_by_itemid(cls, db: Session, item_id: str) -> Optional[Self]:
|
||||
"""按媒体条目 ID 查询首条记录。"""
|
||||
item: Optional[Self] = db.execute(select(cls).where(cls.item_id == item_id)).scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def get_by_server_itemid(cls, db: Session, server: str, item_id: str):
|
||||
return db.execute(
|
||||
def get_by_server_itemid(cls, db: Session, server: str, item_id: str) -> Optional[Self]:
|
||||
"""在指定服务器中按媒体条目 ID 查询首条记录。"""
|
||||
item: Optional[Self] = db.execute(
|
||||
select(cls).where(cls.server == server, cls.item_id == item_id)
|
||||
).scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def empty(cls, db: Session, server: Optional[str] = None):
|
||||
def empty(cls, db: Session, server: Optional[str] = None) -> None:
|
||||
"""在调用方事务中暂存媒体服务器条目清空操作。"""
|
||||
statement = delete(cls)
|
||||
if server is not None:
|
||||
@@ -69,7 +73,7 @@ class MediaServerItem(Base):
|
||||
db.execute(statement, execution_options={"synchronize_session": False})
|
||||
|
||||
@classmethod
|
||||
def delete_stale(cls, db: Session, server: str, sync_time: str):
|
||||
def delete_stale(cls, db: Session, server: str, sync_time: str) -> int:
|
||||
"""在调用方事务中删除本轮同步未更新的条目。"""
|
||||
return execute_dml(
|
||||
db,
|
||||
@@ -81,7 +85,7 @@ class MediaServerItem(Base):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def delete_excluded_servers(cls, db: Session, servers: List[str]):
|
||||
def delete_excluded_servers(cls, db: Session, servers: List[str]) -> int:
|
||||
"""在调用方事务中删除不属于启用服务器的条目。"""
|
||||
statement = delete(cls)
|
||||
if servers:
|
||||
@@ -96,7 +100,7 @@ class MediaServerItem(Base):
|
||||
def exist_by_media_identity(
|
||||
cls, db: Session, media_source: MediaSource, media_id: str, mtype: str,
|
||||
server: Optional[str] = None,
|
||||
):
|
||||
) -> Optional[Self]:
|
||||
"""按规范媒体身份和类型查询媒体服务器条目。"""
|
||||
statement = select(cls).where(
|
||||
cls.media_source == str(media_source),
|
||||
@@ -105,13 +109,15 @@ class MediaServerItem(Base):
|
||||
)
|
||||
if server:
|
||||
statement = statement.where(cls.server == server)
|
||||
return db.execute(statement).scalars().first()
|
||||
item: Optional[Self] = db.execute(statement).scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def exists_by_title(
|
||||
cls, db: Session, title: str, mtype: str, year: str,
|
||||
server: Optional[str] = None,
|
||||
):
|
||||
) -> Optional[Self]:
|
||||
"""按标题及可选类型、年份和服务器查询首条记录。"""
|
||||
statement = select(cls).where(cls.title == title)
|
||||
if mtype:
|
||||
statement = statement.where(cls.item_type == mtype)
|
||||
@@ -119,7 +125,8 @@ class MediaServerItem(Base):
|
||||
statement = statement.where(cls.year == str(year))
|
||||
if server:
|
||||
statement = statement.where(cls.server == server)
|
||||
return db.execute(statement).scalars().first()
|
||||
item: Optional[Self] = db.execute(statement).scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def _cross_source_title_statement(
|
||||
@@ -150,15 +157,17 @@ class MediaServerItem(Base):
|
||||
return list(db.execute(statement).scalars().all())
|
||||
|
||||
@classmethod
|
||||
async def async_get_by_itemid(cls, db: AsyncSession, item_id: str):
|
||||
async def async_get_by_itemid(cls, db: AsyncSession, item_id: str) -> Optional[Self]:
|
||||
"""异步按媒体条目 ID 查询首条记录。"""
|
||||
result = await db.execute(select(cls).filter(cls.item_id == item_id))
|
||||
return result.scalars().first()
|
||||
item: Optional[Self] = result.scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
async def async_exist_by_media_identity(
|
||||
cls, db: AsyncSession, media_source: MediaSource, media_id: str, mtype: str,
|
||||
server: Optional[str] = None,
|
||||
):
|
||||
) -> Optional[Self]:
|
||||
"""异步按规范媒体身份和类型查询媒体服务器条目。"""
|
||||
statement = select(cls).filter(
|
||||
cls.media_source == str(media_source),
|
||||
@@ -168,7 +177,8 @@ class MediaServerItem(Base):
|
||||
if server:
|
||||
statement = statement.filter(cls.server == server)
|
||||
result = await db.execute(statement)
|
||||
return result.scalars().first()
|
||||
item: Optional[Self] = result.scalars().first()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
async def async_list_cross_source_by_title(
|
||||
@@ -186,7 +196,8 @@ class MediaServerItem(Base):
|
||||
async def async_exists_by_title(
|
||||
cls, db: AsyncSession, title: str, mtype: str, year: str,
|
||||
server: Optional[str] = None,
|
||||
):
|
||||
) -> Optional[Self]:
|
||||
"""异步按标题及可选类型、年份和服务器查询首条记录。"""
|
||||
if not mtype and not year:
|
||||
statement = select(cls).filter(cls.title == title)
|
||||
elif not year:
|
||||
@@ -202,4 +213,5 @@ class MediaServerItem(Base):
|
||||
if server:
|
||||
statement = statement.filter(cls.server == server)
|
||||
result = await db.execute(statement)
|
||||
return result.scalars().first()
|
||||
item: Optional[Self] = result.scalars().first()
|
||||
return item
|
||||
|
||||
@@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py
|
||||
回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。
|
||||
|
||||
2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 118 个文件(117 个生产文件和 1 个脚本)。
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 119 个文件(118 个生产文件和 1 个脚本)。
|
||||
每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。
|
||||
验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖
|
||||
签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准,
|
||||
|
||||
@@ -106,9 +106,9 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
|
||||
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
|
||||
| Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 |
|
||||
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
|
||||
| Python 源码量 | 372,583 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| Python 源码量 | 372,595 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 |
|
||||
| 全量 mypy 历史债务 | 8,756 / 437 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| 全量 mypy 历史债务 | 8,742 / 436 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| Ruff 历史诊断 | 98 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
|
||||
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |
|
||||
|
||||
|
||||
1
mypy.ini
1
mypy.ini
@@ -103,6 +103,7 @@ files =
|
||||
app/db/base.py,
|
||||
app/db/oper/plugindata.py,
|
||||
app/db/models/downloadfailure.py,
|
||||
app/db/models/mediaserver.py,
|
||||
app/db/models/passkey.py,
|
||||
app/db/models/plugindata.py,
|
||||
app/db/models/siteicon.py,
|
||||
|
||||
@@ -995,9 +995,6 @@
|
||||
"app/db/models/downloadhistory.py": {
|
||||
"no-untyped-def": 19
|
||||
},
|
||||
"app/db/models/mediaserver.py": {
|
||||
"no-untyped-def": 10
|
||||
},
|
||||
"app/db/models/message.py": {
|
||||
"type-arg": 1
|
||||
},
|
||||
@@ -1034,7 +1031,6 @@
|
||||
},
|
||||
"app/db/oper/mediaserver.py": {
|
||||
"arg-type": 12,
|
||||
"no-any-return": 4,
|
||||
"no-untyped-def": 7,
|
||||
"type-arg": 1
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user