1 |
Inherited from systemd project. |
2 |
|
3 |
Signed-off-by: Marcus Folkesson <marcus.folkesson@×××××.com> |
4 |
--- |
5 |
hwdb/parse_hwdb.py | 246 +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 |
1 file changed, 246 insertions(+) |
7 |
create mode 100755 hwdb/parse_hwdb.py |
8 |
|
9 |
diff --git a/hwdb/parse_hwdb.py b/hwdb/parse_hwdb.py |
10 |
new file mode 100755 |
11 |
index 000000000..f4cc9c697 |
12 |
--- /dev/null |
13 |
+++ b/hwdb/parse_hwdb.py |
14 |
@@ -0,0 +1,246 @@ |
15 |
+#!/usr/bin/env python3 |
16 |
+# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
17 |
+# SPDX-License-Identifier: MIT |
18 |
+# |
19 |
+# This file is part of systemd. It is distrubuted under the MIT license, see |
20 |
+# below. |
21 |
+# |
22 |
+# Copyright 2016 Zbigniew Jędrzejewski-Szmek |
23 |
+# |
24 |
+# The MIT License (MIT) |
25 |
+# |
26 |
+# Permission is hereby granted, free of charge, to any person obtaining a copy |
27 |
+# of this software and associated documentation files (the "Software"), to deal |
28 |
+# in the Software without restriction, including without limitation the rights |
29 |
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
30 |
+# copies of the Software, and to permit persons to whom the Software is |
31 |
+# furnished to do so, subject to the following conditions: |
32 |
+# |
33 |
+# The above copyright notice and this permission notice shall be included in |
34 |
+# all copies or substantial portions of the Software. |
35 |
+# |
36 |
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
37 |
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
38 |
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
39 |
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
40 |
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
41 |
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
42 |
+# SOFTWARE. |
43 |
+ |
44 |
+import glob |
45 |
+import string |
46 |
+import sys |
47 |
+import os |
48 |
+ |
49 |
+try: |
50 |
+ from pyparsing import (Word, White, Literal, ParserElement, Regex, |
51 |
+ LineStart, LineEnd, |
52 |
+ OneOrMore, Combine, Or, Optional, Suppress, Group, |
53 |
+ nums, alphanums, printables, |
54 |
+ stringEnd, pythonStyleComment, QuotedString, |
55 |
+ ParseBaseException) |
56 |
+except ImportError: |
57 |
+ print('pyparsing is not available') |
58 |
+ sys.exit(77) |
59 |
+ |
60 |
+try: |
61 |
+ from evdev.ecodes import ecodes |
62 |
+except ImportError: |
63 |
+ ecodes = None |
64 |
+ print('WARNING: evdev is not available') |
65 |
+ |
66 |
+try: |
67 |
+ from functools import lru_cache |
68 |
+except ImportError: |
69 |
+ # don't do caching on old python |
70 |
+ lru_cache = lambda: (lambda f: f) |
71 |
+ |
72 |
+EOL = LineEnd().suppress() |
73 |
+EMPTYLINE = LineEnd() |
74 |
+COMMENTLINE = pythonStyleComment + EOL |
75 |
+INTEGER = Word(nums) |
76 |
+STRING = QuotedString('"') |
77 |
+REAL = Combine((INTEGER + Optional('.' + Optional(INTEGER))) ^ ('.' + INTEGER)) |
78 |
+SIGNED_REAL = Combine(Optional(Word('-+')) + REAL) |
79 |
+UDEV_TAG = Word(string.ascii_uppercase, alphanums + '_') |
80 |
+ |
81 |
+TYPES = {'mouse': ('usb', 'bluetooth', 'ps2', '*'), |
82 |
+ 'evdev': ('name', 'atkbd', 'input'), |
83 |
+ 'id-input': ('modalias'), |
84 |
+ 'touchpad': ('i8042', 'rmi', 'bluetooth', 'usb'), |
85 |
+ 'joystick': ('i8042', 'rmi', 'bluetooth', 'usb'), |
86 |
+ 'keyboard': ('name', ), |
87 |
+ 'sensor': ('modalias', ), |
88 |
+ } |
89 |
+ |
90 |
+@lru_cache() |
91 |
+def hwdb_grammar(): |
92 |
+ ParserElement.setDefaultWhitespaceChars('') |
93 |
+ |
94 |
+ prefix = Or(category + ':' + Or(conn) + ':' |
95 |
+ for category, conn in TYPES.items()) |
96 |
+ matchline = Combine(prefix + Word(printables + ' ' + '®')) + EOL |
97 |
+ propertyline = (White(' ', exact=1).suppress() + |
98 |
+ Combine(UDEV_TAG - '=' - Word(alphanums + '_=:@*.!-;, "') - Optional(pythonStyleComment)) + |
99 |
+ EOL) |
100 |
+ propertycomment = White(' ', exact=1) + pythonStyleComment + EOL |
101 |
+ |
102 |
+ group = (OneOrMore(matchline('MATCHES*') ^ COMMENTLINE.suppress()) - |
103 |
+ OneOrMore(propertyline('PROPERTIES*') ^ propertycomment.suppress()) - |
104 |
+ (EMPTYLINE ^ stringEnd()).suppress()) |
105 |
+ commentgroup = OneOrMore(COMMENTLINE).suppress() - EMPTYLINE.suppress() |
106 |
+ |
107 |
+ grammar = OneOrMore(group('GROUPS*') ^ commentgroup) + stringEnd() |
108 |
+ |
109 |
+ return grammar |
110 |
+ |
111 |
+@lru_cache() |
112 |
+def property_grammar(): |
113 |
+ ParserElement.setDefaultWhitespaceChars(' ') |
114 |
+ |
115 |
+ dpi_setting = (Optional('*')('DEFAULT') + INTEGER('DPI') + Suppress('@') + INTEGER('HZ'))('SETTINGS*') |
116 |
+ mount_matrix_row = SIGNED_REAL + ',' + SIGNED_REAL + ',' + SIGNED_REAL |
117 |
+ mount_matrix = (mount_matrix_row + ';' + mount_matrix_row + ';' + mount_matrix_row)('MOUNT_MATRIX') |
118 |
+ |
119 |
+ props = (('MOUSE_DPI', Group(OneOrMore(dpi_setting))), |
120 |
+ ('MOUSE_WHEEL_CLICK_ANGLE', INTEGER), |
121 |
+ ('MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL', INTEGER), |
122 |
+ ('MOUSE_WHEEL_CLICK_COUNT', INTEGER), |
123 |
+ ('MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL', INTEGER), |
124 |
+ ('ID_INPUT', Literal('1')), |
125 |
+ ('ID_INPUT_ACCELEROMETER', Literal('1')), |
126 |
+ ('ID_INPUT_JOYSTICK', Literal('1')), |
127 |
+ ('ID_INPUT_KEY', Literal('1')), |
128 |
+ ('ID_INPUT_KEYBOARD', Literal('1')), |
129 |
+ ('ID_INPUT_MOUSE', Literal('1')), |
130 |
+ ('ID_INPUT_POINTINGSTICK', Literal('1')), |
131 |
+ ('ID_INPUT_SWITCH', Literal('1')), |
132 |
+ ('ID_INPUT_TABLET', Literal('1')), |
133 |
+ ('ID_INPUT_TABLET_PAD', Literal('1')), |
134 |
+ ('ID_INPUT_TOUCHPAD', Literal('1')), |
135 |
+ ('ID_INPUT_TOUCHSCREEN', Literal('1')), |
136 |
+ ('ID_INPUT_TRACKBALL', Literal('1')), |
137 |
+ ('MOUSE_WHEEL_TILT_HORIZONTAL', Literal('1')), |
138 |
+ ('MOUSE_WHEEL_TILT_VERTICAL', Literal('1')), |
139 |
+ ('POINTINGSTICK_SENSITIVITY', INTEGER), |
140 |
+ ('POINTINGSTICK_CONST_ACCEL', REAL), |
141 |
+ ('ID_INPUT_JOYSTICK_INTEGRATION', Or(('internal', 'external'))), |
142 |
+ ('ID_INPUT_TOUCHPAD_INTEGRATION', Or(('internal', 'external'))), |
143 |
+ ('XKB_FIXED_LAYOUT', STRING), |
144 |
+ ('XKB_FIXED_VARIANT', STRING), |
145 |
+ ('KEYBOARD_LED_NUMLOCK', Literal('0')), |
146 |
+ ('KEYBOARD_LED_CAPSLOCK', Literal('0')), |
147 |
+ ('ACCEL_MOUNT_MATRIX', mount_matrix), |
148 |
+ ) |
149 |
+ fixed_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE') |
150 |
+ for name, val in props] |
151 |
+ kbd_props = [Regex(r'KEYBOARD_KEY_[0-9a-f]+')('NAME') |
152 |
+ - Suppress('=') - |
153 |
+ ('!' ^ (Optional('!') - Word(alphanums + '_')))('VALUE') |
154 |
+ ] |
155 |
+ abs_props = [Regex(r'EVDEV_ABS_[0-9a-f]{2}')('NAME') |
156 |
+ - Suppress('=') - |
157 |
+ Word(nums + ':')('VALUE') |
158 |
+ ] |
159 |
+ |
160 |
+ grammar = Or(fixed_props + kbd_props + abs_props) + EOL |
161 |
+ |
162 |
+ return grammar |
163 |
+ |
164 |
+ERROR = False |
165 |
+def error(fmt, *args, **kwargs): |
166 |
+ global ERROR |
167 |
+ ERROR = True |
168 |
+ print(fmt.format(*args, **kwargs)) |
169 |
+ |
170 |
+def convert_properties(group): |
171 |
+ matches = [m[0] for m in group.MATCHES] |
172 |
+ props = [p[0] for p in group.PROPERTIES] |
173 |
+ return matches, props |
174 |
+ |
175 |
+def parse(fname): |
176 |
+ grammar = hwdb_grammar() |
177 |
+ try: |
178 |
+ with open(fname, 'r', encoding='UTF-8') as f: |
179 |
+ parsed = grammar.parseFile(f) |
180 |
+ except ParseBaseException as e: |
181 |
+ error('Cannot parse {}: {}', fname, e) |
182 |
+ return [] |
183 |
+ return [convert_properties(g) for g in parsed.GROUPS] |
184 |
+ |
185 |
+def check_match_uniqueness(groups): |
186 |
+ matches = sum((group[0] for group in groups), []) |
187 |
+ matches.sort() |
188 |
+ prev = None |
189 |
+ for match in matches: |
190 |
+ if match == prev: |
191 |
+ error('Match {!r} is duplicated', match) |
192 |
+ prev = match |
193 |
+ |
194 |
+def check_one_default(prop, settings): |
195 |
+ defaults = [s for s in settings if s.DEFAULT] |
196 |
+ if len(defaults) > 1: |
197 |
+ error('More than one star entry: {!r}', prop) |
198 |
+ |
199 |
+def check_one_mount_matrix(prop, value): |
200 |
+ numbers = [s for s in value if s not in {';', ','}] |
201 |
+ if len(numbers) != 9: |
202 |
+ error('Wrong accel matrix: {!r}', prop) |
203 |
+ try: |
204 |
+ numbers = [abs(float(number)) for number in numbers] |
205 |
+ except ValueError: |
206 |
+ error('Wrong accel matrix: {!r}', prop) |
207 |
+ bad_x, bad_y, bad_z = max(numbers[0:3]) == 0, max(numbers[3:6]) == 0, max(numbers[6:9]) == 0 |
208 |
+ if bad_x or bad_y or bad_z: |
209 |
+ error('Mount matrix is all zero in {} row: {!r}', |
210 |
+ 'x' if bad_x else ('y' if bad_y else 'z'), |
211 |
+ prop) |
212 |
+ |
213 |
+def check_one_keycode(prop, value): |
214 |
+ if value != '!' and ecodes is not None: |
215 |
+ key = 'KEY_' + value.upper() |
216 |
+ if key not in ecodes: |
217 |
+ key = value.upper() |
218 |
+ if key not in ecodes: |
219 |
+ error('Keycode {} unknown', key) |
220 |
+ |
221 |
+def check_properties(groups): |
222 |
+ grammar = property_grammar() |
223 |
+ for matches, props in groups: |
224 |
+ prop_names = set() |
225 |
+ for prop in props: |
226 |
+ # print('--', prop) |
227 |
+ prop = prop.partition('#')[0].rstrip() |
228 |
+ try: |
229 |
+ parsed = grammar.parseString(prop) |
230 |
+ except ParseBaseException as e: |
231 |
+ error('Failed to parse: {!r}', prop) |
232 |
+ continue |
233 |
+ # print('{!r}'.format(parsed)) |
234 |
+ if parsed.NAME in prop_names: |
235 |
+ error('Property {} is duplicated', parsed.NAME) |
236 |
+ prop_names.add(parsed.NAME) |
237 |
+ if parsed.NAME == 'MOUSE_DPI': |
238 |
+ check_one_default(prop, parsed.VALUE.SETTINGS) |
239 |
+ elif parsed.NAME == 'ACCEL_MOUNT_MATRIX': |
240 |
+ check_one_mount_matrix(prop, parsed.VALUE) |
241 |
+ elif parsed.NAME.startswith('KEYBOARD_KEY_'): |
242 |
+ check_one_keycode(prop, parsed.VALUE) |
243 |
+ |
244 |
+def print_summary(fname, groups): |
245 |
+ print('{}: {} match groups, {} matches, {} properties' |
246 |
+ .format(fname, |
247 |
+ len(groups), |
248 |
+ sum(len(matches) for matches, props in groups), |
249 |
+ sum(len(props) for matches, props in groups))) |
250 |
+ |
251 |
+if __name__ == '__main__': |
252 |
+ args = sys.argv[1:] or glob.glob(os.path.dirname(sys.argv[0]) + '/[67]0-*.hwdb') |
253 |
+ |
254 |
+ for fname in args: |
255 |
+ groups = parse(fname) |
256 |
+ print_summary(fname, groups) |
257 |
+ check_match_uniqueness(groups) |
258 |
+ check_properties(groups) |
259 |
+ |
260 |
+ sys.exit(ERROR) |
261 |
-- |
262 |
2.15.1 |