How crs4/hl7apy works
Project Type and Purpose
• HL7apy is an open-source Python library (not a client/server web app).
• It provides intuitive handling of HL7 v2 messages: parsing, creation, validation, and a simple MLLP server/client for message transport.
• Core Controller (hl7apy/core.py)
• Entry points: parse_message(), create_message()
• Orchestrates parser, factory, validator, MLLP
• Parser Component (hl7apy/parser.py)
• Transforms raw ER7 strings into an in-memory message tree (composite of segments→fields→datatypes)
• Loads version-specific definitions dynamically
• Factory Component (hl7apy/factories.py)
• Builds message trees programmatically using factory methods
• Data Model Modules
• Base: hl7apy/base_datatypes.py, hl7apy/consts.py
• Version folders (hl7apy/v2_1 … hl7apy/v2_8_2): datatypes, fields, groups, segments, tables
• Validation Component (hl7apy/validation.py)
• Validates message trees against HL7 XSD rules, custom profiles, Z-elements
• MLLP Transport (hl7apy/mllp.py)
• Implements MLLP framing/protocol
• Provides MLLPServer and client for sending/receiving messages over TCP
• Utilities (hl7apy/utils.py + utils/hl7apy_profile_parser)
• Helpers for name resolution, profile parsing, encoding customization
• Support Layers
• examples/ (sample iti_21 client.py & server.py)
• tests/ (unit tests via nose2.cfg)
• docs/ (Sphinx sources, custom theme)
• CI / packaging: .github/workflows, setup.py, tox.ini
Relationships and Data Flows
• ER7 string → Parser → Message tree object
• Message tree ← Factory (for programmatic creation)
• Message tree → Validator → ValidationReport
• Message tree → MLLP → External system (hospital applications)
• Core orchestrates the sequence: Parser ↔ Factory ↔ Validator ↔ MLLP
• Data Model modules “plug into” Parser and Factory via dynamic loading
Architectural Patterns & Principles
• Modular, version-based folder structure (each HL7 version is a plugin)
• Factory pattern for object creation
• Composite pattern for tree-structured messages
• Single responsibility per module: parsing, building, validating, transporting
• Clear separation of core logic vs. I/O vs. support