home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / PTAGS.PY < prev    next >
Encoding:
Python Source  |  2001-01-17  |  1.2 KB  |  52 lines

  1. #! /usr/bin/env 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, re, os
  14.  
  15. tags = []    # Modified global variable!
  16.  
  17. def main():
  18.     args = sys.argv[1:]
  19.     for file in args: treat_file(file)
  20.     if tags:
  21.         fp = open('tags', 'w')
  22.         tags.sort()
  23.         for s in tags: fp.write(s)
  24.  
  25.  
  26. expr = '^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
  27. matcher = re.compile(expr)
  28.  
  29. def treat_file(file):
  30.     try:
  31.         fp = open(file, 'r')
  32.     except:
  33.         sys.stderr.write('Cannot open %s\n' % file)
  34.         return
  35.     base = os.path.basename(file)
  36.     if base[-3:] == '.py':
  37.         base = base[:-3]
  38.     s = base + '\t' + file + '\t' + '1\n'
  39.     tags.append(s)
  40.     while 1:
  41.         line = fp.readline()
  42.         if not line:
  43.             break
  44.         m = matcher.match(line)
  45.         if m:
  46.             content = m.group(0)
  47.             name = m.group(2)
  48.             s = name + '\t' + file + '\t/^' + content + '/\n'
  49.             tags.append(s)
  50.  
  51. main()
  52.