1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Generates HTML error output
15 """
16
17 import string
18 from types import *
19 import time
20 import logging
21
22
23
24 from errors import *
25
26
27 logger = logging.getLogger('pyx12.error_html')
28 logger.setLevel(logging.DEBUG)
29
30
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
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
89
90 - def loop(self, loop_node):
91 if loop_node.type != 'wrapper':
92
93 self.loop_info = 'Loop %s: %s' % (loop_node.id, loop_node.name)
94
96 """
97 """
98 self.fd.write('<span class="info"> %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
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 = []
116
117 for i in range(1, len(seg_data)+1):
118 if seg_data.is_composite(ref_des = '%02i' % (i)):
119
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
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"> %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: %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
150 err_cde = err_tuple[0]
151 err_str = err_tuple[1]
152 if err_cde != '3':
153 self.fd.write('<span class="error"> %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
158 if not (seg_data.get_seg_id()=='GE' and 'GS' in err_str):
159 self.fd.write('<span class="error"> %s (Element Error Code: %s)</span><br>\n' % \
160 (err_str, err_cde))
161
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
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
194
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
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('&', '&')
214 output = output.replace(' ', ' ')
215 output = output.replace('>', '>')
216 output = output.replace('<', '<')
217 return output
218