winziprint.py: Add feature to abort jobs

This commit is contained in:
2026-01-15 23:00:15 +01:00
parent 1cf775beec
commit cf11791dad
3 changed files with 24 additions and 6 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.idea .idea
__pycache__ __pycache__
*.exe *.exe
*.spec

View File

@@ -4,4 +4,5 @@ pyinstaller ^
--console ^ --console ^
--icon "NONE" ^ --icon "NONE" ^
--name "WinziPrint" ^ --name "WinziPrint" ^
--clean ^
src/winziprint.py src/winziprint.py

View File

@@ -11,16 +11,19 @@ import socketserver
import io import io
import signal import signal
import math import math
import threading
import weasyprint import weasyprint
import pypdf import pypdf
VERSION = __version__ = '0.2.9' VERSION = __version__ = '0.3.0'
SOCKET_ADDRESS = ('127.0.0.1', 30983) SOCKET_ADDRESS = ('127.0.0.1', 30983)
BATCH_SIZE = 10 BATCH_SIZE = 10
RUNNING: dict[int, bool] = {}
def convert_part(output_file: str, batch: list[str], step_cb: Callable, encoding: str = None) -> list[int]: def convert_part(output_file: str, batch: list[str], step_cb: Callable, encoding: str = None) -> list[int]:
documents = [] documents = []
@@ -55,6 +58,8 @@ def convert(input_files: list[str],
steps = [0] steps = [0]
def next_step() -> None: def next_step() -> None:
if not RUNNING[threading.get_ident()]:
raise InterruptedError('aborted')
steps[0] += 1 steps[0] += 1
if progress: if progress:
print(f'progress: {steps[0]}/{total_steps}', file=out, flush=True) print(f'progress: {steps[0]}/{total_steps}', file=out, flush=True)
@@ -233,12 +238,23 @@ def _get_arg(args: list[str], n1: str, n2: str = None, flag: bool = False) -> No
class ConnectionHandler(socketserver.StreamRequestHandler): class ConnectionHandler(socketserver.StreamRequestHandler):
def handle(self): def handle(self):
try: try:
while True: out = io.TextIOWrapper(self.wfile, encoding='utf-8')
out = io.TextIOWrapper(self.wfile, encoding='utf-8') RUNNING[threading.get_ident()] = True
for line in io.TextIOWrapper(self.rfile, encoding='utf-8'): print(f'id: {threading.get_ident()}', file=out, flush=True)
_wrapper_convert(line.strip().split(';'), out=out) for line in io.TextIOWrapper(self.rfile, encoding='utf-8'):
except ValueError: if not RUNNING[threading.get_ident()]:
print(f'error: aborted', file=out, flush=True)
break
parts = line.strip().split(';')
if len(parts) == 2 and parts[0] == 'cancel':
thread_id = int(parts[1].strip())
RUNNING[thread_id] = False
continue
_wrapper_convert(parts, out=out)
except (ValueError, ConnectionError):
pass # socket closed by client pass # socket closed by client
finally:
del RUNNING[threading.get_ident()]
def main() -> None: def main() -> None: