Example #1
0
 def test_mult_matching_subloops_ok(self):
     """
     Test for match of 820 Individual Remittance Loop
     """
     cmap = pyx12.map_if.load_map_file('820.5010.X218.xml', self.param)
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B/N1')
     self.assertNotEqual(node, None)
     start_node = node
     self.assertEqual(node.base_name, 'segment')
     node.cur_count = 1
     seg_data = pyx12.segment.Segment(
         'ENT*1*2J*EI*99998707~', '~', '*', ':')
     self.errh.reset()
     #print node.get_path()
     (node, pop, push) = self.walker.walk(
         node, seg_data, self.errh, 5, 4, None)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     #print node.get_path()
     self.assertEqual(get_id_list(pop), ['1000B', 'HEADER'])
     self.assertEqual(get_id_list(push), ['TABLE2AREA3', '2000B'])
     self.assertEqual(traverse_path(start_node, pop, push),
                      pop_to_parent_loop(node).get_path())
Example #2
0
    def seg(self, seg_node, seg_data):
        """
        Generate XML for the segment data and matching map node

        @param seg_node: Map Node
        @type seg_node: L{node<map_if.x12_node>}
        @param seg_data: Segment object
        @type seg_data: L{segment<segment.Segment>}
        """
        if not seg_node.is_segment():
            raise EngineError('Node must be a segment')
        parent = pop_to_parent_loop(seg_node)  # Get enclosing loop
        # check path for new loops to be added
        cur_path = self._path_list(parent.get_path())
        #if seg_node.id == 'GS':
        #    import ipdb; ipdb.set_trace()
        if self.last_path == cur_path and seg_node.is_first_seg_in_loop():
            # loop repeat
            self.writer.pop()
            (xname, attrib) = self._get_loop_info(cur_path[-1])
            self.writer.push(xname, attrib)
        else:
            last_path = self.last_path
            match_idx = self._get_path_match_idx(last_path, cur_path)
            root_path = self._path_list(commonprefix(['/'.join(cur_path), '/'.join(last_path)]))
            if seg_node.is_first_seg_in_loop() and root_path == cur_path:
                match_idx -= 1
            for i in range(len(last_path) - 1, match_idx - 1, -1):
                self.writer.pop()
            for i in range(match_idx, len(cur_path)):
                (xname, attrib) = self._get_loop_info(cur_path[i])
                self.writer.push(xname, attrib)
        seg_node_id = self._get_node_id(seg_node, parent, seg_data)
        (xname, attrib) = self._get_seg_info(seg_node_id)
        self.writer.push(xname, attrib)
        for i in range(len(seg_data)):
            child_node = seg_node.get_child_node_by_idx(i)
            if child_node.usage == 'N' or seg_data.get('%02i' % (i + 1)).is_empty():
                pass  # Do not try to ouput for invalid or empty elements
            elif child_node.is_composite():
                (xname, attrib) = self._get_comp_info(seg_node_id)
                self.writer.push(xname, attrib)
                comp_data = seg_data.get('%02i' % (i + 1))
                for j in range(len(comp_data)):
                    subele_node = child_node.get_child_node_by_idx(j)
                    (xname, attrib) = self._get_subele_info(subele_node.id)
                    self.writer.elem(xname, comp_data[j].get_value(), attrib)
                self.writer.pop()  # end composite
            elif child_node.is_element():
                if seg_data.get_value('%02i' % (i + 1)) == '':
                    pass
                    #self.writer.empty(u"ele", attrs={u'id': child_node.id})
                else:
                    (xname, attrib) = self._get_ele_info(child_node.id)
                    self.writer.elem(xname, seg_data.get_value('%02i' % (i + 1)), attrib)
            else:
                raise EngineError('Node must be a either an element or a composite')
        self.writer.pop()  # end segment
        self.last_path = cur_path
