#!/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")
