mirror of
https://github.com/jxxghp/MoviePilot-Plugins.git
synced 2026-05-24 23:16:49 +00:00
- 数据模型重构: 全面引入 Pydantic 模型(ClashConfig, Proxy, ProxyGroup 等)替代原有字典结构,提供更严格的数据验证与类型安全。 - 数据迁移机制: 新增 v2.1.0 数据升级脚本,支持将旧版代理、策略组及规则数据自动迁移至新架构。 - 配置补丁系统: 实现基于 JSON Patch 的细粒度配置修补机制,替代旧版覆盖逻辑,提升配置修改的灵活性。 - 服务层优化: 重写 ClashRuleProviderService 以适配新对象模型,增强代码可维护性与扩展性。 - API模型同步: 更新相关 API 数据模型以保持与内部数据结构的一致性。 - 用户界面: 批量规则管理和数据项隐藏支持
33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
from pydantic import BaseModel, Field, RootModel
|
|
|
|
|
|
class PatchItem(BaseModel):
|
|
lifecycle: int = Field(default=3)
|
|
patch: str
|
|
|
|
|
|
class DataPatch(RootModel[dict[str, PatchItem]]):
|
|
"""DataPatch model for storing patch items."""
|
|
root: dict[str, PatchItem] = Field(default_factory=dict, description="Dictionary of patch items.")
|
|
|
|
def update_patch(self, alive_keys: list[str] | set[str], lifespan: int = 3):
|
|
outdated_keys = []
|
|
for key in list(self.root.keys()):
|
|
if key not in alive_keys:
|
|
self.root[key].lifecycle -= 1
|
|
if self.root[key].lifecycle == 0:
|
|
outdated_keys.append(key)
|
|
else:
|
|
self.root[key].lifecycle = lifespan
|
|
for key in outdated_keys:
|
|
del self.root[key]
|
|
|
|
def __setitem__(self, key: str, value: PatchItem):
|
|
self.root[key] = value
|
|
|
|
def __contains__(self, key: str) -> bool:
|
|
return key in self.root
|
|
|
|
def __getitem__(self, key: str) -> PatchItem:
|
|
return self.root[key]
|