[PATCH libevdev] Change python script to be python 2 and 3 compatible

Peter Hutterer peter.hutterer at who-t.net
Wed Aug 21 21:54:06 PDT 2013


Incompatibility here was the changed print function, replaced with
str.format(). Not quite as nice to read, but now we don't have to worry
about the python version.

str.format() requires Python 2.6 which is reasonable enough given that
even RHEL6 ships that.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 configure.ac                 |   1 +
 libevdev/Makefile.am         |   2 +-
 libevdev/make-event-names.py | 102 ++++++++++++++++++++++---------------------
 3 files changed, 54 insertions(+), 51 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/Makefile.am b/libevdev/Makefile.am
index 9879c0e..fc8f7eb 100644
--- a/libevdev/Makefile.am
+++ b/libevdev/Makefile.am
@@ -14,7 +14,7 @@ libevdevincludedir = $(includedir)/libevdev-1.0/libevdev
 libevdevinclude_HEADERS = libevdev.h
 
 event-names.h: Makefile make-event-names.py
-	        $(srcdir)/make-event-names.py --output=c > $@
+	        $(PYTHON) $(srcdir)/make-event-names.py --output=c > $@
 
 EXTRA_DIST = make-event-names.py
 CLEANFILES = event-names.h
diff --git a/libevdev/make-event-names.py b/libevdev/make-event-names.py
index cf56fd9..62851e3 100755
--- a/libevdev/make-event-names.py
+++ b/libevdev/make-event-names.py
@@ -43,96 +43,98 @@ 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()
+	str = "static const char * const {}_map[{}_MAX + 1] = {{\n".format(prefix, prefix.upper())
+	str += "	[0 ... {}_MAX] = NULL,\n".format(prefix.upper())
 	for val, name in getattr(bits, prefix).items():
-		print "	[%s] = \"%s\"," % (name, name)
-	print "};"
-	print ""
+		str += "	[{}] = \"{}\",\n".format(name, name)
+	str += "};\n"
+	return str
 
 def print_python_bits(bits, prefix):
 	if  not hasattr(bits, prefix):
 		return
 
-	print "%s_map = {" % (prefix)
+	str = "{}_map = {{\n".format(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 ""
+		str += "	{} : \"{}\",\n".format(val, name)
+	str += "}\n"
+	str += "for k, v in {}_map.items():\n".format(prefix)
+	str += "	{}_map[v] = k\n".format(prefix)
+	str += "\n"
+	return str
 
 def print_map(bits):
-	print "static const char * const * const event_type_map[EV_MAX + 1] = {"
-	print "	[0 ... EV_MAX] = NULL,"
+	str = "static const char * const * const event_type_map[EV_MAX + 1] = {\n"
+	str += "	[0 ... EV_MAX] = NULL,\n"
 
 	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())
+		str += "	[EV_{}] = {}_map,\n".format(prefix[:-1], prefix[:-1].lower())
 
-	print "};"
-	print ""
+	str += "};\n"
 
-	print "static const int ev_max[EV_MAX + 1] = {"
-	print "	[0 ... EV_MAX] = -1,"
+	str += "static const int ev_max[EV_MAX + 1] = {\n"
+	str += "	[0 ... EV_MAX] = -1,\n"
 	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 ""
+		str += "	[EV_{}] = {}_MAX,\n".format(prefix[:-1], prefix[:-1])
+	str += "};\n"
+	return str
 
 def print_python_map(bits):
-	print "map = {"
+	str = "map = {\n"
 
 	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())
+		str += "	{} : {}_map,\n".format(val, name.lower())
 
-	print "}"
-	print ""
+	str += "}\n"
+	return str
 
 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 ""
+	str = "/* THIS FILE IS GENERATED, DO NOT EDIT */"
+	str += "\n"
+	str += "#ifndef EVENT_NAMES_H\n"
+	str += "#define EVENT_NAMES_H\n"
+	str += "\n"
+	str += "#define SYN_MAX 3 /* linux/input.h doesn't define that */\n"
+	str += "\n"
 
 	for prefix in prefixes:
 		if prefix == "BTN_":
 			continue
-		print_bits(bits, prefix[:-1].lower())
+		str += print_bits(bits, prefix[:-1].lower())
 
-	print_map(bits)
+	str += print_map(bits)
 
-	print "#endif /* EVENT_NAMES_H */"
+	str += "#endif /* EVENT_NAMES_H */"
+	return str
 
 def print_python_mapping_table(bits):
-	print "# THIS FILE IS GENERATED, DO NOT EDIT"
-	print ""
+	str = "# THIS FILE IS GENERATED, DO NOT EDIT\n"
+	str += "\n"
 
 	for prefix in prefixes:
 		if prefix == "BTN_":
 			continue
-		print_python_bits(bits, prefix[:-1].lower())
+		str += print_python_bits(bits, prefix[:-1].lower())
 
-	print_python_map(bits)
+	str += 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 ""
+	str += "def event_get_type_name(type):\n"
+	str += "	return ev_map[type]\n"
+	str += "\n"
+	str += "\n"
+	str += "def event_get_code_name(type, code):\n"
+	str += "	if map.has_key(type) and map[type].has_key(code):\n"
+	str += "		return map[type][code]\n"
+	str += "	return 'UNKNOWN'\n"
+	str += "\n"
+	return str
 
 def parse_define(bits, line):
 	m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
@@ -182,6 +184,6 @@ if __name__ == "__main__":
 
 	args = parser.parse_args(sys.argv[1:])
 	if args.output == "python":
-		print_python_mapping_table(bits)
+		print(print_python_mapping_table(bits))
 	else:
-		print_mapping_table(bits)
+		print(print_mapping_table(bits))
-- 
1.8.2.1



More information about the Input-tools mailing list