[PATCH evemu 04/16] python: be forgiving about invalid event types/codes in the public API
Peter Hutterer
peter.hutterer at who-t.net
Mon Aug 11 18:34:38 PDT 2014
Return None if not found instead of throwing a KeyError which reveals too much
of the internal implementation anyway.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
python/evemu/__init__.py | 14 +++++++++++---
python/evemu/tests/test_device.py | 20 ++++++++++++++++++++
2 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index c7c30f3..f58ab4f 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -34,22 +34,30 @@ __all__ = ["Device"]
def event_get_value(event_type, event_code = None):
"""
Return the integer-value for the given event type and/or code string
- e.g. "EV_ABS" returns 0x03, ("EV_ABS", "ABS_Y") returns 0x01
+ e.g. "EV_ABS" returns 0x03, ("EV_ABS", "ABS_Y") returns 0x01.
+ Unknown event types or type/code combinations return None.
If an event code is passed, the event type may be given as integer or
string.
"""
- return evemu.event_names._event_get_value(event_type, event_code)
+ try:
+ return evemu.event_names._event_get_value(event_type, event_code)
+ except KeyError:
+ return None
def event_get_name(event_type, event_code = None):
"""
Return the string-value for the given event type and/or code value
e.g. 0x03 returns "EV_ABS", ("EV_ABS", 0x01) returns "ABS_Y"
+ Unknown event types or type/code combinations return None.
If an event code is passed, the event type may be given as integer or
string.
"""
- return evemu.event_names._event_get_name(event_type, event_code)
+ try:
+ return evemu.event_names._event_get_name(event_type, event_code)
+ except KeyError:
+ return None
class InputEvent(object):
__slots__ = 'sec', 'usec', 'type', 'code', 'value'
diff --git a/python/evemu/tests/test_device.py b/python/evemu/tests/test_device.py
index 2873758..78fc7c1 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -276,6 +276,26 @@ class DevicePropertiesTestCase(evemu.testing.testcase.BaseTestCase):
self.assertTrue(e.matches("EV_ABS"))
self.assertTrue(e.matches("EV_ABS", "ABS_X"))
+ def test_event_names(self):
+ self.assertEqual(evemu.event_get_value("EV_SYN"), 0x00)
+ self.assertEqual(evemu.event_get_value("EV_KEY"), 0x01)
+ self.assertEqual(evemu.event_get_value("EV_ABS"), 0x03)
+ self.assertEqual(evemu.event_get_value("EV_FOO"), None)
+
+ self.assertEqual(evemu.event_get_value("EV_SYN", "SYN_REPORT"), 0x00)
+ self.assertEqual(evemu.event_get_value("EV_KEY", "KEY_Z"), 44)
+ self.assertEqual(evemu.event_get_value("EV_ABS", "ABS_X"), 0x00)
+ self.assertEqual(evemu.event_get_value("EV_ABS", "ABS_FOO"), None)
+
+ self.assertEqual(evemu.event_get_name(0x00), "EV_SYN")
+ self.assertEqual(evemu.event_get_name(0x01), "EV_KEY")
+ self.assertEqual(evemu.event_get_name(0x03), "EV_ABS")
+ self.assertEqual(evemu.event_get_name(0xFFFF), None)
+
+ self.assertEqual(evemu.event_get_name("EV_SYN", 0x00), "SYN_REPORT")
+ self.assertEqual(evemu.event_get_name("EV_REL", 0x01), "REL_Y")
+ self.assertEqual(evemu.event_get_name("EV_ABS", 0x00), "ABS_X")
+ self.assertEqual(evemu.event_get_name("EV_ABS", 0xFFFF), None)
if __name__ == "__main__":
unittest.main()
--
1.9.3
More information about the Input-tools
mailing list