From 07096ad4aaa51087c268f580eb3738195140138c Mon Sep 17 00:00:00 2001 From: ske087 Date: Fri, 24 Apr 2026 15:52:04 +0300 Subject: [PATCH] feat: auto-install CUPS printer on startup when work_place != notconfig --- app.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/app.py b/app.py index ed30a65..ddebf29 100644 --- a/app.py +++ b/app.py @@ -477,6 +477,55 @@ print("=" * 60) print("CONTINUING WITH NORMAL INITIALIZATION" if not CONFIGURATION_MODE else "CONFIGURATION MODE ACTIVE") print("=" * 60) +# --------------------------------------------------------------------------- +# Auto-printer setup +# --------------------------------------------------------------------------- + +def _ensure_printer(device_name): + """ + Check whether a CUPS printer named *device_name* is installed. + If not found, install it automatically using lpadmin. + + Only called when work_place is set to a real name (not 'notconfig'). + """ + print(f"🖨 Checking printer for work_place='{device_name}' ...") + try: + import cups + conn = cups.Connection() + printers = conn.getPrinters() + if device_name in printers: + print(f"✓ Printer '{device_name}' already installed — no action needed.") + return + # Printer not found — install it + print(f"🖨 Printer '{device_name}' not found. Starting installation ...") + cmd = [ + "sudo", "/usr/sbin/lpadmin", + "-p", device_name, + "-E", + "-v", "usb://CITIZEN/CT-S310II?serial=00000000", + "-m", "CTS310II.ppd", + ] + result = subprocess.run(cmd, capture_output=True, text=True, timeout=30) + if result.returncode == 0: + print(f"✓ Printer '{device_name}' installed successfully.") + else: + print(f"✗ lpadmin returned code {result.returncode}: {result.stderr.strip()}") + except ImportError: + print("⚠ python3-cups not available — printer check skipped. " + "Install with: sudo apt install python3-cups") + except Exception as e: + print(f"⚠ Printer setup skipped: {e}") + + +# Run auto-printer setup only when device has a real work_place +_auto_printer_name = APP_CONFIG.get("device_name", "notconfig") +if not CONFIGURATION_MODE and _auto_printer_name.lower() != "notconfig": + _ensure_printer(_auto_printer_name) +else: + if _auto_printer_name.lower() == "notconfig": + print("🖨 Printer setup skipped (work_place=notconfig).") + + def check_system_requirements(): """ Check and set up system requirements for the application