Update to 2.7.3
[profile/ivi/python.git] / Tools / scripts / ptags.py
1 #! /usr/bin/env 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, re, os
14
15 tags = []    # Modified global variable!
16
17 def main():
18     args = sys.argv[1:]
19     for filename in args:
20         treat_file(filename)
21     if tags:
22         fp = open('tags', 'w')
23         tags.sort()
24         for s in tags: fp.write(s)
25
26
27 expr = '^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
28 matcher = re.compile(expr)
29
30 def treat_file(filename):
31     try:
32         fp = open(filename, 'r')
33     except:
34         sys.stderr.write('Cannot open %s\n' % filename)
35         return
36     base = os.path.basename(filename)
37     if base[-3:] == '.py':
38         base = base[:-3]
39     s = base + '\t' + filename + '\t' + '1\n'
40     tags.append(s)
41     while 1:
42         line = fp.readline()
43         if not line:
44             break
45         m = matcher.match(line)
46         if m:
47             content = m.group(0)
48             name = m.group(2)
49             s = name + '\t' + filename + '\t/^' + content + '/\n'
50             tags.append(s)
51
52 if __name__ == '__main__':
53     main()