Example #3
0
    def seg(self, seg_node, seg_data):
        """
        Generate XML for the segment data and matching map node

        @param seg_node: Map Node
        @type seg_node: L{node<map_if.x12_node>}
        @param seg_data: Segment object
        @type seg_data: L{segment<segment.Segment>}
        """
        if not seg_node.is_segment():
            raise EngineError('Node must be a segment')
        parent = pop_to_parent_loop(seg_node)  # Get enclosing loop
        # check path for new loops to be added
        cur_path = self._path_list(parent.get_path())
        if self.last_path != cur_path:
            last_path = self.last_path
            match_idx = self._get_path_match_idx(last_path, cur_path)
            root_path = self._path_list(
                os.path.commonprefix(['/'.join(cur_path),
                                      '/'.join(last_path)]))
            if seg_node.is_first_seg_in_loop() and root_path == cur_path:
                match_idx -= 1
            for i in range(len(last_path) - 1, match_idx - 1, -1):
                self.writer.pop()
            for i in range(match_idx, len(cur_path)):
                (xname, attrib) = self._get_loop_info(cur_path[i])
                self.writer.push(xname, attrib)
        seg_node_id = self._get_node_id(seg_node, parent, seg_data)
        (xname, attrib) = self._get_seg_info(seg_node_id)
        self.writer.push(xname, attrib)
        for i in range(len(seg_data)):
            child_node = seg_node.get_child_node_by_idx(i)
            if child_node.usage == 'N' or seg_data.get('%02i' %
                                                       (i + 1)).is_empty():
                pass  # Do not try to ouput for invalid or empty elements
            elif child_node.is_composite():
                (xname, attrib) = self._get_comp_info(seg_node_id)
                self.writer.push(xname, attrib)
                comp_data = seg_data.get('%02i' % (i + 1))
                for j in range(len(comp_data)):
                    subele_node = child_node.get_child_node_by_idx(j)
                    (xname, attrib) = self._get_subele_info(subele_node.id)
                    self.writer.elem(xname, comp_data[j].get_value(), attrib)
                self.writer.pop()  # end composite
            elif child_node.is_element():
                if seg_data.get_value('%02i' % (i + 1)) == '':
                    pass
                    #self.writer.empty(u"ele", attrs={u'id': child_node.id})
                else:
                    (xname, attrib) = self._get_ele_info(child_node.id)
                    self.writer.elem(xname,
                                     seg_data.get_value('%02i' % (i + 1)),
                                     attrib)
            else:
                raise EngineError(
                    'Node must be a either an element or a composite')
        self.writer.pop()  # end segment
        self.last_path = cur_path
Example #4
0
    def _add_segment(self, cur_data_node, segment_x12_node, seg_data, pop_loops, push_loops):
        """
        From the last position in the X12 Data Node Tree, find the correct
        position for the new segment; moving up or down the tree as appropriate.

        G{callgraph}

        @param cur_data_node: Current X12 Data Node
        @type cur_data_node: L{node<x12context.X12DataNode>}
        @param segment_x12_node: Segment Map Node
        @type segment_x12_node: L{node<map_if.x12_node>}
        @return: New X12 Data Node
        @rtype: L{node<x12context.X12DataNode>}
        """
        if not segment_x12_node.is_segment():
            raise errors.EngineError('Node must be a segment')
        # Get enclosing loop
        orig_data_node = cur_data_node
        parent_x12_node = pop_to_parent_loop(segment_x12_node)
        cur_loop_node = cur_data_node
        if cur_loop_node.type == 'seg':
            cur_loop_node = cur_loop_node.parent
        # check path for new loops to be added
        new_path = parent_x12_node.x12path
        last_path = cur_loop_node.x12_map_node.x12path
        if last_path != new_path:
            for x12_loop in pop_loops:
                if cur_loop_node.id != x12_loop.id:
                    raise errors.EngineError('Loop pop: %s != %s' %
                                             (cur_loop_node.id, x12_loop.id))
                cur_loop_node = cur_loop_node.parent
            for x12_loop in push_loops:
                if cur_loop_node is None:
                    raise errors.EngineError('cur_loop_node is None. x12_loop: %s' % (x12_loop.id))
                # push new loop nodes, if needed
                cur_loop_node = cur_loop_node._add_loop_node(x12_loop)
        else:
            # handle loop repeat
            if cur_loop_node.parent is not None and segment_x12_node.is_first_seg_in_loop():
                cur_loop_node = cur_loop_node.parent._add_loop_node(
                    segment_x12_node.parent)
        try:
            new_node = X12SegmentDataNode(self.x12_map_node, seg_data)
        except Exception:
            mypath = self.x12_map_node.get_path()
            err_str = 'X12SegmentDataNode failed: x12_path={}, seg_date={}'.format(mypath, seg_data)
            raise errors.EngineError(err_str)
        try:
            new_node.parent = cur_loop_node
            cur_loop_node.children.append(new_node)
        except Exception:
            err_str = 'X12SegmentDataNode child append failed:'
            err_str += ' seg_x12_path=%s' % (segment_x12_node.get_path())
            err_str += ', orig_datanode=%s' % (orig_data_node.cur_path)
            err_str += ', cur_datanode=%s' % (cur_data_node.cur_path)
            err_str += ', seg_data=%s' % (seg_data)
            raise errors.EngineError(err_str)
        return new_node
