HL7 v2 parser, envelope mapping, LIMS push wiring (PR-7)#37
Open
ramonski wants to merge 1 commit into
Open
Conversation
Turn raw HL7 bytes into the existing Envelope so the HL7 server serves LIMS pushes the same way the ASTM server does. Closes the parser gap; instrument-specific routing (sample ID, keyword mapping, OBR-4 filtering) lands in the next PR. - transports/hl7/parser.py: parse(raw) -> Envelope. Wraps the hl7 PyPI package for defensive escape-sequence handling. Segment mapping MSH->H, PID->P, OBR->O, OBX->R, NTE->C. Raw text lands in metadata.hl7. - core/envelope.py: Metadata gains hl7 field; astm and lis2a soft- default to "" so HL7 envelopes validate without faking ASTM representations. ENVELOPE_VERSION bumped to 1.1 (additive, backwards compatible). serialize_envelope learns the "hl7" format. - core/output.py: DiskCaptureHandler gains a payload= callable so callers can pick which Metadata field gets persisted. ASTM retains the default (metadata.astm); HL7 server passes a lambda for metadata.hl7 and ext=".hl7". - cli/hl7_server.py: parses HL7 via the new parser, runs the shared Pipeline. New flags --url / --consumer / --message-format / --retries / --delay so the HL7 server can push to SENAITE the same way the ASTM server does. Without --url the server stays capture-only. - tests/data/envelopes/*.json: regenerated for envelope_version 1.1. - tests/test_envelope_schema.py: assertion that astm/lis2a are required is replaced with one verifying they soft-default; HL7 envelopes need that. - tests/test_hl7_parser.py (new, 25 tests): per-fixture envelope shape checks for fresh blood (OBS), liquid QC (LQC, ranges populated), proficiency (PRF, ranges suppressed), and the flagged variant with NTE notes. Plus serializer integration and robustness (bytes/str, LF normalization, malformed input). Suite: 340 passed, 1 skipped. flake8 clean. Next: PR-8 — HemoScreen instrument adapter. MSH-3 == "HemoScreen" resolution; sample ID from PID-2; OBR-4 == "OBS" routing (LQC/PRF captured-but-not-pushed); keyword mapping via the existing # -> _ABS / % -> _PERC convention.
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.
Turn raw HL7 bytes into the existing
Envelopeso the HL7 serverserves LIMS pushes the same way the ASTM server does. Closes the
parser gap from PR-6; instrument-specific routing (sample ID,
keyword mapping, OBR-4 filtering) lands in the next PR.
Summary
transports/hl7/parser.py—parse(raw) -> Envelope. Wrapsthe
hl7package for defensiveescape-sequence and encoding handling. Segment mapping:
MSHHPIDPOBROOBXRNTECRaw text lands in
metadata.hl7.core/envelope.py—Metadatagains anhl7field.astmandlis2aare soft-defaulted to""so HL7 envelopesvalidate without faking ASTM representations.
ENVELOPE_VERSIONbumped to1.1(additive, backwardscompatible — consumers that pin to
1.0keep working).serialize_envelopelearns the"hl7"format.core/output.py—DiskCaptureHandlergains apayload=callable so callers pick which
Metadatafield gets persisted.ASTM retains the default (
metadata.astm); HL7 server passes alambda for
metadata.hl7andext=".hl7".cli/hl7_server.py— parses HL7 via the new parser, runsthe shared
Pipeline. New flags--url / --consumer / --message-format / --retries / --delayso the HL7 server canpush to SENAITE the same way the ASTM server does. Without
--urlthe server stays capture-only.tests/data/envelopes/*.json— regenerated forenvelope_version1.1. This is the expected snapshot churn on aschema bump (called out in the refactor plan §6 / Risks).
Test plan
python -m pytest src/senaite/astm/tests/— 340 passed, 1 skipped.flake8 --config ci_flake8.cfg src/senaite/astm— clean.test_hl7_parser.py(25 tests):OBS), liquid QC(
LQC, ranges populated), proficiency (PRF, rangessuppressed), flagged-with-NTE.
LL,---) parse without crashing."hl7"mode returns raw text;"json"mode includesmetadata.hl7withastm/lis2aasempty strings.
bytesandstr; normalises\nto\r;raises
ValueErroron input without an MSH header.snapshots.
Smoke-test
A
.hl7file lands in/tmp/hl7-captures/. With--url http://user:pass@host/senaiteon the server, the parsed envelopealso gets pushed to SENAITE.
Out of scope (PR-8)
MSH-3 == "HemoScreen".OBR-4routing — onlyOBSshould reach the LIMS pipeline;LQCandPRFare debug-logged and dropped on the push side(still captured to disk).
#→_ABSand%→_PERC, which matches HemoScreen'sparameter names verbatim — no conversion table needed on the
senaite.astm side.
PID-2for the SENAITE adapter.