summaryrefslogtreecommitdiffstats
path: root/Tools/idle/eventparse.py
blob: cb2028dc0370b3c796bd4400d29bed6ec69cf172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python

"""Parse event definitions out of comments in source files."""

import re
import sys
import os
import string
import getopt
import glob
import fileinput
import pprint

def main():
    hits = []
    sublist = []
    args = sys.argv[1:]
    if not args:
        args = filter(lambda s: 'A' <= s[0] <= 'Z', glob.glob("*.py"))
        if not args:
            print "No arguments, no [A-Z]*.py files."
            return 1
    for line in fileinput.input(args):
        if line[:2] == '#$':
            if not sublist:
                sublist.append('file %s' % fileinput.filename())
                sublist.append('line %d' % fileinput.lineno())
            sublist.append(string.strip(line[2:-1]))
        else:
            if sublist:
                hits.append(sublist)
                sublist = []
    if sublist:
        hits.append(sublist)
        sublist = []
    dd = {}
    for sublist in hits:
        d = {}
        for line in sublist:
            words = string.split(line, None, 1)
            if len(words) != 2:
                continue
            tag = words[0]
            l = d.get(tag, [])
            l.append(words[1])
            d[tag] = l
        if d.has_key('event'):
            keys = d['event']
            if len(keys) != 1:
                print "Multiple event keys in", d
                print 'File "%s", line %d' % (d['file'], d['line'])
            key = keys[0]
            if dd.has_key(key):
                print "Duplicate event in", d
                print 'File "%s", line %d' % (d['file'], d['line'])
                return
            dd[key] = d
        else:
            print "No event key in", d
            print 'File "%s", line %d' % (d['file'], d['line'])
    winevents = getevents(dd, "win")
    unixevents = getevents(dd, "unix")
    save = sys.stdout
    f = open("keydefs.py", "w")
    try:
        sys.stdout = f
        print "windows_keydefs = \\"
        pprint.pprint(winevents)
        print
        print "unix_keydefs = \\"
        pprint.pprint(unixevents)
    finally:
        sys.stdout = save
    f.close()

def getevents(dd, key):
    res = {}
    events = dd.keys()
    events.sort()
    for e in events:
        d = dd[e]
        if d.has_key(key) or d.has_key("all"):
            list = []
            for x in d.get(key, []) + d.get("all", []):
                list.append(x)
                if key == "unix" and x[:5] == "<Alt-":
                    x = "<Meta-" + x[5:]
                    list.append(x)
            res[e] = list
    return res

if __name__ == '__main__':
    sys.exit(main())