Example #5
0
 def xtest_repeat_loop_with_one_segment(self):
     cmap = pyx12.map_if.load_map_file('841.4010.XXXC.xml', self.param)
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000/2100/SPI')
     self.assertNotEqual(node, None, 'Node not found')
     start_node = node
     #node.cur_count = 1
     self.walker.setCountState({node.x12path: 1})
     self.errh.reset()
     seg_data = pyx12.segment.Segment('SPI*00', '~', '*', ':')
     (node, pop, push) = self.walker.walk(node, seg_data, self.errh, 5, 4, None)
     self.assertNotEqual(node, None, 'walker failed')
     self.assertEqual(seg_data.get_seg_id(), node.id)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     self.assertEqual(get_id_list(pop), ['2100'])
     self.assertEqual(get_id_list(push), ['2100'])
     self.assertEqual(traverse_path(start_node, pop, push), pop_to_parent_loop(node).get_path())
Example #6
0
    def seg_context(self, seg_node, seg_data, pop_loops, push_loops):
        """
        Generate XML for the segment data and matching map node

        @param seg_node: Map Node
        @type seg_node: L{node<map_if.x12_node>}
        @param seg_data: Segment object
        @type seg_data: L{segment<segment.Segment>}
        """
        assert seg_node.is_segment(), 'Node must be a segment'
        parent = pop_to_parent_loop(seg_node)  # Get enclosing loop
        for loop in pop_loops:
            self.writer.pop()
        for loop in push_loops:
            (xname, attrib) = self._get_loop_info(loop.id)
            self.writer.push(xname, attrib)
        (xname, attrib) = self._get_seg_info(seg_node.id)
        self.writer.push(xname, attrib)
        for i in range(len(seg_data)):
            child_node = seg_node.get_child_node_by_idx(i)
            if child_node.usage == 'N' or seg_data.get('%02i' %
                                                       (i + 1)).is_empty():
                pass  # Do not try to ouput for invalid or empty elements
            elif child_node.is_composite():
                (xname, attrib) = self._get_comp_info(seg_node.id)
                self.writer.push(xname, attrib)
                comp_data = seg_data.get('%02i' % (i + 1))
                for j in range(len(comp_data)):
                    subele_node = child_node.get_child_node_by_idx(j)
                    (xname, attrib) = self._get_subele_info(subele_node.id)
                    self.writer.elem(xname, comp_data[j].get_value(), attrib)
                self.writer.pop()  # end composite
            elif child_node.is_element():
                if seg_data.get_value('%02i' % (i + 1)) == '':
                    pass
                    #self.writer.empty(u"ele", attrs={u'id': child_node.id})
                else:
                    (xname, attrib) = self._get_ele_info(child_node.id)
                    self.writer.elem(xname,
                                     seg_data.get_value('%02i' % (i + 1)),
                                     attrib)
            else:
                raise EngineError(
                    'Node must be a either an element or a composite')
        self.writer.pop()  # end segment
Example #7
0
 def test_repeat_loop_with_one_segment_EQ(self):
     #errh = pyx12.error_handler.errh_null()
     cmap = pyx12.map_if.load_map_file('270.4010.X092.A1.xml', self.param)
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2000C/2100C/2110C/EQ')
     start_node = node
     self.assertNotEqual(node, None, 'Node not found')
     node.cur_count = 1
     seg_data = pyx12.segment.Segment('EQ*30**CHD', '~', '*', ':')
     self.errh.reset()
     (node, pop, push) = self.walker.walk(
         node, seg_data, self.errh, 5, 4, None)
     self.assertNotEqual(node, None, 'walker failed')
     self.assertEqual(seg_data.get_seg_id(), node.id)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     self.assertEqual(get_id_list(pop), ['2110C'])
     self.assertEqual(get_id_list(push), ['2110C'])
     self.assertEqual(traverse_path(start_node, pop, push),
                      pop_to_parent_loop(node).get_path())
