Repository navigation
asyncio/aiohttp servers, uploads to the website, history settings and a startup cache - #16
Merged
Merged
Conversation
asyncore and asynchat are gone from python 3.12, and the hand-written HTTP server was the largest part of what depended on them. lpdserver.py is now an asyncio stream protocol and httpserver.py serves the same path handlers (and the static files) with aiohttp. Both run on one event loop, as before, so handlers still see a single thread and the job list needs no locking; main.py starts them with asyncio.run, writes the print history every ten seconds and on SIGTERM as well as Ctrl+C. LPD behaviour is unchanged, including senders that close the connection to mark the end of the data (even by reset), the trailing null on a sized data file and ignoring unknown commands. 1,089 production jobs replayed through it with tools/replay_corpus.py arrive byte for byte. Directory listings from the static file server are gone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
The mail queue is an asyncio.Queue worked by a task on the loop, and the blocking smtplib call runs through asyncio.to_thread. The results list and its pickle are now only touched on the loop, so there is no thread sharing them. Queueing no longer blocks the web server if the queue is full: it raises, and the send form already reports "unable to queue email". Shutdown waits up to 30 seconds for queued mail to go. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
webupload.py turns a parsed job into the website's JSON, one document per invoice, credit note or statement, and cuts the account PDF into a PDF per document (a document can span pages). uploader.py queues them on the event loop, posts each as multipart with the API token in a worker thread, retries network failures and 5xx, and records the outcome in upload.pickle. The job page gets an "Upload to website" button when [Web] base_url and api_key are set, an "Uploaded from this job" table, and the home page counts failures. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
…ings The sent emails, printed documents and uploads each remember their own number of records, set under [Gmail], [Printing] and [Web] result_count, so they can be sized independently. Printing defaults to 200 and uploads to 120. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
Settings -> Web Uploads shows the configured website (as a link) and the last three characters of the API key, the automatic upload kinds, and the queue and recent failures. A new Uploads page lists recent uploads. [Web] auto_types (invoice, crednote, statement) queues documents as they arrive, through the same queue as the Upload button. The "Uploaded from this job" table links with docnum / account. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
The fields the parser found are only used to colour in the debug page, and are about twice the size of the parsed pages, so they are no longer stored on the job: debug() parses again for the one job it shows. The PDFs are made from the parsed pages alone, so whether to format now depends on `parsed`. Every extractor adds its fields and its parsed page together, so this gives the same answer as before (checked on all 1,089 stored jobs). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
~/printerface/derived.pickle holds, for each job file in the loaded history, its parsed pages and the names of its PDFs, with the size and modification time of the job file and a stamp of the parser code. At startup a job whose entry is still good takes them from the cache, and its PDFs are only drawn again if they have gone from disk: a thousand jobs start in under a second, against about eight. A job file that has changed, or a different parser, means that job is parsed again. The file is rewritten, atomically, when something changed, at most every three minutes and at shutdown, and holds only the jobs still loaded. A change to the stationery does not redraw anything by itself. Settings > Stationery counts the documents drawn under older stationery (stationary.py, the [Printing] company details, the logos) and redraws them on request. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
Every poll of the control loop looks at the two newest month directories for job files that are not in the history: copied in from another instance, say. Once a file has been still for two seconds it is loaded (through the derived cache), put into the history in file name order, and run through auto-print and auto-upload exactly as a job that arrived over LPD is. A file older than everything in a full history is ignored, one that will not load is tried again only if its size changes, and a job that arrived over LPD is never found again. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Eight commits, in the order they build on each other.
Servers on asyncio and aiohttp
smtplibin a worker thread.Uploads to the website
webupload.pyturns a parsed invoice, credit note or statement into the website's JSON and cuts the account PDF into one PDF per document (a document can span pages).uploader.pyqueues them, posts each as multipart with the API token in a worker thread, retries network failures and 5xx, and records the outcome inupload.pickle.[Web] base_urlandapi_keyare set) and an "Uploaded from this job" table.[Web] auto_types(invoice,crednote,statement) uploads documents as they arrive.History settings
result_count([Gmail],[Printing],[Web]).Faster startup
parsedalone.~/printerface/derived.picklecaches each loaded job's parsed pages and PDF names (keyed by job file name, with its size and mtime and a parser stamp). 1,089 jobs start in 0.84 s instead of 8 s here; on merlot 306 jobs came up in about 5 s warm. The cache is only an optimisation: anything not matching is worked out again. A stationery change does not redraw by itself; Settings > Stationery counts the older documents and has a button.Testing
python -m unittest discover --pattern='*.py'withPRINTERFACE_HOMEpointing at a scratch directory: 156 tests, 1 skipped (the corpus test, which needsWEBUPLOAD_CORPUS). Run on this branch as pushed.New dependencies:
aiohttp,pypdf.🤖 Generated with Claude Code
https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP