1+ from dataclasses import dataclass , field
12from typing import TYPE_CHECKING
23
34from nonebot .log import logger
1314if TYPE_CHECKING :
1415 from nonebot_bison .db .db_model import Target
1516
16- scheduler_dict : dict [SiteConfig , Scheduler ] = {}
17+ type SiteName = str
18+
19+
20+ @dataclass
21+ class SchedulerDict :
22+ schedulers : dict [SiteName , Scheduler ] = field (default_factory = dict )
23+
24+ def __getitem__ (self , key : str | SiteConfig , / ) -> Scheduler :
25+ match key :
26+ case str ():
27+ return self .schedulers [key ]
28+ case SiteConfig ():
29+ return self .schedulers [key .name ]
30+ case _:
31+ raise TypeError (f"Unsupport SchedulerDict key type: { type (key )} " )
32+
33+ def __setitem__ (self , key : str | SiteConfig , value : Scheduler , / ):
34+ match key :
35+ case str ():
36+ self .schedulers [key ] = value
37+ case SiteConfig ():
38+ self .schedulers [key .name ] = value
39+ case _:
40+ raise TypeError (f"Unsupport SchedulerDict key type: { type (key )} " )
41+
42+ def __contains__ (self , key : str | SiteConfig ) -> bool :
43+ match key :
44+ case str ():
45+ return key in self .schedulers
46+ case SiteConfig ():
47+ return key .name in self .schedulers
48+ case _:
49+ raise TypeError (f"Unsupport SchedulerDict key type: { type (key )} " )
50+
51+
52+ scheduler_dict = SchedulerDict ()
1753
1854
1955async def init_scheduler ():
20- _schedule_class_dict : dict [SiteConfig , list [Target ]] = {}
21- _schedule_class_platform_dict : dict [SiteConfig , list [str ]] = {}
56+ _schedule_class_name : dict [SiteName , SiteConfig ] = {}
57+ _schedule_class_dict : dict [SiteName , list [Target ]] = {}
58+ _schedule_class_platform_dict : dict [SiteName , list [str ]] = {}
2259 for platform in Platform .registry .values ():
2360 platform : Platform = platform
2461 site = platform .site_config
2562 if not hasattr (site , "name" ) or not site .name :
2663 site .name = f"AnonymousScheduleConfig[{ platform .platform_config .display_name } ]"
2764
65+ site_name = site .name
2866 platform_name = platform .platform_config .display_name
2967 targets = await data_access .get_platform_target (platform_name )
30- if site not in _schedule_class_dict :
31- _schedule_class_dict [site ] = list (targets )
68+ if site_name not in _schedule_class_dict :
69+ _schedule_class_name [site_name ] = site
70+ _schedule_class_dict [site_name ] = list (targets )
3271 else :
33- _schedule_class_dict [site ].extend (targets )
34- if site not in _schedule_class_platform_dict :
35- _schedule_class_platform_dict [site ] = [platform_name ]
72+ _schedule_class_dict [site_name ].extend (targets )
73+ if site_name not in _schedule_class_platform_dict :
74+ _schedule_class_platform_dict [site_name ] = [platform_name ]
3675 else :
37- _schedule_class_platform_dict [site ].append (platform_name )
76+ _schedule_class_platform_dict [site_name ].append (platform_name )
77+
3878 for site , target_list in _schedule_class_dict .items ():
39- if not plugin_config .bison_use_browser and site .require_browser :
40- logger .warning (f"{ site . name } requires browser, it will not schedule." )
79+ if not plugin_config .bison_use_browser and _schedule_class_name [ site ] .require_browser :
80+ logger .warning (f"{ site } requires browser, it will not schedule." )
4181 continue
4282
4383 schedulable_args = []
@@ -46,7 +86,7 @@ async def init_scheduler():
4686 (target .platform_name , T_Target (target .target ), Platform .registry [target .platform_name ].use_batch )
4787 )
4888 platform_name_list = _schedule_class_platform_dict [site ]
49- scheduler_dict [site ] = Scheduler (site , schedulable_args , platform_name_list )
89+ scheduler_dict [site ] = Scheduler (_schedule_class_name [ site ] , schedulable_args , platform_name_list )
5090 # if is_cookie_client_manager(site.client_mgr):
5191 # client_mgr = cast("CookieClientManager", scheduler_dict[site].client_mgr)
5292 # await client_mgr.refresh_client()
0 commit comments