266: #line 283 "python_tangler.ipk"
267:
268: class python_tangler(tangler_base):
269: def __init__(self,sink,weaver):
270: tangler_base.__init__(self,sink,weaver)
271: self.matchPOD = re.compile('^ *#@(.*)$')
272: self.matchcomment = re.compile('^([^#]*)#.*$')
273: self.excludeid = []
274: self.userdict = {}
275: self.tokeniser = python_tokeniser(report_comments = 1, split_multiline_strings=1)
276: self.language = 'python'
277:
278: def __del__(self):
279: try:
280: tokens = self.tokeniser.close()
281: except:
282: print 'Tokeniser error'
283: print 'closing tokeniser for',self.sink.name
284: tangler_base.__del__(self)
285:
286: def writeline(self,data,file,count,inhibit_sref=0):
287: match = self.matchPOD.match(data)
288: if match:
289: command = match.group(1)
290: py_exec(command,file,count,globals(),self.userdict)
291: else:
292: self.weaver.set_fc_anchor(file,count)
293:
294: if self.sink.lines_written == 0 and len(data)>2:
295: inhibit_sref = data[:2]=='#!'
296: self._handle_sref(file,count, inhibit_sref)
297: self._writeline(data)
298:
299: try:
300: tokens = self.tokeniser.tokenize(data+'\n')
301: except TokenError, e:
302: print 'Tokeniser error',e
303: print 'in file',file,'line',line
304: print 'data['+data+']'
305:
306:
307:
308: chars_written = 0
309: self.weaver.start_code_line(self.sink.lines_written)
310: if tokens:
311: for kind,id,lstart,lend,dummy in tokens:
312: first = lstart[1]
313: last = lend[1]
314: self.weaver.write_code_fragment(data[chars_written:first])
315: markup = None
316: if kind == token.NAME:
317: if keyword.iskeyword(id): markup = 'KEYWORD'
318: elif kind == COMMENT: markup = 'COMMENT'
319: elif kind in [token.STRING,
320: MULTILINE_STRING_FIRST,
321: MULTILINE_STRING_MIDDLE,
322: MULTILINE_STRING_LAST]: markup = 'STRING'
323: elif kind == token.NUMBER: markup = 'NUMBER'
324: elif kind in py_bracket_tokens : markup = 'BRACKET'
325: elif kind in py_punct_tokens : markup = 'PUNCT'
326: elif kind in py_op_tokens: markup = 'OP'
327: self.weaver.write_code_fragment(data[first:last], markup)
328: chars_written = last
329: self.weaver.write_code_fragment(data[chars_written:])
330: self.weaver.end_code_line()
331:
332: dst_count = self.sink.lines_written
333: dst_file = self.sink.name
334: class_name = 0
335: function_name = 0
336: level = 0
337: for kind,id,lstart,lend,dummy in tokens:
338: if kind == token.INDENT:
339: level = level + 1
340: elif kind == token.DEDENT:
341: level = level - 1
342: if kind is token.NAME:
343: if not (keyword.iskeyword(id) or id in self.excludeid):
344: if not self.pass_frame.ids.has_key(id): self.pass_frame.ids[id]=[]
345: self.pass_frame.ids[id].append((file,count,dst_file,dst_count))
346: if class_name:
347:
348: if not self.pass_frame.classes.has_key(id): self.pass_frame.classes[id]=[]
349: self.pass_frame.classes[id].append((file,count,dst_file,dst_count))
350: class_name = 0
351: elif function_name:
352: if not self.pass_frame.functions.has_key(id): self.pass_frame.functions[id]=[]
353: self.pass_frame.functions[id].append((file,count,dst_file,dst_count))
354: function_name = 0
355: elif id == 'class':
356: class_name = 1
357: elif id == 'def':
358: function_name = 1
359:
360: def write_comment(self,line,file,count):
361: self.writeline('# '+line,file,count)
362:
363: def start_section(self, file, count):
364: data = '#line '+str(count)+' '+'"'+file+'"'
365: self._writeline(data)
366: if self.weaver:
367: self.weaver.echotangle(self.sink.lines_written,data)
368:
369: def get_comment_tangler(self):
370: return script_comment_tangler(self.sink)
371:
372: def get_string_tangler(self,eol,width):
373: return c_string_tangler(self.sink,self.get_weaver(),eol,width)
374:
375: def function(self,
376: name,
377: indent,
378: source_file,
379: source_line,
380: description=None,
381: arguments=None,
382: precondition=None,
383: result=None,
384: postcondition=None,
385: initial=None,
386: final=None,
387: body=None):
388:
389: tangle_function(
390: self.sink,
391: source_file,
392: source_line,
393: indent,
394: name,
395: description=description,
396: arguments=arguments,
397: precondition=precondition,
398: result=result,
399: postcondition=postcondition,
400: initial=initial,
401: final=final,
402: body=body)
403:
404: weave_function(
405: self.weaver,
406: indent,
407: name,
408: description=description,
409: arguments=arguments,
410: precondition=precondition,
411: result=result,
412: postcondition=postcondition,
413: initial=initial,
414: final=final,
415: body=body)
416: