6.6.10. Python Tangler

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