File: parse-event-codes

package info (click to toggle)
python-uinput 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 929; ansic: 373; makefile: 31; sh: 1
file content (34 lines) | stat: -rwxr-xr-x 1,127 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

from __future__ import print_function
from __future__ import with_statement

import re
import sys

def parse_event_codes(filepath, ev_type, ev_name):
    with open(filepath) as f:
        ev_codes = {}
        for line in f:
            match = re.match(r"^#define (" + ev_name + "_.*)\t+((?:0x[0-9a-f]+)|(?:\d+))", line)
            if match:
                ev_id   = match.group(1).strip()
                ev_code = match.group(2).strip()
                ev_codes[ev_id] = ev_code
                print("%s = (%s, %s)" % (ev_id, ev_type, ev_code))
                continue

            match = re.match(r"^#define (" + ev_name + "_.*)\t+((?:[A-Z_]+))", line)
            if match:
                ev_id   = match.group(2).strip()
                ev_code = ev_codes[ev_id]
                print("%s = (%s, %s)" % (match.group(1).strip(), ev_type, ev_code))


if __name__ == "__main__":
    filepath = sys.argv[1]

    parse_event_codes(filepath, "0x01", "KEY")
    parse_event_codes(filepath, "0x01", "BTN")
    parse_event_codes(filepath, "0x02", "REL")
    parse_event_codes(filepath, "0x03", "ABS")