@@ -32,65 +32,82 @@ def _is_non_class_time() -> bool:
3232 """
3333 try :
3434 # 1. 检查课间禁用开关是否启用
35- if not _is_instant_draw_disable_enabled ():
35+ instant_draw_disable = readme_settings_async (
36+ "program_functionality" , "instant_draw_disable"
37+ )
38+ if not instant_draw_disable :
3639 return False
3740
38- # 2. 获取非上课时间段配置
39- non_class_times = _get_non_class_times_config ()
40- if not non_class_times :
41- return False
41+ # 2. 检查是否启用了ClassIsland数据源
42+ use_class_island_source = readme_settings_async (
43+ "time_settings" , "class_island_source_enabled"
44+ )
45+
46+ if use_class_island_source :
47+ # 使用ClassIsland数据判断是否为课间时间
48+ class_island_break_status = readme_settings_async (
49+ "time_settings" , "current_class_island_break_status"
50+ )
51+ # 确保返回布尔值
52+ return bool (class_island_break_status )
53+ else :
54+ # 使用CSES配置的非上课时间段
55+ non_class_times = _get_non_class_times_config ()
56+ if not non_class_times or not isinstance (non_class_times , dict ):
57+ # 如果非上课时间配置不存在或格式不正确,返回False
58+ return False
4259
43- # 3. 获取当前时间并转换为总秒数
44- current_total_seconds = _get_current_time_in_seconds ()
60+ # 3. 获取当前时间并转换为总秒数
61+ current_total_seconds = _get_current_time_in_seconds ()
4562
46- # 4. 检查当前时间是否在任何非上课时间段内
47- return _is_time_in_ranges (current_total_seconds , non_class_times )
63+ # 4. 检查当前时间是否在任何非上课时间段内
64+ return _is_time_in_ranges (current_total_seconds , non_class_times )
4865
4966 except Exception as e :
5067 logger .error (f"检测非上课时间失败: { e } " )
5168 return False
5269
5370
54- def _is_instant_draw_disable_enabled () -> bool :
55- """检查课间禁用开关是否启用
71+ def _get_non_class_times_config () -> Dict [ str , str ] :
72+ """获取非上课时间段配置
5673
5774 Returns:
58- bool: 如果课间禁用开关启用返回True,否则返回False
75+ Dict[str, str]: 非上课时间段配置字典,如果获取失败返回空字典
5976 """
6077 try :
61- settings_path = get_settings_path ()
62- if not file_exists (settings_path ):
63- return False
64-
65- with open_file (settings_path , "r" , encoding = "utf-8" ) as f :
66- settings = json .load (f )
78+ # 从data/CSES目录获取CSES文件
79+ cses_dir = get_data_path ("CSES" )
80+ if not os .path .exists (cses_dir ):
81+ logger .info ("CSES目录不存在,返回空的非上课时间配置" )
82+ return {}
6783
68- program_functionality = settings . get ( "program_functionality" , {})
69- return program_functionality . get ( "instant_draw_disable" , False )
84+ # 获取CSES目录中的所有YAML文件
85+ import os
7086
71- except Exception as e :
72- logger . error ( f"读取课间禁用设置失败: { e } " )
73- return False
87+ cses_files = [
88+ f for f in os . listdir ( cses_dir ) if f . lower (). endswith (( ".yaml" , ".yml" ) )
89+ ]
7490
91+ if not cses_files :
92+ logger .info ("CSES目录中没有找到YAML文件,返回空的非上课时间配置" )
93+ return {}
7594
76- def _get_non_class_times_config () -> Dict [ str , str ]:
77- """获取非上课时间段配置
95+ # 使用第一个找到的CSES文件
96+ cses_file_path = os . path . join ( cses_dir , cses_files [ 0 ])
7897
79- Returns:
80- Dict[str, str]: 非上课时间段配置字典,如果获取失败返回空字典
81- """
82- try :
83- time_settings_path = get_settings_path ()
84- if not file_exists (time_settings_path ):
98+ # 创建CSES解析器并加载文件
99+ parser = CSESParser ()
100+ if not parser .load_from_file (cses_file_path ):
101+ logger .error (f"加载CSES文件失败: { cses_file_path } " )
85102 return {}
86103
87- with open_file ( time_settings_path , "r" , encoding = "utf-8" ) as f :
88- time_settings = json . load ( f )
89-
90- return time_settings . get ( " non_class_times" , {})
104+ # 使用CSES解析器获取非上课时间段
105+ non_class_times = parser . get_non_class_times ( )
106+ logger . info ( f"成功从CSES文件生成 { len ( non_class_times ) } 个非上课时间段" )
107+ return non_class_times
91108
92109 except Exception as e :
93- logger .error (f"读取时间设置失败 : { e } " )
110+ logger .error (f"读取CSES时间设置失败 : { e } " )
94111 return {}
95112
96113
0 commit comments