[PATCH v2 libevdev] Change event name generate script to be python 2/3 compatible

Peter Hutterer peter.hutterer at who-t.net
Sun Aug 25 16:15:47 PDT 2013


Python 3 doesn't have a print statement, only a print function. Replace
all calls in the form
   print "blah"
with
   print("blah")

regex:
  s/print "(.*)/print("\1/

Print as function requires Python 2.6 which is reasonable enough given
that even RHEL6 ships that.

Even though it's not needed for 2.6, use
	from __future__ import print_function
to avoid accidentally introducing a print statement in the future.
With this line, print "blah" is now a syntax error in python 2.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
Changes to v1:
- don't use str.format, simply adding () around the print statement is
  enough
- use from future... as indicated in commit message

 configure.ac                 |  1 +
 libevdev/make-event-names.py | 89 ++++++++++++++++++++++----------------------
 2 files changed, 46 insertions(+), 44 deletions(-)

diff --git a/configure.ac b/configure.ac
index 68df538..1195e54 100644
--- a/configure.ac
+++ b/configure.ac
@@ -27,6 +27,7 @@ AM_SILENT_RULES([yes])
 
 # Check for programs
 AC_PROG_CC
+AM_PATH_PYTHON([2.6])
 
 # Initialize libtool
 LT_PREREQ([2.2])
diff --git a/libevdev/make-event-names.py b/libevdev/make-event-names.py
index cf56fd9..d946dc2 100755
--- a/libevdev/make-event-names.py
+++ b/libevdev/make-event-names.py
@@ -4,6 +4,7 @@
 # mapping table
 #
 
+from __future__ import print_function
 import re
 import sys
 import argparse
@@ -43,66 +44,66 @@ blacklist = [
 def print_bits(bits, prefix):
 	if  not hasattr(bits, prefix):
 		return
-	print "static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper())
-	print "	[0 ... %s_MAX] = NULL," % prefix.upper()
+	print("static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper()))
+	print("	[0 ... %s_MAX] = NULL," % prefix.upper())
 	for val, name in getattr(bits, prefix).items():
-		print "	[%s] = \"%s\"," % (name, name)
-	print "};"
-	print ""
+		print("	[%s] = \"%s\"," % (name, name))
+	print("};")
+	print("")
 
 def print_python_bits(bits, prefix):
 	if  not hasattr(bits, prefix):
 		return
 
-	print "%s_map = {" % (prefix)
+	print("%s_map = {" % (prefix))
 	for val, name in getattr(bits, prefix).items():
-		print "	%d : \"%s\"," % (val, name)
-	print "}"
-	print "for k, v in %s_map.items():" % (prefix)
-	print "	%s_map[v] = k" % (prefix)
-	print ""
+		print("	%d : \"%s\"," % (val, name))
+	print("}")
+	print("for k, v in %s_map.items():" % (prefix))
+	print("	%s_map[v] = k" % (prefix))
+	print("")
 
 def print_map(bits):
-	print "static const char * const * const event_type_map[EV_MAX + 1] = {"
-	print "	[0 ... EV_MAX] = NULL,"
+	print("static const char * const * const event_type_map[EV_MAX + 1] = {")
+	print("	[0 ... EV_MAX] = NULL,")
 
 	for prefix in prefixes:
 		if prefix == "BTN_" or prefix == "EV_" or prefix == "INPUT_PROP_":
 			continue
-		print "	[EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower())
+		print("	[EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower()))
 
-	print "};"
-	print ""
+	print("};")
+	print("")
 
-	print "static const int ev_max[EV_MAX + 1] = {"
-	print "	[0 ... EV_MAX] = -1,"
+	print("static const int ev_max[EV_MAX + 1] = {")
+	print("	[0 ... EV_MAX] = -1,")
 	for prefix in prefixes:
 		if prefix == "BTN_" or prefix == "EV_" or prefix == "INPUT_PROP_":
 			continue
-		print "	[EV_%s] = %s_MAX," % (prefix[:-1], prefix[:-1])
-	print "};"
-	print ""
+		print("	[EV_%s] = %s_MAX," % (prefix[:-1], prefix[:-1]))
+	print("};")
+	print("")
 
 def print_python_map(bits):
-	print "map = {"
+	print("map = {")
 
 	for val, name in getattr(bits, "ev").items():
 		name = name[3:]
 		if name == "REP" or name == "PWR"  or name == "FF_STATUS"  or name == "MAX":
 			continue
-		print "	%d : %s_map," % (val, name.lower())
+		print("	%d : %s_map," % (val, name.lower()))
 
-	print "}"
-	print ""
+	print("}")
+	print("")
 
 def print_mapping_table(bits):
-	print "/* THIS FILE IS GENERATED, DO NOT EDIT */"
-	print ""
-	print "#ifndef EVENT_NAMES_H"
-	print "#define EVENT_NAMES_H"
-	print ""
-	print "#define SYN_MAX 3 /* linux/input.h doesn't define that */"
-	print ""
+	print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
+	print("")
+	print("#ifndef EVENT_NAMES_H")
+	print("#define EVENT_NAMES_H")
+	print("")
+	print("#define SYN_MAX 3 /* linux/input.h doesn't define that */")
+	print("")
 
 	for prefix in prefixes:
 		if prefix == "BTN_":
@@ -111,11 +112,11 @@ def print_mapping_table(bits):
 
 	print_map(bits)
 
-	print "#endif /* EVENT_NAMES_H */"
+	print("#endif /* EVENT_NAMES_H */")
 
 def print_python_mapping_table(bits):
-	print "# THIS FILE IS GENERATED, DO NOT EDIT"
-	print ""
+	print("# THIS FILE IS GENERATED, DO NOT EDIT")
+	print("")
 
 	for prefix in prefixes:
 		if prefix == "BTN_":
@@ -124,15 +125,15 @@ def print_python_mapping_table(bits):
 
 	print_python_map(bits)
 
-	print "def event_get_type_name(type):"
-	print "	return ev_map[type]"
-	print ""
-	print ""
-	print "def event_get_code_name(type, code):"
-	print "	if map.has_key(type) and map[type].has_key(code):"
-	print "		return map[type][code]"
-	print "	return 'UNKNOWN'"
-	print ""
+	print("def event_get_type_name(type):")
+	print("	return ev_map[type]")
+	print("")
+	print("")
+	print("def event_get_code_name(type, code):")
+	print("	if map.has_key(type) and map[type].has_key(code):")
+	print("		return map[type][code]")
+	print("	return 'UNKNOWN'")
+	print("")
 
 def parse_define(bits, line):
 	m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
-- 
1.8.2.1



More information about the Input-tools mailing list