Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 1 | #! /usr/local/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 regexp |
| 15 | import path |
| 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 | matcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(') |
| 28 | |
| 29 | def treat_file(file): |
| 30 | try: |
| 31 | fp = open(file, 'r') |
| 32 | except: |
| 33 | print 'Cannot open', file |
| 34 | return |
| 35 | base = path.basename(file) |
| 36 | if base[-3:] = '.py': base = base[:-3] |
| 37 | s = base + '\t' + file + '\t' + '1\n' |
| 38 | tags.append(s) |
| 39 | while 1: |
| 40 | line = fp.readline() |
| 41 | if not line: break |
| 42 | res = matcher.exec(line) |
| 43 | if res: |
| 44 | (a, b), (a1, b1), (a2, b2) = res |
| 45 | name = line[a2:b2] |
| 46 | s = name + '\t' + file + '\t/^' + line[a:b] + '/\n' |
| 47 | tags.append(s) |
| 48 | |
| 49 | main() |