home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / EPTAGS.PY < prev    next >
Encoding:
Python Source  |  2000-09-18  |  1.5 KB  |  57 lines

  1. #! /usr/bin/env python
  2. """Create a TAGS file for Python programs, usable with GNU Emacs.
  3.  
  4. usage: eptags pyfiles...
  5.  
  6. The output TAGS file is usable with Emacs version 18, 19, 20.
  7. Tagged are:
  8.  - functions (even inside other defs or classes)
  9.  - classes
  10.  
  11. eptags warns about files it cannot open.
  12. eptags will not give warnings about duplicate tags.
  13.  
  14. BUGS:
  15.    Because of tag duplication (methods with the same name in different
  16.    classes), TAGS files are not very useful for most object-oriented
  17.    python projects.
  18. """
  19. import sys,re
  20.  
  21. expr = r'^[ \t]*(def|class)[ \t]+([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*[:\(]'
  22. matcher = re.compile(expr)
  23.  
  24. def treat_file(file, outfp):
  25.     """Append tags found in file named 'file' to the open file 'outfp'"""
  26.     try:
  27.         fp = open(file, 'r')
  28.     except:
  29.         sys.stderr.write('Cannot open %s\n'%file)
  30.         return
  31.     charno = 0
  32.     lineno = 0
  33.     tags = []
  34.     size = 0
  35.     while 1:
  36.         line = fp.readline()
  37.         if not line:
  38.             break
  39.         lineno = lineno + 1
  40.         m = matcher.search(line)
  41.         if m:
  42.             tag = m.group(0) + '\177%d,%d\n'%(lineno,charno)
  43.             tags.append(tag)
  44.             size = size + len(tag)
  45.         charno = charno + len(line)
  46.     outfp.write('\f\n%s,%d\n'%(file,size))
  47.     for tag in tags:
  48.         outfp.write(tag)
  49.  
  50. def main():
  51.     outfp = open('TAGS', 'w')
  52.     for file in sys.argv[1:]:
  53.         treat_file(file, outfp)
  54.  
  55. if __name__=="__main__":
  56.     main()
  57.