[PATCH libinput] udev: add the hwdb_parser.py test from systemd

Peter Hutterer peter.hutterer at who-t.net
Fri Oct 28 05:33:12 UTC 2016


upstream for this file lives in systemd, any changes to the actual parser
should flow back there.

libinput's matches are fairly simple. We have the various LIBINPUT_MODEL_ tags
that just take a "1" and the two attributes that are dimensions.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 configure.ac                       |   4 +
 udev/90-libinput-model-quirks.hwdb |   2 +-
 udev/Makefile.am                   |   4 +
 udev/parse_hwdb.py                 | 178 +++++++++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100755 udev/parse_hwdb.py

diff --git a/configure.ac b/configure.ac
index 38b465d..8658247 100644
--- a/configure.ac
+++ b/configure.ac
@@ -46,6 +46,10 @@ AC_PROG_CC_C99
 AC_PROG_CXX # Only used by build C++ test
 AC_PROG_GREP
 
+# Only used for testing the hwdb
+AM_PATH_PYTHON([3.0],, [:])
+AM_CONDITIONAL([HAVE_PYTHON], [test "$PYTHON" != :])
+
 # Initialize libtool
 LT_PREREQ([2.2])
 LT_INIT
diff --git a/udev/90-libinput-model-quirks.hwdb b/udev/90-libinput-model-quirks.hwdb
index ec71c26..fed28e2 100644
--- a/udev/90-libinput-model-quirks.hwdb
+++ b/udev/90-libinput-model-quirks.hwdb
@@ -96,7 +96,7 @@ libinput:name:SynPS/2 Synaptics TouchPad:dmi:*svnHewlett-Packard:*pnHPCompaq8510
  LIBINPUT_MODEL_HP8510_TOUCHPAD=1
 
 # HP Stream 11
-evdev:name:SYN1EDE:00 06CB:7442:dmi:*svnHewlett-Packard:pnHPStreamNotebookPC11*
+libinput:name:SYN1EDE:00 06CB:7442:dmi:*svnHewlett-Packard:pnHPStreamNotebookPC11*
  LIBINPUT_MODEL_HP_STREAM11_TOUCHPAD=1
 
 ##########################################
diff --git a/udev/Makefile.am b/udev/Makefile.am
index f06dc56..74103f4 100644
--- a/udev/Makefile.am
+++ b/udev/Makefile.am
@@ -41,3 +41,7 @@ DISTCLEANFILES = \
 		 80-libinput-device-groups.rules \
 		 90-libinput-model-quirks.rules 
 EXTRA_DIST = 80-libinput-test-device.rules
