[PATCH evemu v2 06/10] make-event-names: replace get_code/type_name functions with a unified set

Peter Hutterer peter.hutterer at who-t.net
Thu Aug 7 22:05:29 PDT 2014


Python has default arguments, so we can have a function that does the right
thing depending on the arguments passed in. And have two functions that
explicitly fetch the name, or value, for an event.

So now we can do things like
  t = event_get_value("EV_ABS")
  c = event_get_value("EV_ABS", "ABS_X")
  t = event_get_name(0x02)
  c = event_get_name(0x02, 0x03)
  c = event_get_name("EV_ABS", 0x03)

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
Changes to v1:
- explicitly compare event_code to None, otherwise 0-values will be
  considered false too

 src/make-event-names.py | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/make-event-names.py b/src/make-event-names.py
index 5fef9a6..e1a4f3d 100755
--- a/src/make-event-names.py
+++ b/src/make-event-names.py
@@ -101,15 +101,33 @@ def print_python_mapping_table(bits):
 	print_python_map(bits)
 
 	p("""
-	def event_get_type_name(type):
-		return ev_map[type]
+	def event_get_name(event_type, event_code = None):
+		'''
+		Returns the event type or code name.
+		'''
+		if event_code == None:
+			if type(event_type) != int:
+				raise TypeError("expected an int")
+			return ev_map[event_type]
 
+		if type(event_code) != int:
+			raise TypeError("expected an int")
 
-	def event_get_code_name(type, code):
-		try:
-			return map[type][code]
-		except KeyError:
-			return 'UNKNOWN'
+		return map[event_type][event_code]
+
+	def event_get_value(event_type, event_code = None):
+		'''
+		Returns the event type or code value.
+		'''
+		if event_code == None:
+			if type(event_type) != str:
+				raise TypeError("expected a string")
+			return ev_map[event_type]
+
+		if type(event_code) != str:
+			raise TypeError("expected a string")
+
+		return map[event_type][event_code]
 	""")
 
 def parse_define(bits, line):
-- 
1.9.3



More information about the Input-tools mailing list