blob: cb2028dc0370b3c796bd4400d29bed6ec69cf172 [file] [log] [blame]
Guido van Rossum504b0bf1999-01-02 21:28:54 +00001#! /usr/bin/env python
2
3"""Parse event definitions out of comments in source files."""
4
5import re
6import sys
7import os
8import string
9import getopt
10import glob
11import fileinput
12import pprint
13
14def main():
15 hits = []
16 sublist = []
17 args = sys.argv[1:]
18 if not args:
19 args = filter(lambda s: 'A' <= s[0] <= 'Z', glob.glob("*.py"))
20 if not args:
21 print "No arguments, no [A-Z]*.py files."
22 return 1
23 for line in fileinput.input(args):
24 if line[:2] == '#$':
25 if not sublist:
26 sublist.append('file %s' % fileinput.filename())
27 sublist.append('line %d' % fileinput.lineno())
28 sublist.append(string.strip(line[2:-1]))
29 else:
30 if sublist:
31 hits.append(sublist)
32 sublist = []
33 if sublist:
34 hits.append(sublist)
35 sublist = []
36 dd = {}
37 for sublist in hits:
38 d = {}
39 for line in sublist:
40 words = string.split(line, None, 1)
41 if len(words) != 2:
42 continue
43 tag = words[0]
44 l = d.get(tag, [])
45 l.append(words[1])
46 d[tag] = l
47 if d.has_key('event'):
48 keys = d['event']
49 if len(keys) != 1:
50 print "Multiple event keys in", d
51 print 'File "%s", line %d' % (d['file'], d['line'])
52 key = keys[0]
53 if dd.has_key(key):
54 print "Duplicate event in", d
55 print 'File "%s", line %d' % (d['file'], d['line'])
56 return
57 dd[key] = d
58 else:
59 print "No event key in", d
60 print 'File "%s", line %d' % (d['file'], d['line'])
61 winevents = getevents(dd, "win")
62 unixevents = getevents(dd, "unix")
63 save = sys.stdout
64 f = open("keydefs.py", "w")
65 try:
66 sys.stdout = f
67 print "windows_keydefs = \\"
68 pprint.pprint(winevents)
69 print
70 print "unix_keydefs = \\"
71 pprint.pprint(unixevents)
72 finally:
73 sys.stdout = save
74 f.close()
75
76def getevents(dd, key):
77 res = {}
78 events = dd.keys()
79 events.sort()
80 for e in events:
81 d = dd[e]
82 if d.has_key(key) or d.has_key("all"):
83 list = []
84 for x in d.get(key, []) + d.get("all", []):
85 list.append(x)
86 if key == "unix" and x[:5] == "<Alt-":
87 x = "<Meta-" + x[5:]
88 list.append(x)
89 res[e] = list
90 return res
91
92if __name__ == '__main__':
93 sys.exit(main())