Package pyx12 :: Module error_html
[hide private]

Source Code for Module pyx12.error_html

  1  ###################################################################### 
  2  # Copyright (c) 2001-2005 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: error_html.py 961 2007-03-23 21:35:20Z johnholland $ 
 12   
 13  """ 
 14  Generates HTML error output 
 15  """ 
 16   
 17  import string 
 18  from types import * 
 19  import time 
 20  import logging 
 21  #import pdb 
 22   
 23  # Intrapackage imports 
 24  from errors import * 
 25  #from utils import seg_str, escape_html_chars 
 26   
 27  logger = logging.getLogger('pyx12.error_html') 
 28  logger.setLevel(logging.DEBUG) 
 29  #logger.setLevel(logging.ERROR) 
 30   
31 -class error_html(object):
32 """ 33 """
34 - def __init__(self, errh, fd, term=('~', '*', '~', '\n')):
35 """ 36 @param fd: target file 37 @type fd: file descriptor 38 @param term: tuple of x12 terminators used 39 @type term: tuple(string, string, string, string) 40 41 @bug: GS errors are re-printing at the GE level 42 """ 43 self.errh = errh 44 self.fd = fd 45 self.seg_term = term[0] 46 self.ele_term = term[1] 47 self.subele_term = term[2] 48 self.eol = '' 49 self.last_line = 0 50 self.loop_info = None
51
52 - def header(self):
53 self.fd.write('<html>\n<head>\n') 54 self.fd.write('<title>X12N Error Analysis</title>\n') 55 self.fd.write('<style type="text/css">\n<!--\n') 56 self.fd.write(' span.seg { color: black; font-style: normal; }\n') 57 self.fd.write(' span.error { background-color: #CCCCFF; color: red; font-style: normal; }\n') 58 self.fd.write(' span.info { color: blue; font-style: normal; }\n') 59 self.fd.write(' span.ele_err { background-color: yellow; color: red; font-style: normal; }\n') 60 self.fd.write(' -->\n</style>\n') 61 self.fd.write(' <link rel="stylesheet" href="errors.css" type="text/css">\n') 62 self.fd.write('</head>\n<body>\n') 63 self.fd.write('<h1>X12N Error Analysis</h1>\n<h3>Analysis Date: %s</h3><p>\n' % \ 64 (time.strftime('%m/%d/%Y %H:%M:%S'))) 65 self.fd.write('<div class="segs" style="">\n')
66
67 - def footer(self):
68 err_st = self.errh.cur_st_node 69 if not err_st.is_closed(): 70 for (err_cde, err_str) in err_st.errors: 71 if err_cde == '2': 72 self.fd.write('<span class="error">&nbsp;%s (Segment Error Code: %s)</span><br>\n' % \ 73 (err_str, err_cde)) 74 err_gs = self.errh.cur_gs_node 75 if not err_gs.is_closed(): 76 for (err_cde, err_str) in err_gs.errors: 77 if err_cde == '3': 78 self.fd.write('<span class="error">&nbsp;%s (Segment Error Code: %s)</span><br>\n' % \ 79 (err_str, err_cde)) 80 err_isa = self.errh.cur_isa_node 81 if not err_isa.is_closed(): 82 for (err_cde, err_str) in err_isa.errors: 83 if err_cde == '023': 84 self.fd.write('<span class="error">&nbsp;%s (Segment Error Code: %s)</span><br>\n' % \ 85 (err_str, err_cde)) 86 self.fd.write('</div>\n') 87 self.fd.write('<p>\n<a href="http://sourceforge.net/projects/pyx12/">pyx12 Validator</a>\n</p>\n') 88 self.fd.write('</body>\n</html>\n')
89
90 - def loop(self, loop_node):
91 if loop_node.type != 'wrapper': 92 #self.gen_info('Loop %s: %s' % (loop_node.id, loop_node.name)) 93 self.loop_info = 'Loop %s: %s' % (loop_node.id, loop_node.name)
94
95 - def gen_info(self, info_str):
96 """ 97 """ 98 self.fd.write('<span class="info">&nbsp;&nbsp;%s</span><br>\n' % (info_str))
99
100 - def gen_seg(self, seg_data, src, err_node_list):
101 """ 102 Find error seg for this segment. 103 Find any skipped error values. 104 ID pos of bad value. 105 @param seg_data: data segment instance 106 """ 107 cur_line = src.cur_line 108 109 #while errh 110 ele_pos_map = {} 111 for err_node in err_node_list: 112 for ele in err_node.elements: 113 ele_pos_map[ele.ele_pos] = ele.subele_pos 114 115 t_seg = [] #list of formatted elements 116 #seg_data.format_ele_list(t_seg) 117 for i in range(1, len(seg_data)+1): 118 if seg_data.is_composite(ref_des = '%02i' % (i)): 119 #if seg_data.get_seg_id()=='CLM': pdb.set_trace() 120 t_seg.append([]) 121 for j in range(1, seg_data.ele_len('%02i' % (i))+1): 122 ref_des = '%02i-%i' % (i, j) 123 ele_str = escape_html_chars(seg_data.get_value(ref_des)) 124 if i in ele_pos_map.keys() and ele_pos_map[i] == j: 125 ele_str = self._wrap_ele_error(ele_str) 126 t_seg[-1].append(ele_str) 127 else: 128 ref_des = '%02i' % (i) 129 ele_str = escape_html_chars(seg_data.get_value(ref_des)) 130 if i in ele_pos_map.keys(): 131 ele_str = self._wrap_ele_error(ele_str) 132 t_seg.append(ele_str) 133 134 for err_node in err_node_list: 135 #for err_tuple in err_node.errors: 136 for err_tuple in err_node.get_error_list(seg_data.get_seg_id(), True): 137 err_cde = err_tuple[0] 138 err_str = err_tuple[1] 139 if err_cde == '3': 140 self.fd.write('<span class="error">&nbsp;%s (Segment Error Code: %s)</span><br>\n' % \ 141 (err_str, err_cde)) 142 if self.loop_info: 143 self.gen_info(self.loop_info) 144 self.loop_info = None 145 self.fd.write('<span class="seg">%i:&nbsp;%s</span><br>\n' % \ 146 (cur_line, self._seg_str(seg_data.get_seg_id(), t_seg))) 147 for err_node in err_node_list: 148 for err_tuple in err_node.get_error_list(seg_data.get_seg_id(), False): 149 #for err_tuple in err_node.errors: 150 err_cde = err_tuple[0] 151 err_str = err_tuple[1] 152 if err_cde != '3': 153 self.fd.write('<span class="error">&nbsp;%s (Segment Error Code: %s)</span><br>\n' % \ 154 (err_str, err_cde)) 155 for ele in err_node.elements: 156 for (err_cde, err_str, err_val) in ele.get_error_list(seg_data.get_seg_id(), False): 157 #for (err_cde, err_str, err_val) in ele.errors: 158 if not (seg_data.get_seg_id()=='GE' and 'GS' in err_str): #Ugly hack 159 self.fd.write('<span class="error">&nbsp;%s (Element Error Code: %s)</span><br>\n' % \ 160 (err_str, err_cde))
161
162 - def _seg_str(self, seg_id, ele_list):
163 """ 164 @param ele_list: list of formatted elements 165 @rtype: string 166 """ 167 return seg_id + self.ele_term + seg_str(ele_list, self.seg_term, self.ele_term, \ 168 self.subele_term, self.eol)
169
170 - def _wrap_ele_error(self, str1):
171 """ 172 @rtype: string 173 """ 174 return '<span class="ele_err">%s</span>' % (str1)
175 176
177 -def seg_str(seg, seg_term, ele_term, subele_term, eol=''):
178 """ 179 Join a list of elements 180 @param seg: List of elements 181 @type seg: list[string|list[string]] 182 @param seg_term: Segment terminator character 183 @type seg_term: string 184 @param ele_term: Element terminator character 185 @type ele_term: string 186 @param subele_term: Sub-element terminator character 187 @type subele_term: string 188 @param eol: End of line character 189 @type eol: string 190 @return: formatted segment 191 @rtype: string 192 """ 193 #if None in seg: 194 # logger.debug(seg) 195 tmp = [] 196 for a in seg: 197 if type(a) is ListType: 198 tmp.append(string.join(a, subele_term)) 199 else: 200 tmp.append(a) 201 return '%s%s%s' % (string.join(tmp, ele_term), seg_term, eol)
202
203 -def escape_html_chars(str_val):
204 """ 205 Escape special HTML characters (& <>) 206 @type str_val: string 207 @return: formatted string 208 @rtype: string 209 """ 210 if str_val is None: 211 return None 212 output = str_val 213 output = output.replace('&', '&amp;') 214 output = output.replace(' ', '&nbsp;') 215 output = output.replace('>', '&gt;') 216 output = output.replace('<', '&lt;') 217 return output
218