1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Capture X12 Errors
15 """
16
17 import logging
18 from types import *
19
20 import tempfile
21
22 import os
23
24
25 from errors import *
26 from xmlwriter import XMLWriter
27
28
29
30
31 logger = logging.getLogger('pyx12.errh_xml')
32
33
34
36 """
37 The interface to the error handling structures.
38 """
39 - def __init__(self, xml_out=None, basedir=None):
40 """
41 @param xml_out: Output filename, if None, will dump to tempfile
42 @param basedir: working directory, where file will be created
43 """
44 if xml_out:
45 self.filename = xml_out
46 fd = open(xml_out, 'w')
47 else:
48 try:
49 (fdesc, self.filename) = tempfile.mkstemp('.xml', 'pyx12_')
50 fd = os.fdopen(fdesc, 'w+b')
51
52
53 except:
54
55 (fdesc, self.filename) = tempfile.mkstemp(suffix='.xml', prefix='pyx12_', dir=basedir)
56 fd = os.fdopen(fdesc, 'w+b')
57
58 self.cur_line = None
59 self.errors = []
60 if not fd:
61 raise EngineError, 'Could not open temp error xml file'
62 self.writer = XMLWriter(fd)
63
64
65
66 self.writer.push(u"x12err")
67
69 while len(self.writer) > 0:
70 self.writer.pop()
71
74
76 """
77 @param err_list: list of errors to apply
78 """
79 self.errors.extend(err_list)
80
81
82
83
84
85
86
87
88
89
91 """
92 @return: Current file line number
93 @rtype: int
94 """
95 return self.cur_line
96
97 - def Write(self, cur_line):
98 """
99 Generate XML for the segment data and matching map node
100
101 """
102 if len(self.errors) > 0:
103 self.writer.push(u"seg", attrs={u'line': '%i'%(cur_line)})
104 for (err_type, err_cde, err_str, err_val, src_line) in self.errors:
105 self.writer.push(u"err", attrs={u"code": err_cde})
106
107
108 self.writer.elem(u"desc", err_str)
109 if err_val:
110 self.writer.elem(u"errval", err_val)
111
112
113
114 self.writer.pop()
115 self.writer.pop()
116 self.errors = []
117
119 """Class for errh_null errors."""
120
122 """
123 A null error object - used for testing.
124 Stores the current error in simple variables.
125 """
127
128 self.errors = []
129
130 self.cur_line = 0
131
132
133
136
139
141 """
142 @return: Current file line number
143 @rtype: int
144 """
145 return self.cur_line
146
148 """
149 """
150 self.cur_line = cur_line
151
152
153
154
155
156
157
158
160 """
161 """
162
163 pass
164
169
174
175 - def add_seg(self, map_node, seg, seg_count, cur_line, ls_id):
179
184
186 """
187 @param err_cde: ISA level error code
188 @type err_cde: string
189 @param err_str: Description of the error
190 @type err_str: string
191 """
192 self.errors.append(('isa', err_cde, err_str, None, None))
193 sout = ''
194 sout += 'Line:%i ' % (self.cur_line)
195 sout += 'ISA:%s - %s' % (err_cde, err_str)
196 logger.error(sout)
197
199 """
200 @param err_cde: GS level error code
201 @type err_cde: string
202 @param err_str: Description of the error
203 @type err_str: string
204 """
205 self.errors.append(('gs', err_cde, err_str, None, None))
206 sout = ''
207 sout += 'Line:%i ' % (self.cur_line)
208 sout += 'GS:%s - %s' % (err_cde, err_str)
209 logger.error(sout)
210
212 """
213 @param err_cde: Segment level error code
214 @type err_cde: string
215 @param err_str: Description of the error
216 @type err_str: string
217 """
218 self.errors.append(('st', err_cde, err_str, None, None))
219 sout = ''
220 sout += 'Line:%i ' % (self.cur_line)
221 sout += 'ST:%s - %s' % (err_cde, err_str)
222 logger.error(sout)
223
224 - def seg_error(self, err_cde, err_str, err_value=None, src_line=None):
225 """
226 @param err_cde: Segment level error code
227 @type err_cde: string
228 @param err_str: Description of the error
229 @type err_str: string
230 """
231 self.errors.append(('seg', err_cde, err_str, err_value, src_line))
232 sout = ''
233 sout += 'Line:%i ' % (self.cur_line)
234 sout += 'SEG:%s - %s' % (err_cde, err_str)
235 if err_value:
236 sout += ' (%s)' % err_value
237 logger.error(sout)
238
239 - def ele_error(self, err_cde, err_str, bad_value):
240 """
241 @param err_cde: Element level error code
242 @type err_cde: string
243 @param err_str: Description of the error
244 @type err_str: string
245 """
246 self.errors.append(('ele', err_cde, err_str, bad_value, None))
247 sout = ''
248 sout += 'Line:%i ' % (self.cur_line)
249 sout += 'ELE:%s - %s' % (err_cde, err_str)
250 if bad_value:
251 sout += ' (%s)' % (bad_value)
252 logger.error(sout)
253
258
263
268
270 """
271 Find the last node of a type
272 """
273 pass
274
277
278
279
280
281
282
283
284
285
287 """
288 """
289 return None
290
292 """
293 """
294 return len(self.errors)
295
297 """
298 @rtype: boolean
299 """
300 return True
301