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