forked from Class-Widgets/Class-Widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
1257 lines (1057 loc) · 43.9 KB
/
utils.py
File metadata and controls
1257 lines (1057 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import atexit
import datetime as dt
import gc
import inspect
import os
import re
import signal
import sys
import threading
import time
import weakref
from abc import ABC, abstractmethod
from heapq import heapify, heappop, heappush
from pathlib import Path
from typing import Any, Callable, ClassVar, Dict, List, Optional, Tuple, Type, Union
if os.name == 'nt':
import win32gui
from win32com.client import Dispatch
import darkdetect
import ntplib
import psutil
import pytz
from loguru import logger
from PyQt5.QtCore import (
QDir,
QLockFile,
QObject,
QTimer,
QtMsgType,
pyqtSignal,
qInstallMessageHandler,
)
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon
from basic_dirs import CW_HOME, LOG_HOME
from file import config_center
from generate_speech import get_tts_service
class StreamToLogger:
"""重定向 print() 到 loguru"""
def write(self, message):
msg = message.strip()
if msg:
logger.opt(depth=1).info(msg)
def flush(self):
pass
def qt_message_handler(mode, context, message): # noqa
"""Qt 消息转发到 loguru"""
msg = message.strip()
if not msg:
return
if mode == QtMsgType.QtCriticalMsg:
logger.error(msg)
logger.complete()
elif mode == QtMsgType.QtFatalMsg:
logger.critical(msg)
logger.complete()
else:
logger.complete()
if config_center.read_conf("Other", "do_not_log") == "0":
log_file = LOG_HOME / "ClassWidgets_main_{time}.log"
logger.add(
log_file,
rotation="1 MB",
retention="1 minute",
encoding="utf-8",
enqueue=True,
backtrace=True,
diagnose=True,
)
sys.stdout = StreamToLogger()
sys.stderr = StreamToLogger()
qInstallMessageHandler(qt_message_handler)
atexit.register(logger.complete)
logger.debug("未禁用日志输出")
else:
logger.info("已禁用日志输出功能, 若需保存日志, 请在“设置”->“高级选项”中关闭禁用日志功能")
def run_once(func: Callable) -> Callable:
"""装饰器: 只执行一次"""
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
return func(*args, **kwargs)
return None
wrapper.has_run = False
return wrapper
LOGO_PATH = CW_HOME / "img" / "logo"
CallbackInfoType = Dict[str, Union[float, dt.datetime]]
TaskHeapType = List[Tuple[dt.datetime, int, Callable[[], Any], float]]
def _reset_signal_handlers() -> None:
"""重置信号处理器为默认状态"""
try:
signal.signal(signal.SIGTERM, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
except (AttributeError, ValueError):
pass
def _terminate_child_processes() -> None:
"""终止所有子进程"""
try:
parent = psutil.Process(os.getpid())
children = parent.children(recursive=True)
if not children:
return
logger.debug(f"尝试终止 {len(children)} 个子进程...")
for child in children:
try:
logger.debug(f"终止子进程 {child.pid}...")
child.terminate()
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
logger.debug(f"子进程 {child.pid}: {e}")
except Exception as e:
logger.warning(f"终止子进程 {child.pid} 时出错: {e}")
_gone, alive = psutil.wait_procs(children, timeout=1.5)
if alive:
logger.warning(f"{len(alive)} 个子进程未在规定时间内终止,将强制终止...")
for p in alive:
try:
logger.debug(f"强制终止子进程 {p.pid}...")
p.kill()
except psutil.NoSuchProcess:
logger.debug(f"子进程 {p.pid} 在强制终止前已消失.")
except Exception as e:
logger.error(f"强制终止子进程 {p.pid} 失败: {e}")
except psutil.NoSuchProcess:
logger.warning("无法获取当前进程信息,跳过子进程终止。")
except Exception as e:
logger.error(f"终止子进程时出现意外错误: {e}")
def restart() -> None:
"""重启程序"""
logger.debug('重启程序')
app = QApplication.instance()
if app:
_reset_signal_handlers()
app.quit()
app.processEvents()
guard.release()
os.execl(sys.executable, sys.executable, *sys.argv)
@run_once
def stop(status: int = 0) -> None:
"""
退出程序
:param status: 退出状态码,0=正常退出,!=0表示异常退出
"""
logger.debug('退出程序...')
try:
tts_service = get_tts_service()
if hasattr(tts_service, '_manager') and tts_service._manager:
tts_service._manager.stop()
except Exception as e:
logger.warning(f"清理TTS管理器时出错: {e}")
if update_timer:
try:
update_timer.stop()
except Exception as e:
logger.warning(f"停止全局更新定时器时出错: {e}")
gc.collect()
try:
asyncio.set_event_loop(None)
except Exception as e:
logger.warning(f"清理异步引用时出错: {e}")
app = QApplication.instance()
guard.release()
if app:
_reset_signal_handlers()
app.quit()
app.processEvents()
_terminate_child_processes()
logger.debug(f"程序退出({status})")
if not app:
os._exit(status)
def calculate_size(
p_w: float = 0.6, p_h: float = 0.7
) -> Tuple[Tuple[int, int], Tuple[int, int]]: # 计算尺寸
"""计算尺寸"""
screen_geometry = QApplication.primaryScreen().geometry()
screen_width = screen_geometry.width()
screen_height = screen_geometry.height()
width = int(screen_width * p_w)
height = int(screen_height * p_h)
return (width, height), (int(screen_width / 2 - width / 2), 150)
class DarkModeWatcher(QObject):
"""
颜色(暗黑)模式监听器
"""
dark_mode_changed = pyqtSignal(bool) # 发出暗黑模式变化信号
def __init__(self, interval: int = 500, parent: Optional[QObject] = None) -> None:
super().__init__(parent)
self._isDarkMode: bool = bool(darkdetect.isDark()) # 初始状态
self.interval = interval
self.update_callback()
def _check_theme(self) -> None:
current_mode: bool = bool(darkdetect.isDark())
if current_mode != self._isDarkMode:
self._isDarkMode = current_mode
self.dark_mode_changed.emit(current_mode) # 发出变化信号
def is_dark(self) -> bool:
"""返回当前是否暗黑模式"""
return self._isDarkMode
def update_callback(self) -> None:
self._callback_id = update_timer.add_callback(
self._check_theme, interval=self.interval / 1000
)
def stop(self) -> None:
"""停止监听"""
if hasattr(self, '_callback_id') and self._callback_id:
update_timer.remove_callback_by_id(self._callback_id)
self._callback_id = None
def start(self, interval: Optional[int] = None) -> None:
"""开始监听"""
if hasattr(self, '_callback_id') and self._callback_id:
update_timer.remove_callback_by_id(self._callback_id)
interval_seconds = (interval / 1000) if interval else 0.5 # 默认0.5秒
self._callback_id = update_timer.add_callback(self._check_theme, interval=interval_seconds)
class TrayIcon(QSystemTrayIcon):
"""托盘图标"""
def __init__(self, parent: Optional[QObject] = None) -> None:
super().__init__(parent)
self.setIcon(QIcon(str(LOGO_PATH / "favicon.png")))
def update_tooltip(self) -> None:
"""更新托盘文字"""
schedule_name_from_conf = config_center.read_conf('General', 'schedule')
if schedule_name_from_conf:
try:
schedule_display_name = schedule_name_from_conf
if schedule_display_name.endswith('.json'):
schedule_display_name = schedule_display_name[:-5]
self.setToolTip(f'Class Widgets - "{schedule_display_name}"')
logger.debug(f'托盘文字更新: "Class Widgets - {schedule_display_name}"')
except Exception as e:
logger.error(f"更新托盘提示时发生错误: {e}")
else:
self.setToolTip("Class Widgets - 未加载课表")
logger.debug('托盘文字更新: "Class Widgets - 未加载课表"')
def push_update_notification(self, text: str = '') -> None:
self.setIcon(QIcon(str(LOGO_PATH / "favicon-update.png"))) # tray
self.showMessage(
"发现 Class Widgets 新版本!", text, QIcon(str(LOGO_PATH / "favicon-update.png")), 5000
)
def push_error_notification(self, title: str = '检查更新失败!', text: str = '') -> None:
self.setIcon(QIcon(str(LOGO_PATH / "favicon-update.png"))) # tray
self.showMessage(title, text, QIcon(str(LOGO_PATH / "favicon-error.ico")), 5000)
class UnionUpdateTimer(QObject):
"""
统一更新计时器
"""
def __init__(self, parent: Optional[QObject] = None, base_interval: float = 0.1) -> None:
super().__init__(parent)
self.timer: QTimer = QTimer(self)
self.timer.timeout.connect(self._on_timeout)
self.task_heap: TaskHeapType = [] # [(next_run, id(callback), callback, interval), ...]
heapify(self.task_heap)
self.callback_info: Dict[int, CallbackInfoType] = {} # 使用id作为键
self._callback_refs: Dict[int, weakref.ReferenceType] = {} # 弱引用存储
self._callback_hashes: Dict[int, int] = {} # 回调函数哈希值验证
self._callback_error_count: Dict[int, int] = {} # 错误计数
self._max_error_count: int = 5 # 最大错误次数
self._is_running: bool = False
self._base_interval: float = max(0.01, base_interval) # 基础间隔,最小10ms
self._next_check_time: Optional[dt.datetime] = None # 下次检查时间
self._last_current_time: Optional[dt.datetime] = None # 上次记录的当前时间
def _on_timeout(self) -> None:
app = QApplication.instance()
if not app or app.closingDown():
self._safe_stop_timer()
return
try:
current_time = TimeManagerFactory.get_instance().get_current_time()
except Exception as e:
logger.error(f"获取当前时间失败: {e}")
raise RuntimeError("无法获得当前时间") from e
# 检测时间跳跃
if self._last_current_time is not None:
time_diff = (current_time - self._last_current_time).total_seconds()
if abs(time_diff) > 2:
logger.info(f"检测到时间跳跃: {time_diff:.2f}秒,重新调度所有任务")
self._reschedule_all_tasks(current_time)
self._last_current_time = current_time
if not self.task_heap:
self._is_running = False
self._safe_stop_timer()
return
executed_count = 0
while self.task_heap and (self.task_heap[0][0] <= current_time):
_next_run, cb_id, _callback, interval = heappop(self.task_heap)
if cb_id not in self._callback_refs or self._callback_refs[cb_id]() is None:
self._cleanup_dead_callback(cb_id)
continue
actual_callback = self._callback_refs[cb_id]()
if actual_callback is None or hash(actual_callback) != self._callback_hashes.get(cb_id):
self._cleanup_dead_callback(cb_id)
continue
try:
actual_callback()
if cb_id in self._callback_error_count:
del self._callback_error_count[cb_id]
executed_count += 1
except Exception as e:
logger.error(
f"回调执行失败: {e}, 回调: {actual_callback.__name__ if hasattr(actual_callback, '__name__') else str(actual_callback)}"
)
self._increment_error_count(cb_id)
if self._should_remove_callback(cb_id):
self._cleanup_dead_callback(cb_id)
continue
# 重新调度回调
if cb_id in self.callback_info:
self.callback_info[cb_id]['last_run'] = current_time
# else:
# logger.debug(f"try push {cb_id} cancelled")
if cb_id in self.callback_info:
next_time = current_time + dt.timedelta(seconds=interval)
self.callback_info[cb_id]['next_run'] = next_time
heappush(self.task_heap, (next_time, cb_id, actual_callback, interval))
# else:
# logger.debug(f"try push {cb_id} cancelled")
# if executed_count > 0:
# logger.debug(f"执行了 {executed_count} 个回调")
if self._is_running:
self._schedule_next()
def _reschedule_all_tasks(self, current_time: dt.datetime) -> None:
"""重新调度所有任务,用于处理时间跳跃"""
new_heap = []
for _next_run_time, cb_id, callback, interval in self.task_heap:
# 重新计算下次执行时间
new_next_time = current_time + dt.timedelta(seconds=interval)
new_heap.append((new_next_time, cb_id, callback, interval))
# 更新回调信息
if cb_id in self.callback_info:
self.callback_info[cb_id]['next_run'] = new_next_time
self.task_heap = new_heap
heapify(self.task_heap)
def _schedule_next(self) -> None:
"""调度器"""
if not self.task_heap:
return
try:
current_time = TimeManagerFactory.get_instance().get_current_time()
except Exception as e:
logger.error(f"获取当前时间失败: {e}")
raise RuntimeError("无法获得当前时间") from e
next_task_time = self.task_heap[0][0]
delay_seconds = (next_task_time - current_time).total_seconds()
# 处理时间异常情况 - 如果延迟时间异常,可能是时间跳跃
if abs(delay_seconds) > 2: # 如果延迟时间超过±2秒,重新调度
logger.warning(f"检测到异常延迟时间: {delay_seconds:.2f}秒,重新调度任务")
self._reschedule_all_tasks(current_time)
if self.task_heap:
next_task_time = self.task_heap[0][0]
delay_seconds = (next_task_time - current_time).total_seconds()
else:
return
if delay_seconds <= 0:
delay_ms = 1 # 立即执行已到期任务
elif delay_seconds > 60.0:
delay_ms = 60000 # 最大60秒
else:
delay_ms = delay_seconds * 1000
delay = max(1, min(int(delay_ms), 60000))
self._next_check_time = current_time + dt.timedelta(milliseconds=delay)
try:
self.timer.start(delay)
# logger.debug(f"延迟={delay}ms, 全局任务数: {len(self.task_heap)}")
except Exception as e:
logger.error(f"启动定时器失败, 延迟={delay}ms: {e}")
fallback_delay = max(1, int(self._base_interval * 1000))
try:
self.timer.start(fallback_delay)
logger.debug(f"使用回退延迟={fallback_delay}ms")
except Exception as fallback_e:
logger.critical(f"回退定时器启动失败: {fallback_e}")
self._is_running = False
self._safe_stop_timer()
def _cleanup_dead_callback(self, cb_id: int) -> None:
"""清理失效的回调函数"""
# logger.debug(f"cleanup {cb_id}")
self.callback_info.pop(cb_id, None)
self._callback_refs.pop(cb_id, None)
self._callback_hashes.pop(cb_id, None)
self._callback_error_count.pop(cb_id, None)
def _increment_error_count(self, cb_id: int) -> None:
"""增加回调错误计数"""
self._callback_error_count[cb_id] = self._callback_error_count.get(cb_id, 0) + 1
def _should_remove_callback(self, cb_id: int) -> bool:
"""判断是否应该移除回调"""
return self._callback_error_count.get(cb_id, 0) >= self._max_error_count
def _safe_stop_timer(self) -> None:
"""安全停止定时器"""
if self.timer and self.timer.isActive():
try:
self.timer.stop()
except RuntimeError as e:
logger.warning(f"停止 QTimer 时发生运行时错误: {e}")
except Exception as e:
logger.error(f"停止 QTimer 时发生未知错误: {e}")
def add_callback(self, callback: Callable[[], Any], interval: float = 1.0) -> int:
"""添加回调函数到定时器
Args:
callback: 要执行的回调函数
interval: 执行间隔(秒), 默认1秒, 最小0.1秒
Returns:
int: 回调函数的唯一ID
Raises:
TypeError: 当callback不是可调用对象时
"""
if not callable(callback):
raise TypeError("回调必须是可调用对象") from None
try:
callback_hash = hash(callback)
except TypeError as err:
raise TypeError("回调函数必须是可哈希的") from err
interval = max(0.1, interval)
current_time: dt.datetime = TimeManagerFactory.get_instance().get_current_time()
next_run = current_time + dt.timedelta(seconds=interval)
cb_id = id(callback)
if cb_id in self.callback_info:
self._cleanup_dead_callback(cb_id)
self.callback_info[cb_id] = {
'interval': interval,
'last_run': current_time,
'next_run': next_run,
}
def cleanup_callback(ref: weakref.ReferenceType) -> None: # noqa
self._cleanup_dead_callback(cb_id)
self._callback_refs[cb_id] = weakref.ref(callback, cleanup_callback)
self._callback_hashes[cb_id] = callback_hash
heappush(self.task_heap, (next_run, cb_id, callback, interval))
should_start = not self._is_running
if should_start:
self.start()
# logger.debug(f"add {cb_id}")
return cb_id
def remove_callback(self, callback: Callable[[], Any]) -> None:
"""移除回调函数
Args:
callback: 要移除的回调函数
"""
cb_id = id(callback)
self.remove_callback_by_id(cb_id)
def remove_callback_by_id(self, callback_id: int) -> None:
"""通过回调ID移除回调函数
Args:
callback_id: 回调函数的ID
"""
if callback_id in self.callback_info:
self._cleanup_dead_callback(callback_id)
new_heap = [] # 重建堆
for next_run, cb_id, callback, interval in self.task_heap:
if cb_id != callback_id:
new_heap.append((next_run, cb_id, callback, interval))
self.task_heap = new_heap
heapify(self.task_heap)
if not self.task_heap: # 如果没有任务, 停止定时器
self._is_running = False
self._safe_stop_timer()
def remove_all_callbacks(self) -> None:
"""移除所有已注册的回调函数"""
# logger.debug("remove_all_callbacks")
self.callback_info.clear()
self._callback_refs.clear()
self._callback_hashes.clear()
self.task_heap.clear()
self._callback_error_count.clear()
self._is_running = False
self._safe_stop_timer()
def start(self) -> None:
"""启动定时器"""
if self._is_running:
return
if not self.task_heap:
logger.debug("任务堆为空")
return
self._is_running = True
logger.debug("启动 UnionUpdateTimer...")
self._schedule_next()
def stop(self) -> None:
"""停止定时器"""
if self._is_running:
self._is_running = False
else:
logger.debug("定时器未运行")
self._safe_stop_timer()
logger.debug("UnionUpdateTimer 已停止")
def set_callback_interval(self, callback: Callable[[], Any], interval: float) -> bool:
"""设置特定回调函数的执行间隔(s)
Args:
callback: 目标回调函数
interval: 新的执行间隔(秒), 最小0.1秒
Returns:
bool: 成功True, 不存在False
"""
interval = max(0.1, interval)
current_time = TimeManagerFactory.get_instance().get_current_time()
next_run = current_time + dt.timedelta(seconds=interval)
cb_id = id(callback)
if cb_id in self.callback_info:
self.callback_info[cb_id]['interval'] = interval
self.callback_info[cb_id]['next_run'] = next_run
new_heap = []
for next_run_time, heap_cb_id, heap_callback, heap_interval in self.task_heap:
if heap_cb_id == cb_id:
new_heap.append((next_run, cb_id, callback, interval))
else:
new_heap.append((next_run_time, heap_cb_id, heap_callback, heap_interval))
self.task_heap = new_heap
heapify(self.task_heap)
return True
return False
def get_callback_interval(self, callback: Callable[[], Any]) -> Optional[float]:
"""获取特定回调函数的执行间隔(s)
Args:
callback: 目标回调函数
Returns:
Optional[float]: 回调间隔(秒), 不存在则返回None
"""
cb_id = id(callback)
if cb_id in self.callback_info:
interval = self.callback_info[cb_id]['interval']
return float(interval) if isinstance(interval, (int, float)) else None
return None
def set_base_interval(self, interval: float) -> None:
"""设置基础检查间隔时间(s)
Args:
interval: 新的基础间隔时间, 最小值为0.05秒
"""
new_interval: float = max(0.05, interval)
self._base_interval = new_interval
was_running: bool = self._is_running
if was_running:
self.stop()
self.start()
def get_base_interval(self) -> float:
"""获取当前基础检查间隔"""
return self._base_interval
def get_callback_count(self) -> int:
"""获取当前已注册的回调函数数量
Returns:
int: 回调函数的总数
"""
return len(self.callback_info)
def get_callback_info(self) -> Dict[Callable[[], Any], CallbackInfoType]:
"""获取所有回调函数的详细信息
Returns:
Dict: 回调函数到其信息的映射,包含间隔时间和下次执行时间
"""
info: Dict[Callable[[], Any], CallbackInfoType] = {}
current_time: dt.datetime = TimeManagerFactory.get_instance().get_current_time()
for cb_id, data in self.callback_info.items():
if cb_id in self._callback_refs:
callback = self._callback_refs[cb_id]()
if callback is not None:
callback_info: CallbackInfoType = {
'interval': data['interval'],
'last_run': data['last_run'],
'next_run': data['next_run'],
'time_until_next': (
(data['next_run'] - current_time).total_seconds()
if isinstance(data['next_run'], dt.datetime)
else 0.0
),
}
info[callback] = callback_info
return info
def get_next_check_time(self) -> Optional[dt.datetime]:
"""获取下次检查时间"""
return self._next_check_time
def get_heap_size(self) -> int:
"""获取当前任务堆中的任务数量
Returns:
int: 堆中待执行任务的数量
"""
return len(self.task_heap)
def is_running(self) -> bool:
"""检查定时器是否正在运行"""
return self._is_running
# 匹配中文字符(预编译)
_CHINESE_CHAR_PATTERN = re.compile(
r"[\u4e00-\u9fff\u3400-\u4dbf\u20000-\u2a6df\u2a700-\u2b73f\u2b740-\u2b81f\u2b820-\u2ceaf\u2ceb0-\u2ebef]"
)
def get_str_length(text: str) -> int:
"""
计算字符串长度,汉字计为2,英文和数字计为1
Args:
text: 要计算的字符串
Returns:
int: 字符串长度
"""
chinese_count = len(_CHINESE_CHAR_PATTERN.findall(text))
# 总长度 = 非中文字符数 + 中文字符数 * 2
return len(text) - chinese_count + chinese_count * 2
def slice_str_by_length(text: str, max_length: int) -> str:
"""
根据指定长度切割字符串,汉字计为2,英文和数字计为1
Args:
text: 要切割的字符串
max_length: 最大长度
Returns:
str: 切割后的字符串
"""
if not text or max_length <= 0:
return ""
if get_str_length(text) <= max_length:
return text
chars = _CHINESE_CHAR_PATTERN.split(text)
chinese_chars = _CHINESE_CHAR_PATTERN.findall(text)
result = []
current_length = 0
char_index = 0
chinese_index = 0
# 交替处理非中文和中文字符
while char_index < len(chars):
# 添加非中文部分
part = chars[char_index]
if current_length + len(part) > max_length:
# 若超出长度限制,只取部分
space_left = max_length - current_length
result.append(part[:space_left])
break
result.append(part)
current_length += len(part)
# 添加中文部分
if chinese_index < len(chinese_chars):
if current_length + 2 > max_length:
break
result.append(chinese_chars[chinese_index])
current_length += 2
chinese_index += 1
char_index += 1
return ''.join(result)
class TimeManagerInterface(ABC):
"""时间管理器接口"""
@abstractmethod
def get_real_time(self) -> dt.datetime:
"""获取真实当前时间(无偏移)"""
pass # noqa
@abstractmethod
def get_current_time(self) -> dt.datetime:
"""获取程序内时间 (偏移后)"""
pass # noqa
@abstractmethod
def get_current_time_without_ms(self) -> dt.datetime:
"""获取程序内时间 (偏移后, 舍去毫秒)"""
pass # noqa
@abstractmethod
def get_current_time_str(self, format_str: str = '%H:%M:%S') -> str:
"""获取格式化时间字符串"""
pass # noqa
@abstractmethod
def get_today(self) -> dt.date:
"""获取今天日期 (偏移后)"""
pass # noqa
@abstractmethod
def get_current_weekday(self) -> int:
"""获取当前星期几 (0=周一, 6=周日)"""
pass # noqa
@abstractmethod
def sync_with_ntp(self) -> bool:
"""同步NTP时间"""
pass # noqa
class LocalTimeManager(TimeManagerInterface):
"""本地时间管理器"""
def __init__(self, config: Optional[Any] = None) -> None:
self._config_center = config or config_center
def get_real_time(self) -> dt.datetime:
"""获取真实当前时间"""
return dt.datetime.now()
def get_current_time(self) -> dt.datetime:
"""获取程序时间(含偏移)"""
time_offset = float(self._config_center.read_conf('Time', 'time_offset', 0))
return self.get_real_time() - dt.timedelta(seconds=time_offset)
def get_current_time_without_ms(self) -> dt.datetime:
"""获取程序时间(含偏移, 舍去毫秒)"""
return self.get_current_time().replace(microsecond=0)
def get_current_time_str(self, format_str: str = '%H:%M:%S') -> str:
"""获取格式化时间字符串"""
return self.get_current_time().strftime(format_str)
def get_today(self) -> dt.date:
"""获取今天日期"""
return self.get_current_time().date()
def get_current_weekday(self) -> int:
"""获取当前星期几(0=周一, 6=周日)"""
return self.get_current_time().weekday()
def get_time_offset(self) -> float:
"""获取时差偏移(秒)"""
return float(self._config_center.read_conf('Time', 'time_offset', 0))
def sync_with_ntp(self) -> bool:
"""为什么"""
logger.warning("本地时间管理器不支持NTP同步")
return False
class NTPTimeManager(TimeManagerInterface):
"""NTP时间管理器"""
_config_center: Any
_ntp_reference_time: Optional[dt.datetime]
_ntp_reference_timestamp: Optional[float]
_lock: threading.Lock
_use_fallback: bool
_last_sync_time: float
_sync_debounce_interval: float
_pending_sync_timer: Optional[Any]
_sync_thread: Optional[threading.Thread]
_running: bool
def __init__(self, config: Optional[Any] = None) -> None:
self._config_center = config or config_center
self._ntp_reference_time = None
self._ntp_reference_timestamp = None
self._lock = threading.Lock()
self._use_fallback = True
self._last_sync_time = 0
self._sync_debounce_interval = 3.5
self._pending_sync_timer = None
self._sync_thread = None
self._running = True
self._start_sync_thread()
# logger.debug("NTP时间管理器初始化完成")
def _start_sync_thread(self) -> None:
"""启动后台同步线程"""
self._sync_thread = threading.Thread(target=self._background_sync, daemon=True)
self._sync_thread.start()
def _background_sync(self) -> None:
"""后台NTP同步"""
try:
# 初始同步
if self.sync_with_ntp():
with self._lock:
self._use_fallback = False
else:
logger.warning("NTP同步失败,继续使用系统时间")
# 周期性同步 (每小时一次)
while self._running:
time.sleep(3600) # 1小时
if not self._running:
break
try:
self.sync_with_ntp()
except Exception as e:
logger.error(f"周期性NTP同步异常: {e}")
except Exception as e:
logger.error(f"NTP同步线程异常: {e}")
def _sync_ntp_internal(self, timeout: float = 5.0) -> bool:
"""执行NTP同步"""
ntp_server = self._config_center.read_conf('Time', 'ntp_server', 'ntp.aliyun.com')
try:
ntp_client = ntplib.NTPClient()
response = ntp_client.request(ntp_server, version=3, timeout=timeout)
ntp_timestamp = response.tx_time
ntp_time_utc = dt.datetime.fromtimestamp(ntp_timestamp, dt.timezone.utc)
timezone_setting = self._config_center.read_conf('Time', 'timezone', 'local')
ntp_time_local = self._convert_to_local_time(ntp_time_utc, timezone_setting)
with self._lock:
self._ntp_reference_time = ntp_time_local
self._ntp_reference_timestamp = time.time()
logger.debug(
f"NTP同步成功: 服务器={ntp_server},时间={ntp_time_local}(local),延迟={response.delay:.3f}秒"
)
return True
except Exception as e:
logger.error(f"NTP同步失败: {e}")
return False
def _convert_to_local_time(self, utc_time: dt.datetime, timezone_setting: str) -> dt.datetime:
"""将UTC时间转换为本地时间"""
if not timezone_setting or timezone_setting == 'local':
local_tz = dt.datetime.now().astimezone().tzinfo
if utc_time.tzinfo is None:
utc_time_with_tz = utc_time.replace(tzinfo=dt.timezone.utc)
else:
utc_time_with_tz = utc_time
local_time = utc_time_with_tz.astimezone(local_tz)
return local_time.replace(tzinfo=None)
try:
utc_tz = pytz.UTC
target_tz = pytz.timezone(timezone_setting)
if utc_time.tzinfo is None:
utc_time = utc_tz.localize(utc_time)
local_time = utc_time.astimezone(target_tz)
return local_time.replace(tzinfo=None)
except Exception as e:
logger.warning(f"时区转换失败,回退系统时区: {e}")
local_tz = dt.datetime.now().astimezone().tzinfo
if utc_time.tzinfo is None:
utc_time_with_tz = utc_time.replace(tzinfo=dt.timezone.utc)
else:
utc_time_with_tz = utc_time
local_time = utc_time_with_tz.astimezone(local_tz)
return local_time.replace(tzinfo=None)
def get_real_time(self) -> dt.datetime:
"""获取真实当前时间"""
with self._lock:
if self._use_fallback or self._ntp_reference_time is None:
return dt.datetime.now()
elapsed_seconds = time.time() - (self._ntp_reference_timestamp or 0)
return self._ntp_reference_time + dt.timedelta(seconds=elapsed_seconds)
def get_current_time(self) -> dt.datetime:
"""获取程序时间(含偏移)"""
time_offset = float(self._config_center.read_conf('Time', 'time_offset', 0))
return self.get_real_time() - dt.timedelta(seconds=time_offset)
def get_current_time_without_ms(self) -> dt.datetime:
"""获取程序时间(含偏移, 舍去毫秒)"""
return self.get_current_time().replace(microsecond=0)
def get_current_time_str(self, format_str: str = '%H:%M:%S') -> str:
"""获取格式化时间字符串"""
return self.get_current_time().strftime(format_str)
def get_today(self) -> dt.date:
"""获取今天日期"""
return self.get_current_time().date()
def get_current_weekday(self) -> int:
"""获取当前星期几(0=周一, 6=周日)"""
return self.get_current_time().weekday()
def get_time_offset(self) -> float:
"""获取时差偏移(秒)"""
return float(self._config_center.read_conf('Time', 'time_offset', 0))
def sync_with_ntp(self) -> bool:
"""进行NTP同步"""
current_time = time.time()
if current_time - self._last_sync_time < self._sync_debounce_interval:
# logger.debug(f"NTP同步防抖({current_time - self._last_sync_time:.1f}秒),延迟执行同步")
if self._pending_sync_timer:
self._pending_sync_timer.cancel()
remaining_time = self._sync_debounce_interval - (current_time - self._last_sync_time)
self._pending_sync_timer = threading.Timer(remaining_time, self._delayed_sync)
self._pending_sync_timer.start()
return True
if self._pending_sync_timer:
self._pending_sync_timer.cancel()
self._pending_sync_timer = None
return self._execute_sync()
def _delayed_sync(self) -> None:
"""延迟执行的同步"""
self._pending_sync_timer = None
self._execute_sync()
def _execute_sync(self) -> bool:
"""执行实际的NTP同步"""
success = self._sync_ntp_internal(timeout=5.0)
if success:
with self._lock:
self._use_fallback = False
self._last_sync_time = time.time()
return success
def get_last_ntp_sync(self) -> Optional[dt.datetime]:
"""获取上次NTP同步时间"""
with self._lock: