[PATCH evemu] python: call only once the libevdev backend per request

Benjamin Tissoires benjamin.tissoires at gmail.com
Fri Aug 29 14:34:16 PDT 2014


We can factorize some code, and provide a cleaner way of implementing
event_get_value(), event_get_name(), input_prop_get_name() and
input_prop_get_value()

Signed-off-by: Benjamin Tissoires <benjamin.tissoires at gmail.com>
---
 python/evemu/__init__.py | 112 ++++++++++++++++++++++++++---------------------
 1 file changed, 62 insertions(+), 50 deletions(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 65e4673..22035b8 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -37,6 +37,54 @@ __all__ = ["Device",
 
 _libevdev = evemu.base.LibEvdev()
 
+def _event_type_get_value_name(event_type):
+    event_type_value = event_type_name = None
+
+    if isinstance(event_type, int):
+        event_type_name = _libevdev.libevdev_event_type_get_name(event_type)
+        if event_type_name == None:
+            return None, None
+        event_type_value = event_type
+    else:
+        event_type_name = str(event_type)
+        event_type_value = _libevdev.libevdev_event_type_from_name(event_type_name)
+        if event_type_value < 0:
+            return None, None
+
+    return event_type_value, event_type_name
+
+def _event_code_get_value_name(event_type_value, event_code):
+    event_code_value = event_code_name = None
+
+    if isinstance(event_code, int):
+        event_code_name = _libevdev.libevdev_event_code_get_name(event_type_value, event_code)
+        if event_code_name == None:
+            return None, None
+        event_code_value = event_code
+    else:
+        event_code_name = str(event_code)
+        event_code_value = _libevdev.libevdev_event_code_from_name(event_type_value, event_code_name)
+        if event_code_value < 0:
+            return None, None
+
+    return event_code_value, event_code_name
+
+def _input_prop_get_value_name(prop):
+    prop_value = prop_name = None
+
+    if isinstance(prop, int):
+        prop_name = _libevdev.libevdev_property_get_name(prop)
+        if prop_name == None:
+            return None, None
+        prop_value = prop
+    else:
+        prop_name = str(prop)
+        prop_value = _libevdev.libevdev_property_from_name(prop_name)
+        if prop_value < 0:
+            return None, None
+
+    return prop_value, prop_name
+
 def event_get_value(event_type, event_code = None):
     """
     Return the integer-value for the given event type and/or code string
@@ -46,27 +94,13 @@ def event_get_value(event_type, event_code = None):
     If an event code is passed, the event type may be given as integer or
     string.
     """
-    t = -1
-    c = -1
-
-    if isinstance(event_type, int):
-        event_type = _libevdev.libevdev_event_type_get_name(event_type)
-        if event_type == 0: # NULL
-            return None
-
-    t = _libevdev.libevdev_event_type_from_name(str(event_type))
-
-    if event_code == None:
-        return None if t < 0 else t
+    event_type_value, event_type_name = _event_type_get_value_name(event_type)
+    if event_code == None or event_type_value == None:
+        return event_type_value
 
-    if isinstance(event_code, int):
-        event_code = _libevdev.libevdev_event_code_get_name(t, event_code)
-        if event_code == 0: # NULL
-            return None
-
-    c = _libevdev.libevdev_event_code_from_name(t, str(event_code))
+    event_code_value, event_code_name = _event_code_get_value_name(event_type_value, event_code)
 
-    return None if c < 0 else c
+    return event_code_value
 
 def event_get_name(event_type, event_code = None):
     """
@@ -77,51 +111,29 @@ def event_get_name(event_type, event_code = None):
     If an event code is passed, the event type may be given as integer or
     string.
     """
-    if not isinstance(event_type, int):
-        event_type = event_get_value(event_type)
-
-    if event_type == None:
-        return None
+    event_type_value, event_type_name = _event_type_get_value_name(event_type)
+    if event_code == None or event_type_name == None:
+        return event_type_name
 
-    if event_code == None:
-        type_name = _libevdev.libevdev_event_type_get_name(event_type)
-        return None if type_name == 0 else type_name
+    event_code_value, event_code_name = _event_code_get_value_name(event_type_value, event_code)
 
-    if not isinstance(event_code, int):
-        event_code = event_get_value(event_type, event_code)
-
-    if event_code == None:
-        return None
-
-    code_name = _libevdev.libevdev_event_code_get_name(event_type, event_code)
-
-    return None if code_name == 0 else code_name
+    return event_code_name
 
 def input_prop_get_name(prop):
     """
     Return the name of the input property, or None if undefined.
     """
-    if not isinstance(prop, int):
-        prop = input_prop_get_value(prop)
+    prop_value, prop_name = _input_prop_get_value_name(prop)
 
-    if prop == None:
-        return None
-
-    prop = _libevdev.libevdev_property_get_name(prop)
-    return None if prop == 0 else prop
+    return prop_name
 
 def input_prop_get_value(prop):
     """
     Return the value of the input property, or None if undefined.
     """
-    if isinstance(prop, int):
-        prop = input_prop_get_name(prop)
-
-    if prop == None:
-        return None
+    prop_value, prop_name = _input_prop_get_value_name(prop)
 
-    prop = _libevdev.libevdev_property_from_name(str(prop))
-    return None if prop < 0 else prop
+    return prop_value
 
 class InputEvent(object):
     __slots__ = 'sec', 'usec', 'type', 'code', 'value'
-- 
1.9.3



More information about the Input-tools mailing list