Commits

John Paulett committed 375d8eb

Parse the message according to the separators specified in the MSH segment.

Comments (0)

Files changed (2)

  * http://www.hl7.org/
 """
 
-__version__ = '0.0.3'
+__version__ = '0.1.0a'
 __author__ = 'John Paulett'
 __email__ = 'john -at- 7oars.com'
 __license__ = 'BSD'
     >>> parse('a|b^4|c\re|f|')
     [[['a'], ['b', '4'], ['c']], [['e'], ['f'], ['']]]
     """
+    ## Strip out unnecessary whitespace
+    strmsg = line.strip()
     ## The method for parsing the message
-    plan = ParsePlan()
+    plan = create_parse_plan(strmsg)
     ## Start spliting the methods based upon the ParsePlan
-    return _split(line.strip(), plan)
+    return _split(strmsg, plan)
 
 def _split(text, plan):
     """Recursive function to split the *text* into an n-deep list, where 
     by pipes and separated by carets. It contains a list of strings.
     """
    
-class ParsePlan(object):
+def create_parse_plan(strmsg):
+    """Creates a plan on how to parse the HL7 message according to
+    the details stored within the message.
+    """
+    ## We will always use a carriage return to separate segments
+    separators = ['\r']
+    ## Parse out the other separators from the characters following
+    ## MSH.  Currently we only go two-levels deep and ignore some
+    ## details.
+    separators.extend(list(strmsg[3:5]))
+    ## The ordered list of containers to create
+    containers = [Message, Segment, Field]
+    return _ParsePlan(separators, containers)
+    
+class _ParsePlan(object):
+    """Details on how to parse an HL7 message. Typically this object
+    should be created via :func:`hl7.create_parse_plan`
+    """
     # field, component, repetition, escape, subcomponent
     # TODO implement component, repetition, escape, and subcomponent
 
-    def __init__(self, separators=('\r', '|', '^'), containers=(Message, Segment, Field)):
+    def __init__(self, separators, containers):
         # TODO test to see performance implications of the assertion
         # since we generate the ParsePlan, this should never be in
         # invalid state
 # you should have received as part of this distribution.
 
 import hl7
+from hl7 import *
 
 ## Sample message from HL7 Normative Edition
 ## http://healthinfo.med.dal.ca/hl7intro/CDA_R2_normativewebedition/help/v3guide/v3guide.htm#v3gexamples
     assert isinstance(h[3], hl7.Segment)
     assert isinstance(h[3][0], hl7.Field)
     assert isinstance(h[3][0][0], str)
-   
 
+def test_create_parse_plan():
+    plan = hl7.create_parse_plan(sample_hl7)
+
+    assert plan.separators == ['\r', '|', '^']
+    assert plan.containers == [Message, Segment, Field]
+
+def test_parse_plan():
+    plan = hl7.create_parse_plan(sample_hl7)
+
+    assert plan.separator == '\r'
+    con = plan.container([1, 2])
+    assert isinstance(con, Message)
+    assert con == [1, 2]
+    assert con.separator == '\r'
+
+def test_parse_plan_next():
+    plan = hl7.create_parse_plan(sample_hl7)
+
+    n1 = plan.next()
+    assert n1.separators == ['|', '^']
+    assert n1.containers == [Segment, Field]
+    
+    n2 = n1.next()
+    assert n2.separators == ['^']
+    assert n2.containers == [Field]
+
+    n3 = n2.next()
+    assert n3 is None
+    
+def test_nonstandard_separators():
+    nonstd = 'MSH$%~\&$GHH LAB\rPID$$$555-44-4444$$EVERYWOMAN%EVE%E%%%L'
+    msg = hl7.parse(nonstd)
+
+    assert str(msg) == nonstd
+    assert len(msg) == 2
+    assert msg[1][5] == ['EVERYWOMAN', 'EVE', 'E', '', '', 'L']
+        
 if __name__ == '__main__':
     import doctest
     import nose