Package pyx12 :: Module x12xml_idtag
[hide private]

Source Code for Module pyx12.x12xml_idtag

  1  ###################################################################### 
  2  # Copyright (c) 2001-2008 Kalamazoo Community Mental Health Services, 
  3  #   John Holland <jholland@kazoocmh.org> <john@zoner.org> 
  4  # All rights reserved. 
  5  # 
  6  # This software is licensed as described in the file LICENSE.txt, which 
  7  # you should have received as part of this distribution. 
  8  # 
  9  ###################################################################### 
 10   
 11  #    $Id: x12xml_idtag.py 1287 2008-06-12 03:56:06Z johnholland $ 
 12   
 13  """ 
 14  Create a XML rendering of the X12 document 
 15  Uses node IDs as the tag names 
 16  """ 
 17   
 18  import os.path 
 19  import logging 
 20   
 21  # Intrapackage imports 
 22  from errors import * 
 23  from x12xml import x12xml 
 24  from xmlwriter import XMLWriter 
 25  from map_walker import pop_to_parent_loop 
 26   
 27  logger = logging.getLogger('pyx12.x12xml.simple') 
 28   
29 -class x12xml_idtag(x12xml):
30 - def __init__(self, fd, dtd_urn=None):
31 x12xml.__init__(self, fd, u"x12idtag", dtd_urn) 32 self.last_path = []
33
34 - def __del__(self):
35 while len(self.writer) > 0: 36 self.writer.pop()
37
38 - def seg(self, seg_node, seg_data):
39 """ 40 Generate XML for the segment data and matching map node 41 42 @param seg_node: Map Node 43 @type seg_node: L{node<map_if.x12_node>} 44 @param seg_data: Segment object 45 @type seg_data: L{segment<segment.Segment>} 46 """ 47 if not seg_node.is_segment(): 48 raise EngineError, 'Node must be a segment' 49 parent = pop_to_parent_loop(seg_node) # Get enclosing loop 50 # check path for new loops to be added 51 cur_path = self._path_list(parent.get_path()) 52 if self.last_path != cur_path: 53 last_path = self.last_path 54 match_idx = self._get_path_match_idx(last_path, cur_path) 55 root_path = self._path_list(os.path.commonprefix( 56 ['/'.join(cur_path), '/'.join(last_path)])) 57 if seg_node.is_first_seg_in_loop() and root_path==cur_path: 58 match_idx -= 1 59 for i in range(len(last_path)-1, match_idx-1, -1): 60 self.writer.pop() 61 for i in range(match_idx, len(cur_path)): 62 (xname, attrib) = self._get_loop_info(cur_path[i]) 63 self.writer.push(xname, attrib) 64 seg_node_id = self._get_node_id(seg_node, parent, seg_data) 65 (xname, attrib) = self._get_seg_info(seg_node_id) 66 self.writer.push(xname, attrib) 67 for i in range(len(seg_data)): 68 child_node = seg_node.get_child_node_by_idx(i) 69 if child_node.usage == 'N' or seg_data.get('%02i' % (i+1)).is_empty(): 70 pass # Do not try to ouput for invalid or empty elements 71 elif child_node.is_composite(): 72 (xname, attrib) = self._get_comp_info(seg_node_id) 73 self.writer.push(xname, attrib) 74 comp_data = seg_data.get('%02i' % (i+1)) 75 for j in range(len(comp_data)): 76 subele_node = child_node.get_child_node_by_idx(j) 77 (xname, attrib) = self._get_subele_info(subele_node.id) 78 self.writer.elem(xname, comp_data[j].get_value(), attrib) 79 self.writer.pop() #end composite 80 elif child_node.is_element(): 81 if seg_data.get_value('%02i' % (i+1)) == '': 82 pass 83 #self.writer.empty(child_node.id) 84 else: 85 (xname, attrib) = self._get_ele_info(child_node.id) 86 self.writer.elem(xname, seg_data.get_value('%02i' % (i+1)), attrib) 87 else: 88 raise EngineError, 'Node must be a either an element or a composite' 89 self.writer.pop() #end segment 90 self.last_path = cur_path
91
92 - def _get_loop_info(self, loop_id):
93 """ 94 Override loop node value 95 """ 96 loop_name = 'L' + loop_id 97 attrib = {} 98 return (loop_name, attrib)
99
100 - def _get_seg_info(self, seg_id):
101 """ 102 Override segment node value 103 """ 104 seg_name = seg_id 105 attrib = {} 106 return (seg_name, attrib)
107
108 - def _get_comp_info(self, comp_id):
109 """ 110 Override composite node value 111 """ 112 comp_name = comp_id 113 attrib = {} 114 return (comp_name, attrib)
115
116 - def _get_ele_info(self, ele_id):
117 """ 118 Override element node value 119 """ 120 name = ele_id 121 attrib = {} 122 return (name, attrib)
123
124 - def _get_subele_info(self, subele_id):
125 """ 126 Override sub-element node value 127 """ 128 name = subele_id 129 attrib = {} 130 return (name, attrib)
131