Get rid of the usage of PATH_MAX
[platform/upstream/libxkbcommon.git] / makekeys.py
1 #!/usr/bin/env python
2
3 import re, sys, itertools
4
5 pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s')
6 matches = [pattern.match(line) for line in open(sys.argv[1])]
7 entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m]
8
9 print('''struct name_keysym {
10     const char *name;
11     xkb_keysym_t keysym;
12 };\n''')
13
14 print('static const struct name_keysym name_to_keysym[] = {');
15 for (name, _) in sorted(entries, key=lambda e: e[0].lower()):
16     print('    {{ "{name}", XKB_KEY_{name} }},'.format(name=name))
17 print('};\n')
18
19 # *.sort() is stable so we always get the first keysym for duplicate
20 print('static const struct name_keysym keysym_to_name[] = {');
21 for (name, _) in (next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1])):
22     print('    {{ "{name}", XKB_KEY_{name} }},'.format(name=name))
23 print('};')