Render every print to a complete ESC/POS payload (reset, content, cut)
and send it once over a fresh connection. Only the connect is retried,
so a link that drops mid-job can no longer print a partial image followed
by a full one, and no text size leaks into the next job. Prints are
capped at about 1 m of paper: tall images shrink, over-long notes are
refused.
Replace the argparse/stdout plumbing with PrinterConfig, InvalidJob and
PrinterUnavailable, and one run_command() dispatcher shared by the CLI
and TUI. Status now really connects; before, escpos's lazy open meant
the web server always reported the printer ready. Rendering with the
TM-T20II profile makes QR centering take effect.
The web server validates and renders jobs in the request (400 on bad
input), queues them for a single worker and answers 202; the page
long-polls /api/jobs/{id}, so no request outlives nginx's proxy timeout
while its job still prints. Status reports busy instead of waiting, and
jobs are logged with the nginx user.
update.sh tests new commits in a scratch worktree before touching the
live clone, and rolls back if /healthz doesn't answer after the restart.
The unit runs from the venv, and the web server defaults to localhost.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
"""Shared test fakes."""
|
|
|
|
import argparse
|
|
|
|
from escpos import printer
|
|
|
|
import receipt
|
|
|
|
|
|
class FakePrinter:
|
|
"""Records the connection lifecycle and raw job payloads instead of talking to hardware."""
|
|
|
|
def __init__(self, fail_open=0, fail_raw=False):
|
|
self.calls = []
|
|
self.payloads = []
|
|
self.fail_open = fail_open # how many open() calls raise before one succeeds
|
|
self.fail_raw = fail_raw
|
|
|
|
def open(self):
|
|
self.calls.append("open")
|
|
if self.fail_open:
|
|
self.fail_open -= 1
|
|
raise OSError("no route to printer")
|
|
|
|
def _raw(self, data):
|
|
self.calls.append("raw")
|
|
if self.fail_raw:
|
|
raise OSError("connection reset")
|
|
self.payloads.append(data)
|
|
|
|
def close(self):
|
|
self.calls.append("close")
|
|
|
|
|
|
def escpos_bytes(build):
|
|
"""What the given escpos calls render to, for asserting on payloads."""
|
|
p = printer.Dummy(profile=receipt.PRINTER_PROFILE)
|
|
build(p)
|
|
return p.output
|
|
|
|
|
|
INIT = escpos_bytes(lambda p: p.hw("INIT"))
|
|
CUT = escpos_bytes(lambda p: p.cut())
|
|
|
|
|
|
def make_args(**overrides):
|
|
defaults = {
|
|
"usb": True, # avoids the network setup script in tests
|
|
"ip": "192.168.192.168",
|
|
"port": 9100,
|
|
"timestamp": False,
|
|
"message": None,
|
|
"name": None,
|
|
}
|
|
defaults.update(overrides)
|
|
return argparse.Namespace(**defaults)
|