home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / scripts / ptags.py < prev    next >
Text File  |  1992-10-02  |  1KB  |  51 lines

  1. #! /usr/local/bin/python
  2.  
  3. # ptags
  4. #
  5. # Create a tags file for Python programs, usable with vi.
  6. # Tagged are:
  7. # - functions (even inside other defs or classes)
  8. # - classes
  9. # - filenames
  10. # Warns about files it cannot open.
  11. # No warnings about duplicate tags.
  12.  
  13. import sys
  14. import regex
  15. import os
  16.  
  17. tags = []    # Modified global variable!
  18.  
  19. def main():
  20.     args = sys.argv[1:]
  21.     for file in args: treat_file(file)
  22.     if tags:
  23.         fp = open('tags', 'w')
  24.         tags.sort()
  25.         for s in tags: fp.write(s)
  26.  
  27.  
  28. expr = '^[ \t]*\(def\|class\)[ \t]+\([a-zA-Z0-9_]+\)[ \t]*[:(]'
  29. matcher = regex.compile(expr)
  30.  
  31. def treat_file(file):
  32.     try:
  33.         fp = open(file, 'r')
  34.     except:
  35.         print 'Cannot open', file
  36.         return
  37.     base = os.path.basename(file)
  38.     if base[-3:] == '.py': base = base[:-3]
  39.     s = base + '\t' + file + '\t' + '1\n'
  40.     tags.append(s)
  41.     while 1:
  42.         line = fp.readline()
  43.         if not line: break
  44.         if matcher.search(line) >= 0:
  45.             (a, b), (a1, b1), (a2, b2) = matcher.regs[:3]
  46.             name = line[a2:b2]
  47.             s = name + '\t' + file + '\t/^' + line[a:b] + '/\n'
  48.             tags.append(s)
  49.  
  50. main()
  51.