feat: add JSON and CSV output formats #7
Thanks @SHIVANSH-ux-ys, the shape is right. Backward-compatible --format flag, both new methods preserve redaction, the README table reads cleanly. One blocking item and two smaller things before merge.
Blocking: json.dump needs ensure_ascii=False
Both write_json and the inner json.dumps in write_csv default to ensure_ascii=True, which escapes every non-ASCII character into \uXXXX. That breaks Cyrillic content in URLs and bodies (and any other non-ASCII text the input may carry). It is the same kind of regression v1.0.1 fixed for the input path (issue #3, UnicodeDecodeError on non-ASCII input). Same shape now landing on the output side.
Two-line fix:
# write_json
with open(output_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# write_csv (inner)
row["headers"] = json.dumps(row.get("headers", {}), ensure_ascii=False)
row["body"] = json.dumps(row.get("body"), ensure_ascii=False)
Recommended: one Cyrillic test in tests/test_formats.py
The repo already has a baseline tests/test_input_validation.py::test_cyrillic_in_url_and_body_parses from the v1.0.1 fix. Mirroring that on the output side prevents the regression above from sneaking back in. Rough shape:
def test_write_json_preserves_cyrillic(tmp_path):
entry = KibanaLogEntry(
method="POST",
url="/api/v1/пользователи/профиль",
status=200,
headers={"X-Запрос-Id": "значение"},
body={"имя": "Михаил"},
)
output = tmp_path / "out.json"
KibanaTestGenerator(tmp_path).write([entry], output, output_format="json")
text = output.read_text(encoding="utf-8")
assert "пользователи" in text
assert "\\u" not in text # ensure_ascii=False guard
Smaller things
fieldnames = ["method", "url", "status", "duration", "headers", "body"] in write_csv will drift if KibanaLogEntry gains a field. Deriving from KibanaLogEntry.model_fields.keys() is a cheap future-proof.output_format raises ValueError rather than silently doing nothing. The dispatch already raises, just no test covers it.# templates dir not needed for json is true today, but if KibanaTestGenerator.__init__ ever starts validating templates upfront, the json/csv tests will start failing for an unrelated reason. Worth flagging in a docstring on write_json / write_csv that the constructor argument is unused for non-pytest formats, or pass templates=None explicitly.Happy to merge once the ensure_ascii=False and Cyrillic test land. Push to the same branch and I will re-review.
Hi @golikovichev,
I've pushed the fixes and resolved the merge conflicts with the latest main branch..
Hi @golikovichev,
I've pushed the fixes and resolved the merge conflicts with the latest main branch!
Specifically:
1. Passed ensure_ascii=False in both the write_json and write_csv serializations so Cyrillic/non-ASCII characters are fully preserved in the output files.
2. 2. Added a robust Cyrillic formatting test in tests/test_formats.py validating that raw Unicode content is intact in both formats without any escaping.
3. 3. Merged the latest main upstream changes, combining the new --max-input-mb size guard with the --format selection cleanly.
All 67 tests are passing. Let me know if you need anything else before merge!
Thanks @SHIVANSH-ux-ys, both blocking items are addressed: ensure_ascii=False lands in write_json and the inner write_csv json.dumps, and test_cyrillic_output_formats mirrors the input-side guard from #3. The fix is correct.
Three smaller items from the previous review are still open:
fieldnames in write_csv is still the hardcoded list. Deriving from KibanaLogEntry.model_fields.keys() keeps the CSV in sync if the entry model gains a field later.output_format="bogus" raises ValueError. The dispatch already raises; just one short test that catches it would lock the behaviour in.# templates dir not needed for json comment in the new tests is still there. A one-line docstring on write_json / write_csv saying the constructor templates arg is unused for non-pytest formats is cleaner than the inline comment.CI didn't run on this branch - first-time contributor workflow approval. I'll approve the run after these are in, and we can land it.
"Thanks for the review! I've pushed a commit addressing those 3 remaining items. Let me know if everything looks good to go now."
Thanks @SHIVANSH-ux-ys, all three items addressed cleanly: fieldnames derived from KibanaLogEntry.model_fields, test_bogus_format_raises_error locks the dispatch behaviour, and the inline comments moved to docstrings on write_json and write_csv. CI matrix is green across the 12 OS / Python combinations. Squash-merging.
golikovichev
ShivanshhhG
This PR implements Issue #5 by adding support for JSON and CSV output formats.
Changes
Verification