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 / eptags.py < prev    next >
Text File  |  1992-10-02  |  1KB  |  51 lines

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