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