|
2 | 2 | Generate a Python extension module with the constants defined in linux/input.h. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import os, sys, re |
6 | | - |
| 5 | +import getopt |
| 6 | +import os |
| 7 | +import re |
| 8 | +import sys |
7 | 9 |
|
8 | 10 | # ----------------------------------------------------------------------------- |
9 | 11 | # The default header file locations to try. |
|
13 | 15 | "/usr/include/linux/uinput.h", |
14 | 16 | ] |
15 | 17 |
|
16 | | -if sys.argv[1:]: |
17 | | - headers = sys.argv[1:] |
| 18 | +opts, args = getopt.getopt(sys.argv[1:], "", ["ecodes", "stubs"]) |
| 19 | +if not opts: |
| 20 | + print("usage: genecodes.py [--ecodes|--stubs] <headers>") |
| 21 | + exit(2) |
18 | 22 |
|
19 | 23 |
|
20 | 24 | # ----------------------------------------------------------------------------- |
|
27 | 31 |
|
28 | 32 |
|
29 | 33 | # ----------------------------------------------------------------------------- |
30 | | -template = r""" |
| 34 | +template_ecodes = r""" |
31 | 35 | #include <Python.h> |
32 | 36 | #ifdef __FreeBSD__ |
33 | 37 | #include <dev/evdev/input.h> |
|
37 | 41 | #endif |
38 | 42 |
|
39 | 43 | /* Automatically generated by evdev.genecodes */ |
40 | | -/* Generated on %s */ |
| 44 | +/* Generated on %s */ |
| 45 | +/* Generated from %s */ |
41 | 46 |
|
42 | 47 | #define MODULE_NAME "_ecodes" |
43 | 48 | #define MODULE_HELP "linux/input.h macros" |
|
71 | 76 | """ |
72 | 77 |
|
73 | 78 |
|
74 | | -def parse_header(header): |
75 | | - for line in open(header): |
76 | | - macro = macro_regex.search(line) |
77 | | - if macro: |
78 | | - yield " PyModule_AddIntMacro(m, %s);" % macro.group(1) |
| 79 | +template_stubs = r""" |
| 80 | +# Automatically generated by evdev.genecodes |
| 81 | +# Generated on %s |
| 82 | +# Generated from %s |
| 83 | +
|
| 84 | +# pylint: skip-file |
| 85 | +
|
| 86 | +ecodes: dict[str, int] |
| 87 | +keys: dict[int, str|list[str]] |
| 88 | +bytype: dict[int, dict[int, str|list[str]]] |
| 89 | +
|
| 90 | +KEY: dict[int, str|list[str]] |
| 91 | +ABS: dict[int, str|list[str]] |
| 92 | +REL: dict[int, str|list[str]] |
| 93 | +SW: dict[int, str|list[str]] |
| 94 | +MSC: dict[int, str|list[str]] |
| 95 | +LED: dict[int, str|list[str]] |
| 96 | +BTN: dict[int, str|list[str]] |
| 97 | +REP: dict[int, str|list[str]] |
| 98 | +SND: dict[int, str|list[str]] |
| 99 | +ID: dict[int, str|list[str]] |
| 100 | +EV: dict[int, str|list[str]] |
| 101 | +BUS: dict[int, str|list[str]] |
| 102 | +SYN: dict[int, str|list[str]] |
| 103 | +FF_STATUS: dict[int, str|list[str]] |
| 104 | +FF_INPUT_PROP: dict[int, str|list[str]] |
| 105 | +
|
| 106 | +%s |
| 107 | +""" |
79 | 108 |
|
80 | 109 |
|
81 | | -all_macros = [] |
82 | | -for header in headers: |
83 | | - try: |
84 | | - fh = open(header) |
85 | | - except (IOError, OSError): |
86 | | - continue |
87 | | - all_macros += parse_header(header) |
| 110 | +def parse_headers(headers=headers): |
| 111 | + for header in headers: |
| 112 | + try: |
| 113 | + fh = open(header) |
| 114 | + except (IOError, OSError): |
| 115 | + continue |
88 | 116 |
|
| 117 | + for line in fh: |
| 118 | + macro = macro_regex.search(line) |
| 119 | + if macro: |
| 120 | + yield macro.group(1) |
| 121 | + |
| 122 | + |
| 123 | +all_macros = list(parse_headers()) |
89 | 124 | if not all_macros: |
90 | 125 | print("no input macros found in: %s" % " ".join(headers), file=sys.stderr) |
91 | 126 | sys.exit(1) |
92 | 127 |
|
93 | | - |
94 | | -macros = os.linesep.join(all_macros) |
95 | | -print(template % (uname, macros)) |
| 128 | +# pylint: disable=possibly-used-before-assignment, used-before-assignment |
| 129 | +if ("--ecodes", "") in opts: |
| 130 | + body = (" PyModule_AddIntMacro(m, %s);" % macro for macro in all_macros) |
| 131 | + template = template_ecodes |
| 132 | +elif ("--stubs", "") in opts: |
| 133 | + body = ("%s: int" % macro for macro in all_macros) |
| 134 | + template = template_stubs |
| 135 | + |
| 136 | +body = os.linesep.join(body) |
| 137 | +text = template % (uname, headers, body) |
| 138 | +print(text.strip()) |
0 commit comments