def _split_msh(content): m = re.match("^MSH(?P<field_sep>\S)", content) if m is not None: # if the regular expression matches, it is an HL7 message field_sep = m.group('field_sep') # get the field separator (first char after MSH) msh = content.split("\r", 1)[0] # get the first segment fields = msh.split(field_sep) seps = fields[1] # get the remaining encoding chars (MSH.2) if len(seps) > len(set(seps)): raise InvalidEncodingChars("Found duplicate encoding chars") try: comp_sep, rep_sep, escape, sub_sep = seps except ValueError: if len(seps) < N_SEPS: raise InvalidEncodingChars('Missing required encoding chars') else: raise InvalidEncodingChars('Found {0} encoding chars'.format(len(seps))) else: encoding_chars = { 'FIELD': field_sep, 'COMPONENT': comp_sep, 'SUBCOMPONENT': sub_sep, 'REPETITION': rep_sep, 'ESCAPE': escape, 'SEGMENT': '\r', 'GROUP': '\r', } else: raise ParserError("Invalid message") return fields, encoding_chars
def check_encoding_chars(encoding_chars): """ Validate the given encoding chars :type encoding_chars: ``dict`` :param encoding_chars: the encoding chars (see :func:`hl7apy.set_default_encoding_chars`) :raises: :exc:`hl7apy.exceptions.InvalidEncodingChars` if the given encoding chars are not valid """ if not isinstance(encoding_chars, collections.MutableMapping): raise InvalidEncodingChars required = {'FIELD', 'COMPONENT', 'SUBCOMPONENT', 'REPETITION', 'ESCAPE'} missing = required - set(encoding_chars.keys()) if missing: raise InvalidEncodingChars('Missing required encoding chars') values = [v for k, v in encoding_chars.items() if k in required] if len(values) > len(set(values)): raise InvalidEncodingChars('Found duplicate encoding chars')