Skip to content

Commit 9a2cefb

Browse files
authored
Merge pull request #2 from OpenSimulationInterface/Python2/3-compatibility
Add support for python2
2 parents c0b37bf + 57e1d32 commit 9a2cefb

1 file changed

Lines changed: 39 additions & 16 deletions

File tree

proto2cpp.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,48 @@ def handleFile(self, fileName):
9090
if fnmatch.fnmatch(filename, '*.proto'):
9191
self.log('\nXXXXXXXXXX\nXX ' + filename + '\nXXXXXXXXXX\n\n')
9292
# Open the file. Use try to detect whether or not we have an actual file.
93-
try:
94-
with open(filename, 'r', encoding='utf8') as inputFile:
95-
self.parseFile(inputFile)
96-
pass
97-
except IOError as e:
98-
self.logError('the file ' + filename + ' could not be opened for reading')
93+
if (sys.version_info > (3, 0)):
94+
try:
95+
with open(filename, 'r', encoding='utf8') as inputFile:
96+
self.parseFile(inputFile)
97+
pass
98+
except IOError as e:
99+
self.logError('the file ' + filename + ' could not be opened for reading')
100+
else:
101+
# Python 2 code in this block
102+
try:
103+
with open(filename, 'r') as inputFile:
104+
self.parseFile(inputFile)
105+
pass
106+
except IOError as e:
107+
self.logError('the file ' + filename + ' could not be opened for reading')
99108

100109
elif not fnmatch.fnmatch(filename, os.path.basename(inspect.getfile(inspect.currentframe()))):
101110
self.log('\nXXXXXXXXXX\nXX ' + filename + '\nXXXXXXXXXX\n\n')
102-
try:
103-
with open(filename, 'r', encoding='utf8') as theFile:
104-
output = ''
105-
for theLine in theFile:
106-
output += theLine
107-
print(output)
108-
self.log(output)
109-
pass
110-
except IOError as e:
111-
self.logError('the file ' + filename + ' could not be opened for reading')
111+
if (sys.version_info > (3, 0)):
112+
try:
113+
with open(filename, 'r', encoding='utf8') as theFile:
114+
output = ''
115+
for theLine in theFile:
116+
output += theLine
117+
print(output)
118+
self.log(output)
119+
pass
120+
except IOError as e:
121+
self.logError('the file ' + filename + ' could not be opened for reading')
122+
else:
123+
# Python 2 code in this block
124+
try:
125+
with open(filename, 'r') as theFile:
126+
output = ''
127+
for theLine in theFile:
128+
output += theLine
129+
print(output)
130+
self.log(output)
131+
pass
132+
except IOError as e:
133+
self.logError('the file ' + filename + ' could not be opened for reading')
134+
112135
else:
113136
self.log('\nXXXXXXXXXX\nXX ' + filename + ' --skipped--\nXXXXXXXXXX\n\n')
114137

0 commit comments

Comments
 (0)