Files
receipt_maker/CLAUDE.md
T
adamfandClaude Opus 5.5 ba92f1fbda
CI / test (3.10) (push) Successful in 1m20s
CI / test (3.12) (push) Successful in 1m49s
CI / test (3.13) (push) Successful in 1m5s
Add scheduled and event-driven printing with a new web UI
The web server now runs a scheduler over schedules kept in SQLite
(~/.local/share/receipt-maker, outside the clone). A schedule is a
trigger, a receipt made of blocks, and options:

- Triggers: once, recurring (rrule-based, DST-safe), sun times,
  reminders before calendar (ICS) events, RSS/JSON watchers, webhooks
  with per-schedule secrets.
- Blocks, rendered fresh as one job: templated notes ({date},
  {days_until:..}, rotating {choice}, trigger vars), QR, saved images,
  random image, calendar agenda, feed headlines, dividers.
- Rules: quiet hours (hold/skip/ignore), daily cap, pause, catch-up
  window for missed runs (never replayed), retries on connect failure
  only; a mid-job drop (new PrinterDropped) is never resent.

receipt.py renders through composable Blocks (render_note/image/qr are
wrappers, output unchanged). Print history persists, with Print again.
The page moves to web/ with Print (note/image/QR + paper preview),
Schedules (presets, editor with live preview and next runs), History
and Settings tabs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 23:58:26 +01:00

4.2 KiB

Receipt Maker

CLI that prints images, notes, and QR codes to an Epson TM-T20II thermal receipt printer (Ethernet via USB adapter, or direct USB with --usb).

Layout

  • receipt.py — the CLI; subcommand per cmd_* function
  • receipt_tui.py — Textual TUI (tui subcommand); thin layer over the cmd_* handlers via receipt.run_action(), which captures stdout/stderr for the log panel and returns an ActionResult (rc, output, error kind). textual is an optional dependency ([tui] extra); the CLI works without it
  • receipt_web.py — FastAPI server (serve subcommand, [web] extra) behind the site's nginx /receipt/ over Tailscale; requires the X-Receipt-Token header nginx adds, and X-Requested-With: receipt on POSTs. POSTs render the job in the request (InvalidJob → 400), queue the bytes in a PrintQueue and answer 202; one worker thread sends them, and the page long-polls /api/jobs/{id}. /api/status only tries the printer lock, so it never waits behind a print. Uploads print from memory. Always runs with no_setup (no sudo; the printer must already be reachable). Deployed on stppi, which is online over Wi-Fi and has the printer cabled to its eth0 (printer 192.168.192.168, Pi 192.168.192.10 via the receipt-printer nmcli profile)
  • Scheduling (all run inside serve; [web] extra): receipt_store.py (SQLite at $RECEIPT_DATA_DIR/receipt.db, default ~/.local/share/receipt-maker; additive-only migrations so update.sh rollbacks still read it), receipt_models.py (pydantic specs: a schedule is trigger + blocks + options), receipt_triggers.py (next fire times, DST-safe, plain-English describe()), receipt_sources.py (ICS/RSS/JSON fetch + parse), receipt_content.py (blocks → receipt.Blocks, {var} template fill — never str.format), receipt_scheduler.py (tick(now) is deterministic; tests drive it with a fake clock). Webhooks are POST /hook/{id} with a per-schedule secret
  • web/ — the page (vanilla ES modules, no build step), served by token-gated routes with relative URLs
  • deploy/ — systemd user units; update.sh (run by receipt-update.timer) tests the new Gitea commit in a scratch worktree, then fast-forwards the server's clone, restarts receipt-web and rolls back if /healthz doesn't answer. Pushing to Gitea master is the deploy
  • connect_receipt.sh — brings up the printer's network interface (needs sudo); run automatically before any print over Ethernet. The TUI runs it with output captured (PrinterConfig.quiet_setup, set by run_action()) so it can't corrupt the screen
  • tests/ — pytest suite; uses FakePrinter + mocked requests, never touches hardware or network

Conventions

  • Printer/network constants live at the top of receipt.py; IP/port overridable via --ip/--port or RECEIPT_PRINTER_IP/RECEIPT_PRINTER_PORT
  • Receipts are lists of receipt.Block (height, ESC/POS build, preview parts); render_blocks() makes one job of them and refuses one over MAX_PRINT_HEIGHT_DOTS
  • Every print is render_*() → bytes → send(config, payload): render() builds the job on an escpos Dummy (ESC @ first, cut last), and send() connects fresh, writes it once and closes. Only the connect is retried; a mid-job drop raises PrinterDropped (a PrinterUnavailable) and is never replayed, by send() or by the scheduler's retries. cmd_* handlers take a PrinterConfig, not a printer handle
  • User errors raise InvalidJob, printer trouble raises PrinterUnavailable; main(), run_action() and the web server turn them into messages / status codes
  • Images are prepared in memory (prepare_for_print: white-composite alpha → resize to 576px → autocontrast → 1-bit dither); nothing is written except the img/ download cache. MAX_PRINT_HEIGHT_DOTS caps paper per job (tall images shrink, long notes are refused)
  • run_command() dispatches every subcommand except tui/serve, for both main() and run_action() (via the PRINT_COMMANDS registry)
  • --no-setup (PrinterConfig.no_setup) skips the connect script everywhere it would run; check it with config.needs_setup

Checks

pytest          # required
ruff check .    # lint (config in pyproject.toml)

Actual printing can only be verified with the physical printer attached.