/ ctiger / signals.py
signals.py
 1  # Copyright (c) 2024 Anoduck
 2  # 
 3  # This software is released under the MIT License.
 4  # https://opensource.org/licenses/MIT
 5  
 6  import signal
 7  import sys
 8  import threading
 9  from proclog import ProcLog
10  from .hidden_dragon.db import HDDB
11  
12  
13  class SignalHandler(threading.Thread):
14      def __init__(self, signal, handler):
15          """
16          Initialize the signal and handler for the object.
17  
18          Parameters:
19              signal (object): The signal to be initialized.
20              handler (object): The handler for the signal.
21  
22          Returns:
23              None
24          """
25          super().__init__()
26          self.signal = signal
27          self.handler = handler
28          self.log = ProcLog().get_log()
29  
30          signal.signal(self.signal, self.handler)
31          self.start()
32  
33      def __del__(self):
34          """
35          Destructor method to clean up resources and reset signal handling.
36          """
37          signal.signal(self.signal, signal.SIG_DFL)
38  
39      def handler(self, signal=signal.SIGINT, frame=None) -> None:
40          """
41          A function to handle a specific signal, perform shutdown tasks, and exit the program.
42          
43          Args:HDDB.close
44              signal (int): The signal to be handled (default is signal.SIGINT).
45              frame (object): The current stack frame (default is None).
46          
47          Returns:
48              None
49          """
50          print('You pressed Ctrl+C!')
51          print('Ok, gracefully shutting down...')
52          # Place shutting down code here
53          db = HDDB()
54          db.close()
55          sys.exit(0)
56  
57      def __main__(self):
58          """
59          A description of the entire function, its parameters, and its return types.
60          """
61          handler = self.handler()
62          signal.signal(signal.SIGINT, handler)
63          all_threads = threading.enumerate()
64          for thread in all_threads:
65              thread.join()