From 0d139851af0016dc84aa0bd606fc0809247383e0 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 12 Oct 2024 12:14:49 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=8C=89=E5=AF=B9=E8=B1=A1=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E7=BB=84=E7=BB=87=E4=BB=A3=E7=A0=81=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/history.py | 1 - app/api/endpoints/login.py | 1 - app/api/endpoints/message.py | 2 +- app/api/endpoints/transfer.py | 1 - app/chain/__init__.py | 2 +- app/chain/mediaserver.py | 3 +- app/chain/message.py | 1 - app/db/mediaserver_oper.py | 1 - app/db/plugindata_oper.py | 2 - app/db/site_oper.py | 1 - app/db/systemconfig_oper.py | 2 - app/db/transferhistory_oper.py | 1 - app/db/user_oper.py | 1 - app/db/userconfig_oper.py | 2 - app/helper/downloader.py | 2 +- app/helper/mediaserver.py | 2 +- app/helper/notification.py | 2 +- app/helper/{servicebase.py => service.py} | 68 +++++++++++++++++++++-- app/helper/serviceconfig.py | 65 ---------------------- app/modules/__init__.py | 2 +- app/modules/slack/__init__.py | 1 - app/modules/telegram/__init__.py | 1 - app/modules/vocechat/__init__.py | 1 - 23 files changed, 71 insertions(+), 94 deletions(-) rename app/helper/{servicebase.py => service.py} (61%) delete mode 100644 app/helper/serviceconfig.py diff --git a/app/api/endpoints/history.py b/app/api/endpoints/history.py index e102ade9..29a399b5 100644 --- a/app/api/endpoints/history.py +++ b/app/api/endpoints/history.py @@ -1,4 +1,3 @@ -import json from typing import List, Any from fastapi import APIRouter, Depends diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index 6d59e227..7d88f8fd 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -1,4 +1,3 @@ -import json from datetime import timedelta from typing import Any, List diff --git a/app/api/endpoints/message.py b/app/api/endpoints/message.py index 48121d3f..7af814d7 100644 --- a/app/api/endpoints/message.py +++ b/app/api/endpoints/message.py @@ -14,7 +14,7 @@ from app.db import get_db from app.db.models import User from app.db.models.message import Message from app.db.user_oper import get_current_active_superuser -from app.helper.serviceconfig import ServiceConfigHelper +from app.helper.service import ServiceConfigHelper from app.log import logger from app.modules.wechat.WXBizMsgCrypt3 import WXBizMsgCrypt from app.schemas.types import MessageChannel diff --git a/app/api/endpoints/transfer.py b/app/api/endpoints/transfer.py index f33c13fd..1530ebe9 100644 --- a/app/api/endpoints/transfer.py +++ b/app/api/endpoints/transfer.py @@ -1,4 +1,3 @@ -import json from pathlib import Path from typing import Any, Optional diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 038d10b0..f0b3df66 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -17,7 +17,7 @@ from app.core.module import ModuleManager from app.db.message_oper import MessageOper from app.db.user_oper import UserOper from app.helper.message import MessageHelper -from app.helper.serviceconfig import ServiceConfigHelper +from app.helper.service import ServiceConfigHelper from app.log import logger from app.schemas import TransferInfo, TransferTorrent, ExistMediaInfo, DownloadingTorrent, CommingMessage, Notification, \ WebhookEventInfo, TmdbEpisode, MediaPerson, FileItem diff --git a/app/chain/mediaserver.py b/app/chain/mediaserver.py index ebc4dd21..d4d549e2 100644 --- a/app/chain/mediaserver.py +++ b/app/chain/mediaserver.py @@ -1,11 +1,10 @@ -import json import threading from typing import List, Union, Optional, Generator from app import schemas from app.chain import ChainBase from app.db.mediaserver_oper import MediaServerOper -from app.helper.serviceconfig import ServiceConfigHelper +from app.helper.service import ServiceConfigHelper from app.log import logger lock = threading.Lock() diff --git a/app/chain/message.py b/app/chain/message.py index d0f63630..c072cad0 100644 --- a/app/chain/message.py +++ b/app/chain/message.py @@ -1,5 +1,4 @@ import copy -import json import re from typing import Any, Optional, Dict, Union diff --git a/app/db/mediaserver_oper.py b/app/db/mediaserver_oper.py index 024d137e..47da9c58 100644 --- a/app/db/mediaserver_oper.py +++ b/app/db/mediaserver_oper.py @@ -1,4 +1,3 @@ -import json from typing import Optional from sqlalchemy.orm import Session diff --git a/app/db/plugindata_oper.py b/app/db/plugindata_oper.py index 18bae943..8a0fcdf3 100644 --- a/app/db/plugindata_oper.py +++ b/app/db/plugindata_oper.py @@ -1,9 +1,7 @@ -import json from typing import Any from app.db import DbOper from app.db.models.plugindata import PluginData -from app.utils.object import ObjectUtils class PluginDataOper(DbOper): diff --git a/app/db/site_oper.py b/app/db/site_oper.py index 754f5fe1..8a83acdd 100644 --- a/app/db/site_oper.py +++ b/app/db/site_oper.py @@ -1,4 +1,3 @@ -import json from datetime import datetime from typing import Tuple, List diff --git a/app/db/systemconfig_oper.py b/app/db/systemconfig_oper.py index 73324d13..9eaf05d1 100644 --- a/app/db/systemconfig_oper.py +++ b/app/db/systemconfig_oper.py @@ -1,10 +1,8 @@ -import json from typing import Any, Union from app.db import DbOper from app.db.models.systemconfig import SystemConfig from app.schemas.types import SystemConfigKey -from app.utils.object import ObjectUtils from app.utils.singleton import Singleton diff --git a/app/db/transferhistory_oper.py b/app/db/transferhistory_oper.py index 96083248..88b02fb2 100644 --- a/app/db/transferhistory_oper.py +++ b/app/db/transferhistory_oper.py @@ -1,4 +1,3 @@ -import json import time from typing import Any, List diff --git a/app/db/user_oper.py b/app/db/user_oper.py index c1a4c1d6..8037aa9a 100644 --- a/app/db/user_oper.py +++ b/app/db/user_oper.py @@ -1,4 +1,3 @@ -import json from typing import Optional from fastapi import Depends, HTTPException diff --git a/app/db/userconfig_oper.py b/app/db/userconfig_oper.py index fcd747ea..b263f71c 100644 --- a/app/db/userconfig_oper.py +++ b/app/db/userconfig_oper.py @@ -1,10 +1,8 @@ -import json from typing import Any, Union, Dict, Optional from app.db import DbOper from app.db.models.userconfig import UserConfig from app.schemas.types import UserConfigKey -from app.utils.object import ObjectUtils from app.utils.singleton import Singleton diff --git a/app/helper/downloader.py b/app/helper/downloader.py index d87489bc..e0e449ba 100644 --- a/app/helper/downloader.py +++ b/app/helper/downloader.py @@ -1,6 +1,6 @@ from typing import Optional -from app.helper.servicebase import ServiceBaseHelper +from app.helper.service import ServiceBaseHelper from app.schemas import DownloaderConf, ServiceInfo from app.schemas.types import SystemConfigKey, ModuleType diff --git a/app/helper/mediaserver.py b/app/helper/mediaserver.py index 433a0ba7..ffeb97be 100644 --- a/app/helper/mediaserver.py +++ b/app/helper/mediaserver.py @@ -1,6 +1,6 @@ from typing import Optional -from app.helper.servicebase import ServiceBaseHelper +from app.helper.service import ServiceBaseHelper from app.schemas import MediaServerConf, ServiceInfo from app.schemas.types import SystemConfigKey, ModuleType diff --git a/app/helper/notification.py b/app/helper/notification.py index 3a9e3adb..86ccb105 100644 --- a/app/helper/notification.py +++ b/app/helper/notification.py @@ -1,6 +1,6 @@ from typing import Optional -from app.helper.servicebase import ServiceBaseHelper +from app.helper.service import ServiceBaseHelper from app.schemas import NotificationConf, ServiceInfo from app.schemas.types import SystemConfigKey, ModuleType diff --git a/app/helper/servicebase.py b/app/helper/service.py similarity index 61% rename from app/helper/servicebase.py rename to app/helper/service.py index 51e346a6..57a1b357 100644 --- a/app/helper/servicebase.py +++ b/app/helper/service.py @@ -1,13 +1,73 @@ from typing import Dict, List, Optional, Type, TypeVar, Generic, Iterator from app.core.module import ModuleManager -from app.helper.serviceconfig import ServiceConfigHelper -from app.schemas import ServiceInfo -from app.schemas.types import SystemConfigKey, ModuleType +from app.db.systemconfig_oper import SystemConfigOper +from app.schemas import DownloaderConf, MediaServerConf, NotificationConf, NotificationSwitchConf, ServiceInfo +from app.schemas.types import NotificationType, SystemConfigKey, ModuleType TConf = TypeVar("TConf") +class ServiceConfigHelper: + """ + 配置帮助类,获取不同类型的服务配置 + """ + + @staticmethod + def get_configs(config_key: SystemConfigKey, conf_type: Type) -> List: + """ + 通用获取配置的方法,根据 config_key 获取相应的配置并返回指定类型的配置列表 + + :param config_key: 系统配置的 key + :param conf_type: 用于实例化配置对象的类类型 + :return: 配置对象列表 + """ + config_data = SystemConfigOper().get(config_key) + if not config_data: + return [] + # 直接使用 conf_type 来实例化配置对象 + return [conf_type(**conf) for conf in config_data] + + @staticmethod + def get_downloader_configs() -> List[DownloaderConf]: + """ + 获取下载器的配置 + """ + return ServiceConfigHelper.get_configs(SystemConfigKey.Downloaders, DownloaderConf) + + @staticmethod + def get_mediaserver_configs() -> List[MediaServerConf]: + """ + 获取媒体服务器的配置 + """ + return ServiceConfigHelper.get_configs(SystemConfigKey.MediaServers, MediaServerConf) + + @staticmethod + def get_notification_configs() -> List[NotificationConf]: + """ + 获取消息通知渠道的配置 + """ + return ServiceConfigHelper.get_configs(SystemConfigKey.Notifications, NotificationConf) + + @staticmethod + def get_notification_switches() -> List[NotificationSwitchConf]: + """ + 获取消息通知场景的开关 + """ + return ServiceConfigHelper.get_configs(SystemConfigKey.NotificationSwitchs, NotificationSwitchConf) + + @staticmethod + def get_notification_switch(mtype: NotificationType) -> Optional[str]: + """ + 获取指定类型的消息通知场景的开关 + """ + switchs = ServiceConfigHelper.get_notification_switches() + for switch in switchs: + if switch.type == mtype.value: + return switch.action + return None + + class ServiceBaseHelper(Generic[TConf]): """ 通用服务帮助类,抽象获取配置和服务实例的通用逻辑 @@ -84,7 +144,7 @@ class ServiceBaseHelper(Generic[TConf]): if service_info.config and ( type_filter is None or service_info.type == type_filter ) and ( - name_filters_set is None or service_info.name in name_filters_set) + name_filters_set is None or service_info.name in name_filters_set) } def get_service(self, name: str, type_filter: Optional[str] = None) -> Optional[ServiceInfo]: diff --git a/app/helper/serviceconfig.py b/app/helper/serviceconfig.py deleted file mode 100644 index 6822d22e..00000000 --- a/app/helper/serviceconfig.py +++ /dev/null @@ -1,65 +0,0 @@ -from typing import List, Type, Optional - -from app.db.systemconfig_oper import SystemConfigOper -from app.schemas import DownloaderConf, MediaServerConf, NotificationConf, NotificationSwitchConf -from app.schemas.types import SystemConfigKey, NotificationType - - -class ServiceConfigHelper: - """ - 配置帮助类,获取不同类型的服务配置 - """ - - @staticmethod - def get_configs(config_key: SystemConfigKey, conf_type: Type) -> List: - """ - 通用获取配置的方法,根据 config_key 获取相应的配置并返回指定类型的配置列表 - - :param config_key: 系统配置的 key - :param conf_type: 用于实例化配置对象的类类型 - :return: 配置对象列表 - """ - config_data = SystemConfigOper().get(config_key) - if not config_data: - return [] - # 直接使用 conf_type 来实例化配置对象 - return [conf_type(**conf) for conf in config_data] - - @staticmethod - def get_downloader_configs() -> List[DownloaderConf]: - """ - 获取下载器的配置 - """ - return ServiceConfigHelper.get_configs(SystemConfigKey.Downloaders, DownloaderConf) - - @staticmethod - def get_mediaserver_configs() -> List[MediaServerConf]: - """ - 获取媒体服务器的配置 - """ - return ServiceConfigHelper.get_configs(SystemConfigKey.MediaServers, MediaServerConf) - - @staticmethod - def get_notification_configs() -> List[NotificationConf]: - """ - 获取消息通知渠道的配置 - """ - return ServiceConfigHelper.get_configs(SystemConfigKey.Notifications, NotificationConf) - - @staticmethod - def get_notification_switches() -> List[NotificationSwitchConf]: - """ - 获取消息通知场景的开关 - """ - return ServiceConfigHelper.get_configs(SystemConfigKey.NotificationSwitchs, NotificationSwitchConf) - - @staticmethod - def get_notification_switch(mtype: NotificationType) -> Optional[str]: - """ - 获取指定类型的消息通知场景的开关 - """ - switchs = ServiceConfigHelper.get_notification_switches() - for switch in switchs: - if switch.type == mtype.value: - return switch.action - return None diff --git a/app/modules/__init__.py b/app/modules/__init__.py index af2dc8fa..967fc529 100644 --- a/app/modules/__init__.py +++ b/app/modules/__init__.py @@ -1,7 +1,7 @@ from abc import abstractmethod, ABCMeta from typing import Generic, Tuple, Union, TypeVar, Type, Dict, Optional, Callable, Any -from app.helper.serviceconfig import ServiceConfigHelper +from app.helper.service import ServiceConfigHelper from app.schemas import Notification, MessageChannel, NotificationConf, MediaServerConf, DownloaderConf from app.schemas.types import ModuleType diff --git a/app/modules/slack/__init__.py b/app/modules/slack/__init__.py index d127d4ab..ce84f828 100644 --- a/app/modules/slack/__init__.py +++ b/app/modules/slack/__init__.py @@ -2,7 +2,6 @@ import json import re from typing import Optional, Union, List, Tuple, Any -from app.core.config import settings from app.core.context import MediaInfo, Context from app.log import logger from app.modules import _ModuleBase, _MessageBase diff --git a/app/modules/telegram/__init__.py b/app/modules/telegram/__init__.py index f22284d8..a46d76ef 100644 --- a/app/modules/telegram/__init__.py +++ b/app/modules/telegram/__init__.py @@ -1,7 +1,6 @@ import json from typing import Optional, Union, List, Tuple, Any, Dict -from app.core.config import settings from app.core.context import MediaInfo, Context from app.log import logger from app.modules import _ModuleBase, _MessageBase diff --git a/app/modules/vocechat/__init__.py b/app/modules/vocechat/__init__.py index 889650a1..40a45a46 100644 --- a/app/modules/vocechat/__init__.py +++ b/app/modules/vocechat/__init__.py @@ -1,7 +1,6 @@ import json from typing import Optional, Union, List, Tuple, Any, Dict -from app.core.config import settings from app.core.context import Context, MediaInfo from app.log import logger from app.modules import _ModuleBase, _MessageBase