fix: support context manager protocol on CDXToolkitWARCWriter (#68)#70
Merged
Conversation
Add __enter__/__exit__ so callers can write `with cdx_toolkit.warc.get_writer(...) as writer:` and have the WARC flushed automatically — important on S3 where the multipart upload is only committed on close, and where a mid-iteration exception used to leave the upload uncommitted. Update cli.py and examples/iter-and-warc.py to use the new idiom; the explicit close() API stays for backward compatibility.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #70 +/- ##
==========================================
+ Coverage 97.12% 97.24% +0.11%
==========================================
Files 9 9
Lines 939 943 +4
==========================================
+ Hits 912 917 +5
+ Misses 27 26 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ame cases Earlier commit accidentally swept those through a replace_all; only the new context-manager tests need info to be a dict (they actually call write_record, which triggers warcio's create_warcinfo_record).
Adds focused unit tests that drive cli.warcer() with a mocked CDX fetcher. Exercises the previously-uncovered revisit branch (the one codecov flagged on the patch coverage check) along with the fgrep / fgrepv skip paths and the --size pop-and-forward to the writer.
wumpus
approved these changes
May 27, 2026
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.
Summary
CDXToolkitWARCWriternow implements__enter__/__exit__, so callers can writewith cdx_toolkit.warc.get_writer(...) as writer:instead of relying on a manualwriter.close().close()API is preserved, so no caller is broken. Updated the CLI (warcer) andexamples/iter-and-warc.pyto demonstrate the idiomatic usage.Test plan
pytest tests/unit/— 31 passed, 1 skipped (AWS-gated), 0 failedflake8 cdx_toolkit tests— clean__enter__returns self, exceptions inside the with block propagate while still closing the file, andclose()is idempotent.