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