bindings: Fix enums.py compatibility with Python 3.

This commit is contained in:
Martin Ling 2014-11-13 20:38:56 +00:00
parent 624d16100e
commit d2a929ab85
1 changed files with 30 additions and 26 deletions

View File

@ -17,6 +17,7 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
## ##
from __future__ import print_function
from xml.etree import ElementTree from xml.etree import ElementTree
from collections import OrderedDict from collections import OrderedDict
import sys, os, re import sys, os, re
@ -69,7 +70,7 @@ code = open(os.path.join(outdirname, 'enums.cpp'), 'w')
swig = open(os.path.join(outdirname, '../swig/enums.i'), 'w') swig = open(os.path.join(outdirname, '../swig/enums.i'), 'w')
for file in (header, code): for file in (header, code):
print >> file, "/* Generated file - edit enums.py instead! */" print("/* Generated file - edit enums.py instead! */", file=file)
# Template for beginning of class declaration and public members. # Template for beginning of class declaration and public members.
header_public_template = """ header_public_template = """
@ -98,66 +99,69 @@ for enum, (classname, classbrief) in classes.items():
briefs = [get_text(m.find('briefdescription')) for m in members] briefs = [get_text(m.find('briefdescription')) for m in members]
# Begin class and public declarations # Begin class and public declarations
print >> header, header_public_template.format( print(header_public_template.format(
brief=classbrief, classname=classname, enumname=enum_name) brief=classbrief, classname=classname, enumname=enum_name), file=header)
# Declare public pointers for each enum value # Declare public pointers for each enum value
for trimmed_name, brief in zip(trimmed_names, briefs): for trimmed_name, brief in zip(trimmed_names, briefs):
if brief: if brief:
print >> header, '\t/** %s */' % brief print('\t/** %s */' % brief, file=header)
print >> header, '\tstatic const %s * const %s;' % ( print('\tstatic const %s * const %s;' % (
classname, trimmed_name) classname, trimmed_name), file=header)
# Declare additional methods if present # Declare additional methods if present
filename = os.path.join(dirname, "%s_methods.hpp" % classname) filename = os.path.join(dirname, "%s_methods.hpp" % classname)
if os.path.exists(filename): if os.path.exists(filename):
print >> header, str.join('', open(filename).readlines()) print(str.join('', open(filename).readlines()), file=header)
# Begin private declarations # Begin private declarations
print >> header, header_private_template.format( print(header_private_template.format(
classname=classname, enumname=enum_name) classname=classname, enumname=enum_name), file=header)
# Declare private constants for each enum value # Declare private constants for each enum value
for trimmed_name in trimmed_names: for trimmed_name in trimmed_names:
print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name) print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header)
# End class declaration # End class declaration
print >> header, '};' print('};', file=header)
# Define private constants for each enum value # Define private constants for each enum value
for name, trimmed_name in zip(member_names, trimmed_names): for name, trimmed_name in zip(member_names, trimmed_names):
print >> code, 'const %s %s::_%s = %s(%s, "%s");' % ( print('const %s %s::_%s = %s(%s, "%s");' % (
classname, classname, trimmed_name, classname, name, trimmed_name) classname, classname, trimmed_name, classname, name, trimmed_name),
file=code)
# Define public pointers for each enum value # Define public pointers for each enum value
for trimmed_name in trimmed_names: for trimmed_name in trimmed_names:
print >> code, 'const %s * const %s::%s = &%s::_%s;' % ( print('const %s * const %s::%s = &%s::_%s;' % (
classname, classname, trimmed_name, classname, trimmed_name) classname, classname, trimmed_name, classname, trimmed_name),
file=code)
# Define map of enum values to constants # Define map of enum values to constants
print >> code, 'template<> const std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % ( print('template<> const std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % (
enum_name, classname, classname, enum_name) enum_name, classname, classname, enum_name), file=code)
for name, trimmed_name in zip(member_names, trimmed_names): for name, trimmed_name in zip(member_names, trimmed_names):
print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name) print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code)
print >> code, '};' print('};', file=code)
# Define additional methods if present # Define additional methods if present
filename = os.path.join(dirname, "%s_methods.cpp" % classname) filename = os.path.join(dirname, "%s_methods.cpp" % classname)
if os.path.exists(filename): if os.path.exists(filename):
print >> code, str.join('', open(filename).readlines()) print(str.join('', open(filename).readlines()), file=code)
# Map EnumValue::id() and EnumValue::name() as SWIG attributes. # Map EnumValue::id() and EnumValue::name() as SWIG attributes.
print >> swig, '%%attribute(sigrok::%s, int, id, id);' % classname print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig)
print >> swig, '%%attributestring(sigrok::%s, std::string, name, name);' % classname print('%%attributestring(sigrok::%s, std::string, name, name);' % classname,
file=swig)
# Instantiate EnumValue template for SWIG # Instantiate EnumValue template for SWIG
print >> swig, '%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % ( print('%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % (
classname, classname, enum_name) classname, classname, enum_name), file=swig)
# Apply any language-specific extras. # Apply any language-specific extras.
print >> swig, '%%enumextras(%s);' % classname print('%%enumextras(%s);' % classname, file=swig)
# Declare additional attributes if present # Declare additional attributes if present
filename = os.path.join(dirname, "%s_methods.i" % classname) filename = os.path.join(dirname, "%s_methods.i" % classname)
if os.path.exists(filename): if os.path.exists(filename):
print >> swig, str.join('', open(filename).readlines()) print(str.join('', open(filename).readlines()), file=swig)