+
+if HAVE_PYTHON
+TESTS = parse_hwdb.py
+endif
diff --git a/udev/parse_hwdb.py b/udev/parse_hwdb.py
new file mode 100755
index 0000000..99e9c59
--- /dev/null
+++ b/udev/parse_hwdb.py
@@ -0,0 +1,178 @@
+#!/usr/bin/python3
+# vim: set expandtab shiftwidth=4:
+# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
+#
+# ANY MODIFICATIONS TO THIS FILE SHOULD BE MERGED INTO THE SYSTEMD UPSTREAM
+#
+# This file is part of systemd. It is distributed under the MIT license, see
+# below.
+#
+# Copyright 2016 Zbigniew Jędrzejewski-Szmek
+#
+# The MIT License (MIT)
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+import functools
+import glob
+import string
+import sys
+import os
+
+try:
+    from pyparsing import (Word, White, Literal, ParserElement, Regex,
+                           LineStart, LineEnd,
+                           ZeroOrMore, OneOrMore, Combine, Or, Optional, Suppress, Group,
+                           nums, alphanums, printables,
+                           stringEnd, pythonStyleComment,
+                           ParseBaseException)
+except ImportError:
+    print('pyparsing is not available')
+    sys.exit(77)
+
+try:
+    from evdev.ecodes import ecodes
+except ImportError:
+    ecodes = None
+    print('WARNING: evdev is not available')
+
+EOL = LineEnd().suppress()
+EMPTYLINE = LineStart() + LineEnd()
+COMMENTLINE = pythonStyleComment + EOL
+INTEGER = Word(nums)
+REAL = Combine((INTEGER + Optional('.' + Optional(INTEGER))) ^ ('.' + INTEGER))
+UDEV_TAG = Word(string.ascii_uppercase, alphanums + '_')
+
+TYPES = {
+         'libinput': ('name', 'touchpad', 'mouse'),
+         }
+
+ at functools.lru_cache()
+def hwdb_grammar():
+    ParserElement.setDefaultWhitespaceChars('')
+
+    prefix = Or(category + ':' + Or(conn) + ':'
+                for category, conn in TYPES.items())
+    matchline = Combine(prefix + Word(printables + ' ' + '®')) + EOL
+    propertyline = (White(' ', exact=1).suppress() +
+                    Combine(UDEV_TAG - '=' - Word(alphanums + '_=:@*.! ') - Optional(pythonStyleComment)) +
+                    EOL)
+    propertycomment = White(' ', exact=1) + pythonStyleComment + EOL
+
+    group = (OneOrMore(matchline('MATCHES*') ^ COMMENTLINE.suppress()) -
+             OneOrMore(propertyline('PROPERTIES*') ^ propertycomment.suppress()) -
+             (EMPTYLINE ^ stringEnd()).suppress() )
+    commentgroup = OneOrMore(COMMENTLINE).suppress() - EMPTYLINE.suppress()
+
+    grammar = OneOrMore(group('GROUPS*') ^ commentgroup) + stringEnd()
+
+    return grammar
+
+ at functools.lru_cache()
+def property_grammar():
+    ParserElement.setDefaultWhitespaceChars(' ')
+
+    model_props = [Regex(r'LIBINPUT_MODEL_[_0-9A-Z]+')('NAME')
+                   - Suppress('=') -
+                   (Literal('1'))('VALUE')
+                  ]
+
+    dimension = INTEGER('X') + Suppress('x') + INTEGER('Y')
+    sz_props = (
+            ('LIBINPUT_ATTR_SIZE_HINT', Group(dimension('SETTINGS*'))),
+            ('LIBINPUT_ATTR_RESOLUTION_HINT', Group(dimension('SETTINGS*'))),
+            )
+    size_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
+                   for name, val in sz_props]
+
+    grammar = Or(model_props + size_props);
+
+    return grammar
+
+ERROR = False
+def error(fmt, *args, **kwargs):
+    global ERROR
+    ERROR = True
+    print(fmt.format(*args, **kwargs))
+
+def convert_properties(group):
+    matches = [m[0] for m in group.MATCHES]
+    props = [p[0] for p in group.PROPERTIES]
+    return matches, props
+
+def parse(fname):
+    grammar = hwdb_grammar()
+    try:
+        parsed = grammar.parseFile(fname)
+    except ParseBaseException as e:
+        error('Cannot parse {}: {}', fname, e)
+        return []
+    return [convert_properties(g) for g in parsed.GROUPS]
+
+def check_match_uniqueness(groups):
+    matches = sum((group[0] for group in groups), [])
+    matches.sort()
+    prev = None
+    for match in matches:
+        if match == prev:
+            error('Match {!r} is duplicated', match)
+        prev = match
+
+def check_one_dimension(prop, value):
+    if int(value[0]) <= 0 or int(value[1]) <= 0:
+        error('Dimension {} invalid', value)
+
+def check_properties(groups):
+    grammar = property_grammar()
+    for matches, props in groups:
+        prop_names = set()
+        for prop in props:
+            # print('--', prop)
+            prop = prop.partition('#')[0].rstrip()
+            try:
+                parsed = grammar.parseString(prop)
+            except ParseBaseException as e:
+                error('Failed to parse: {!r}', prop)
+                continue
+            # print('{!r}'.format(parsed))
+            if parsed.NAME in prop_names:
+                error('Property {} is duplicated', parsed.NAME)
+            prop_names.add(parsed.NAME)
+            if parsed.NAME == "LIBINPUT_ATTR_SIZE_HINT" or \
+               parsed.NAME == "LIBINPUT_ATTR_RESOLUTION_HINT":
+                check_one_dimension(prop, parsed.VALUE)
+
+def print_summary(fname, groups):
+    print('{}: {} match groups, {} matches, {} properties'
+          .format(fname,
+                  len(groups),
+                  sum(len(matches) for matches, props in groups),
+                  sum(len(props) for matches, props in groups),
+          ))
+
+if __name__ == '__main__':
+    args = sys.argv[1:] or glob.glob(os.path.dirname(sys.argv[0]) + '/*.hwdb')
+
+    for fname in args:
+        groups = parse(fname)
+        print_summary(fname, groups)
+        check_match_uniqueness(groups)
+        check_properties(groups)
+
+    sys.exit(ERROR)
-- 
2.7.4



More information about the wayland-devel mailing list