Example #8
0
 def test_mult_matching_subloops_ok(self):
     """
     Test for match of 820 Individual Remittance Loop
     """
     cmap = pyx12.map_if.load_map_file('820.5010.X218.xml', self.param)
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     #node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     #node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B')
     self.assertNotEqual(node, None)
     self.assertEqual(node.base_name, 'loop')
     #node.cur_count = 1
     node = cmap.getnodebypath('/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B/N1')
     self.assertNotEqual(node, None)
     start_node = node
     self.assertEqual(node.base_name, 'segment')
     #node.cur_count = 1
     seg_data = pyx12.segment.Segment('ENT*1*2J*EI*99998707~', '~', '*',
                                      ':')
     self.errh.reset()
     #print node.get_path()
     self.walker.setCountState({
         '/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER':
         1,
         '/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A':
         1,
         '/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B':
         1,
         '/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B/N1':
         1,
     })
     (node, pop, push) = self.walker.walk(node, seg_data, self.errh, 5, 4,
                                          None)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     #print node.get_path()
     self.assertEqual(get_id_list(pop), ['1000B', 'HEADER'])
     self.assertEqual(get_id_list(push), ['TABLE2AREA3', '2000B'])
     self.assertEqual(traverse_path(start_node, pop, push),
                      pop_to_parent_loop(node).get_path())
Example #9
0
 def xtest_repeat_loop_with_one_segment(self):
     cmap = pyx12.map_if.load_map_file('841.4010.XXXC.xml', self.param)
     node = cmap.getnodebypath(
         '/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000/2100/SPI')
     self.assertNotEqual(node, None, 'Node not found')
     start_node = node
     #node.cur_count = 1
     self.walker.setCountState({node.x12path: 1})
     self.errh.reset()
     seg_data = pyx12.segment.Segment('SPI*00', '~', '*', ':')
     (node, pop, push) = self.walker.walk(node, seg_data, self.errh, 5, 4,
                                          None)
     self.assertNotEqual(node, None, 'walker failed')
     self.assertEqual(seg_data.get_seg_id(), node.id)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     self.assertEqual(get_id_list(pop), ['2100'])
     self.assertEqual(get_id_list(push), ['2100'])
     self.assertEqual(traverse_path(start_node, pop, push),
                      pop_to_parent_loop(node).get_path())
Example #10
0
    def seg_context(self, seg_node, seg_data, pop_loops, push_loops):
        """
        Generate XML for the segment data and matching map node

        @param seg_node: Map Node
        @type seg_node: L{node<map_if.x12_node>}
        @param seg_data: Segment object
        @type seg_data: L{segment<segment.Segment>}
        """
        assert seg_node.is_segment(), 'Node must be a segment'
        parent = pop_to_parent_loop(seg_node)  # Get enclosing loop
        for loop in pop_loops:
            self.writer.pop()
        for loop in push_loops:
            (xname, attrib) = self._get_loop_info(loop.id)
            self.writer.push(xname, attrib)
        (xname, attrib) = self._get_seg_info(seg_node.id)
        self.writer.push(xname, attrib)
        for i in range(len(seg_data)):
            child_node = seg_node.get_child_node_by_idx(i)
            if child_node.usage == 'N' or seg_data.get('%02i' % (i + 1)).is_empty():
                pass  # Do not try to ouput for invalid or empty elements
            elif child_node.is_composite():
                (xname, attrib) = self._get_comp_info(seg_node.id)
                self.writer.push(xname, attrib)
                comp_data = seg_data.get('%02i' % (i + 1))
                for j in range(len(comp_data)):
                    subele_node = child_node.get_child_node_by_idx(j)
                    (xname, attrib) = self._get_subele_info(subele_node.id)
                    self.writer.elem(xname, comp_data[j].get_value(), attrib)
                self.writer.pop()  # end composite
            elif child_node.is_element():
                if seg_data.get_value('%02i' % (i + 1)) == '':
                    pass
                    #self.writer.empty(u"ele", attrs={u'id': child_node.id})
                else:
                    (xname, attrib) = self._get_ele_info(child_node.id)
                    self.writer.elem(xname, seg_data.get_value(
                        '%02i' % (i + 1)), attrib)
            else:
                raise EngineError('Node must be a either an element or a composite')
        self.writer.pop()  # end segment
