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
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
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
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
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