diff --git a/BasaltTrainer/.style.yapf b/BasaltTrainer/.style.yapf deleted file mode 100644 index 431ff9b..0000000 --- a/BasaltTrainer/.style.yapf +++ /dev/null @@ -1,2 +0,0 @@ -# The column limit. -column_limit=79 \ No newline at end of file diff --git a/BasaltTrainer/cmd_server.py b/BasaltTrainer/cmd_server.py index fa00887..61bd318 100644 --- a/BasaltTrainer/cmd_server.py +++ b/BasaltTrainer/cmd_server.py @@ -68,15 +68,15 @@ class CmdServer: # Send handshake request to Presenter self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.HANDSHAKE_REQUEST)) # And the payload data - self.__pipe_operator.write(HANDSHAKE_REQUEST_PACKER.pack( - payload.pixel_kind, - payload.width, - payload.height - )) + self.__pipe_operator.write( + HANDSHAKE_REQUEST_PACKER.pack( + payload.pixel_kind, payload.width, payload.height + ) + ) # Wait for handshake response from Presenter (code 0x62) code_bytes = self.__pipe_operator.read(CODE_PACKER.size) - (code, ) = CODE_PACKER.unpack(code_bytes) + (code,) = CODE_PACKER.unpack(code_bytes) if ProtocolCode(code) != ProtocolCode.HANDSHAKE_RESPONSE: raise RuntimeError("expect handshake response code, but got another") @@ -84,8 +84,7 @@ class CmdServer: self.__status = ServerStatus.Running return - def tick(self, data_receiver: Callable[[], None], - request_stop: bool) -> bool: + def tick(self, data_receiver: Callable[[], None], request_stop: bool) -> bool: """ Tick function called every frame to wait for data ready from Presenter and send response. Returns True if a stop code was received (meaning the process should stop), False otherwise. @@ -101,7 +100,7 @@ class CmdServer: while True: # Wait for code from Presenter code_bytes = self.__pipe_operator.read(CODE_PACKER.size) - (code, ) = CODE_PACKER.unpack(code_bytes) + (code,) = CODE_PACKER.unpack(code_bytes) # Analyse code match ProtocolCode(code): @@ -109,7 +108,9 @@ class CmdServer: # Receive data data_receiver() # Send data received symbol - self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.DATA_RECEIVED)) + self.__pipe_operator.write( + CODE_PACKER.pack(ProtocolCode.DATA_RECEIVED) + ) return False case ProtocolCode.ACTIVELY_STOP: # Presenter requested stop. @@ -119,7 +120,6 @@ class CmdServer: case _: raise RuntimeError("unexpected protocol code when running") - def __wait_stop(self) -> None: # Send stop request code self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP_REQUEST)) @@ -128,7 +128,7 @@ class CmdServer: while True: # Accept code code_bytes = self.__pipe_operator.read(CODE_PACKER.size) - (code, ) = CODE_PACKER.unpack(code_bytes) + (code,) = CODE_PACKER.unpack(code_bytes) # Check whether it is stop response match ProtocolCode(code): @@ -138,5 +138,3 @@ class CmdServer: return case _: raise RuntimeError("unexpected protocol code when waiting quit") - - diff --git a/BasaltTrainer/main.py b/BasaltTrainer/main.py index 1ce799b..3fd2635 100644 --- a/BasaltTrainer/main.py +++ b/BasaltTrainer/main.py @@ -1,39 +1,36 @@ from cmd_server import CmdServer, HandshakePayload, PixelKind -from while_stopper import WhileStopper +from while_stopper import INSTANCE as STOPPER import logging -import signal def receive_data() -> None: - logging.info('Data received') + logging.info("Data received") def main(): server = CmdServer() - print('Please launch BasaltPresenter now.') - print('Then press Enter to continue...') + print("Please launch BasaltPresenter now.") + print("Then press Enter to continue...") input() - logging.info('Waiting BasaltPresenter...') + logging.info("Waiting BasaltPresenter...") server.wait_handshake(HandshakePayload(PixelKind.GRAY_U8, 600, 600)) - logging.info('Start to running.') - stopper = WhileStopper() - stopper.register() + logging.info("Start to running.") + STOPPER.register() while True: - if server.tick(receive_data, stopper.is_stop_requested()): + if server.tick(receive_data, STOPPER.is_stop_requested()): break - stopper.unregister() + STOPPER.unregister() - logging.info('Program stop.') - - print('Press Enter to exit...') - input() + logging.info("Program stop.") if __name__ == "__main__": - logging.basicConfig(level=logging.DEBUG, - format='[%(asctime)s] [%(levelname)s] %(message)s', - datefmt='%Y-%m-%d %H:%M:%S') + logging.basicConfig( + level=logging.DEBUG, + format="[%(asctime)s] [%(levelname)s] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + ) main() diff --git a/BasaltTrainer/pipe_operator.py b/BasaltTrainer/pipe_operator.py index 3cd8790..1236b67 100644 --- a/BasaltTrainer/pipe_operator.py +++ b/BasaltTrainer/pipe_operator.py @@ -24,7 +24,7 @@ class PipeOperator: def __init__(self, name: str): """ Initialize the PipeOperator. - + :param name: Name of the pipe :param is_server: True if this instance should create the pipe (server), False if connecting to existing pipe (client) """ @@ -35,13 +35,14 @@ class PipeOperator: self.pipe_handle = win32pipe.CreateNamedPipe( self.pipe_name, win32pipe.PIPE_ACCESS_DUPLEX, - win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE + win32pipe.PIPE_TYPE_BYTE + | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT, 1, # Number of pipe instances 65536, # Output buffer size 65536, # Input buffer size 0, # Default timeout - None # Security attributes + None, # Security attributes ) if self.pipe_handle == win32file.INVALID_HANDLE_VALUE: raise RuntimeError("Failed to create named pipe.") @@ -78,7 +79,7 @@ class PipeOperator: def read(self, size: int) -> bytes: """ Read data from the pipe. This is a blocking operation. - + :param size: Number of bytes to read :return: Bytes read from the pipe """ @@ -96,8 +97,7 @@ class PipeOperator: data: Any (hr, data) = win32file.ReadFile(self.pipe_handle, size) if hr != 0: # Error occurred - raise RuntimeError( - f"Failed to read from pipe, error code: {hr}") + raise RuntimeError(f"Failed to read from pipe, error code: {hr}") data = bytes(data) if len(data) != size: raise RuntimeError( @@ -128,7 +128,7 @@ class PipeOperator: def write(self, data: bytes) -> None: """ Write data to the pipe. This is a blocking operation. - + :param data: Data to write to the pipe """ if len(data) == 0: @@ -147,8 +147,7 @@ class PipeOperator: total_written = 0 while total_written < len(data): try: - bytes_written = os.write(self.pipe_handle, - data[total_written:]) + bytes_written = os.write(self.pipe_handle, data[total_written:]) total_written += bytes_written except OSError as e: raise RuntimeError(f"Failed to write to named pipe: {e}") diff --git a/BasaltTrainer/pyproject.toml b/BasaltTrainer/pyproject.toml index 254ed41..bddcb84 100644 --- a/BasaltTrainer/pyproject.toml +++ b/BasaltTrainer/pyproject.toml @@ -26,3 +26,5 @@ name = "pytorch-cu126" url = "https://download.pytorch.org/whl/cu126" explicit = true +[too.ruff] +line-length = 88 diff --git a/BasaltTrainer/while_stopper.py b/BasaltTrainer/while_stopper.py index c8648a0..17d2047 100644 --- a/BasaltTrainer/while_stopper.py +++ b/BasaltTrainer/while_stopper.py @@ -9,44 +9,49 @@ else: class WhileStopper: - - is_registered: bool - stop_requested: bool + _is_registered: bool + _stop_requested: bool def __init__(self) -> None: - self.is_registered = False - self.stop_requested = False + self._is_registered = False + self._stop_requested = False def is_stop_requested(self) -> bool: - if not self.is_registered: - raise RuntimeError('unexpected stopper status') + if not self._is_registered: + raise RuntimeError("unexpected stopper status") - return self.stop_requested + return self._stop_requested def register(self): - if self.is_registered: - raise RuntimeError('unexpected stopper status') + if self._is_registered: + raise RuntimeError("unexpected stopper status") - self.stop_requested = False + self._stop_requested = False if IS_WINDOWS: - win32api.SetConsoleCtrlHandler(self.__win_handler, True) + win32api.SetConsoleCtrlHandler(_win_handler, True) else: - signal.signal(signal.SIGINT, self.__posix_handler) - self.is_registered = True + signal.signal(signal.SIGINT, _posix_handler) + self._is_registered = True def unregister(self): - if not self.is_registered: - raise RuntimeError('unexpected stopper status') + if not self._is_registered: + raise RuntimeError("unexpected stopper status") if IS_WINDOWS: - win32api.SetConsoleCtrlHandler(None, False) + win32api.SetConsoleCtrlHandler(_win_handler, False) else: signal.signal(signal.SIGINT, signal.SIG_DFL) - self.is_registered = False + self._is_registered = False - def __win_handler(self, ctrl_type: int) -> bool: - self.stop_requested = True - return True - def __posix_handler(self, signal, frame) -> None: - self.stop_requested = True +INSTANCE: WhileStopper = WhileStopper() +"""The singleton of WhileStopper""" + + +def _win_handler(ctrl_type: int) -> bool: + INSTANCE._stop_requested = True + return True + + +def _posix_handler(signal, frame) -> None: + INSTANCE._stop_requested = True