Example #11
0
 def test_repeat_loop_with_one_segment_EQ(self):
     #errh = pyx12.error_handler.errh_null()
     cmap = pyx12.map_if.load_map_file('270.4010.X092.A1.xml', self.param)
     node = cmap.getnodebypath(
         '/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2000C/2100C/2110C/EQ'
     )
     start_node = node
     self.assertNotEqual(node, None, 'Node not found')
     #node.cur_count = 1
     self.walker.setCountState({node.x12path: 1})
     seg_data = pyx12.segment.Segment('EQ*30**CHD', '~', '*', ':')
     self.errh.reset()
     (node, pop, push) = self.walker.walk(node, seg_data, self.errh, 5, 4,
                                          None)
     self.assertNotEqual(node, None, 'walker failed')
     self.assertEqual(seg_data.get_seg_id(), node.id)
     self.assertEqual(self.errh.err_cde, None, self.errh.err_str)
     self.assertEqual(get_id_list(pop), ['2110C'])
     self.assertEqual(get_id_list(push), ['2110C'])
     self.assertEqual(traverse_path(start_node, pop, push),
                      pop_to_parent_loop(node).get_path())
Example #12
0
    def _add_segment(self, cur_data_node, segment_x12_node, seg_data,
                     pop_loops, push_loops):
        """
        From the last position in the X12 Data Node Tree, find the correct
        position for the new segment; moving up or down the tree as appropriate.

        G{callgraph}

        @param cur_data_node: Current X12 Data Node
        @type cur_data_node: L{node<x12context.X12DataNode>}
        @param segment_x12_node: Segment Map Node
        @type segment_x12_node: L{node<map_if.x12_node>}
        @return: New X12 Data Node
        @rtype: L{node<x12context.X12DataNode>}
        """
        if not segment_x12_node.is_segment():
            raise errors.EngineError('Node must be a segment')
        # Get enclosing loop
        orig_data_node = cur_data_node
        parent_x12_node = pop_to_parent_loop(segment_x12_node)
        cur_loop_node = cur_data_node
        if cur_loop_node.type == 'seg':
            cur_loop_node = cur_loop_node.parent
        # check path for new loops to be added
        new_path = parent_x12_node.x12path
        last_path = cur_loop_node.x12_map_node.x12path
        if last_path != new_path:
            for x12_loop in pop_loops:
                if cur_loop_node.id != x12_loop.id:
                    raise errors.EngineError('Loop pop: %s != %s' %
                                             (cur_loop_node.id, x12_loop.id))
                cur_loop_node = cur_loop_node.parent
            for x12_loop in push_loops:
                if cur_loop_node is None:
                    raise errors.EngineError(
                        'cur_loop_node is None. x12_loop: %s' % (x12_loop.id))
                # push new loop nodes, if needed
                cur_loop_node = cur_loop_node._add_loop_node(x12_loop)
        else:
            # handle loop repeat
            if cur_loop_node.parent is not None and segment_x12_node.is_first_seg_in_loop(
            ):
                cur_loop_node = cur_loop_node.parent._add_loop_node(
                    segment_x12_node.parent)
        try:
            new_node = X12SegmentDataNode(self.x12_map_node, seg_data)
        except Exception:
            mypath = self.x12_map_node.get_path()
            err_str = 'X12SegmentDataNode failed: x12_path={}, seg_date={}'.format(
                mypath, seg_data)
            raise errors.EngineError(err_str)
        try:
            new_node.parent = cur_loop_node
            cur_loop_node.children.append(new_node)
        except Exception:
            err_str = 'X12SegmentDataNode child append failed:'
            err_str += ' seg_x12_path=%s' % (segment_x12_node.get_path())
            err_str += ', orig_datanode=%s' % (orig_data_node.cur_path)
            err_str += ', cur_datanode=%s' % (cur_data_node.cur_path)
            err_str += ', seg_data=%s' % (seg_data)
            raise errors.EngineError(err_str)
        return new_node