crouching_tiger/ctiger/signals.py
anoduck 832e46bf1d feat(Features): 🚧 Work continues on development of Hidden Dragon
polishing signal reception, creation of ap class, added time class,further work on logging and features.

Hidden Dragon is unfinished, do not use.
2024-03-29 21:12:29 -04:00

65 lines
1.8 KiB
Python

# Copyright (c) 2024 Anoduck
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
import signal
import sys
import threading
from proclog import ProcLog
from .hidden_dragon.db import HDDB
class SignalHandler(threading.Thread):
def __init__(self, signal, handler):
"""
Initialize the signal and handler for the object.
Parameters:
signal (object): The signal to be initialized.
handler (object): The handler for the signal.
Returns:
None
"""
super().__init__()
self.signal = signal
self.handler = handler
self.log = ProcLog().get_log()
signal.signal(self.signal, self.handler)
self.start()
def __del__(self):
"""
Destructor method to clean up resources and reset signal handling.
"""
signal.signal(self.signal, signal.SIG_DFL)
def handler(self, signal=signal.SIGINT, frame=None) -> None:
"""
A function to handle a specific signal, perform shutdown tasks, and exit the program.
Args:HDDB.close
signal (int): The signal to be handled (default is signal.SIGINT).
frame (object): The current stack frame (default is None).
Returns:
None
"""
print('You pressed Ctrl+C!')
print('Ok, gracefully shutting down...')
# Place shutting down code here
db = HDDB()
db.close()
sys.exit(0)
def __main__(self):
"""
A description of the entire function, its parameters, and its return types.
"""
handler = self.handler()
signal.signal(signal.SIGINT, handler)
all_threads = threading.enumerate()
for thread in all_threads:
thread.join()