Skip to content

Commit 0e55886

Browse files
authored
QPID-8413: Python 3 support in managementgen (#24)
1 parent 039ba9e commit 0e55886

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

managementgen/qmf-gen

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# under the License.
1919
#
2020

21+
from __future__ import print_function
22+
2123
import sys
2224
import os
2325
from qmfgen.schema import SchemaPackage, SchemaClass
@@ -61,7 +63,7 @@ v2_style = opts.v2_style
6163
gen = Generator(outdir, templatedir)
6264

6365
if len(args) == 0:
64-
print "no input files"
66+
print("no input files")
6567
parser.exit()
6668

6769
vargs = {}

managementgen/qmfgen/generate.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
1717
# under the License.
1818
#
1919

20+
from __future__ import print_function
21+
2022
from xml.dom.minidom import parse, parseString, Node
21-
from cStringIO import StringIO
2223
from stat import *
2324
from errno import *
2425
import os
2526
import os.path
2627
import filecmp
2728
import re
2829

30+
try:
31+
from cStringIO import StringIO
32+
except ImportError:
33+
from io import StringIO
34+
35+
2936
class Template:
3037
"""
3138
Expandable File Template - This class is instantiated each time a
@@ -267,8 +274,8 @@ def createPath (self, path):
267274
exists = True
268275
try:
269276
mode = os.stat (path)[ST_MODE]
270-
except OSError, (err,text):
271-
if err == ENOENT or err == ESRCH:
277+
except OSError as e:
278+
if e.errno == ENOENT or e.errno == ESRCH:
272279
exists = False
273280
else:
274281
raise
@@ -354,7 +361,7 @@ def writeIfChanged (self, stream, target, force=False):
354361
os.rename (tempFile, target)
355362

356363
if self.verbose:
357-
print "Generated:", target
364+
print("Generated:", target)
358365

359366
def targetPackageFile (self, schema, templateFile):
360367
dot = templateFile.find(".")

managementgen/qmfgen/schema.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
#
1919

2020
from xml.dom.minidom import parse, parseString, Node
21-
from cStringIO import StringIO
22-
#import md5
21+
22+
try:
23+
from cStringIO import StringIO
24+
except ImportError:
25+
from io import StringIO
26+
2327
try:
2428
import hashlib
2529
_md5Obj = hashlib.md5
@@ -38,15 +42,16 @@ def addSubHash(self, hash):
3842
self.md5Sum.update(hash.getDigest())
3943

4044
def getDigest(self):
41-
return self.md5Sum.digest()
45+
# type: () -> bytes
46+
return bytes(self.md5Sum.digest())
4247

4348
def _compute(self, node):
4449
attrs = node.attributes
45-
self.md5Sum.update(node.nodeName)
50+
self.md5Sum.update(node.nodeName.encode())
4651

4752
for idx in range(attrs.length):
48-
self.md5Sum.update(attrs.item(idx).nodeName)
49-
self.md5Sum.update(attrs.item(idx).nodeValue)
53+
self.md5Sum.update(attrs.item(idx).nodeName.encode())
54+
self.md5Sum.update(attrs.item(idx).nodeValue.encode())
5055

5156
for child in node.childNodes:
5257
if child.nodeType == Node.ELEMENT_NODE:
@@ -1064,10 +1069,12 @@ def genArgSchemaMap(self, stream, variables):
10641069

10651070
def genSchemaMD5(self, stream, variables):
10661071
sum = self.hash.getDigest()
1067-
for idx in range (len (sum)):
1072+
for idx, val in enumerate(sum):
10681073
if idx != 0:
1069-
stream.write (",")
1070-
stream.write (hex (ord (sum[idx])))
1074+
stream.write(",")
1075+
if isinstance(val, str):
1076+
val = ord(val) # Python 2
1077+
stream.write(hex(val))
10711078

10721079

10731080
class SchemaClass:
@@ -1443,13 +1450,13 @@ def genPresenceMaskBytes (self, stream, variables):
14431450
if count == 0:
14441451
stream.write("0")
14451452
else:
1446-
stream.write (str(((count - 1) / 8) + 1))
1453+
stream.write (str(((count - 1) // 8) + 1))
14471454

14481455
def genPresenceMaskConstants (self, stream, variables):
14491456
count = 0
14501457
for prop in self.properties:
14511458
if prop.isOptional == 1:
1452-
stream.write(" static const uint8_t presenceByte_%s = %d;\n" % (prop.name, count / 8))
1459+
stream.write(" static const uint8_t presenceByte_%s = %d;\n" % (prop.name, count // 8))
14531460
stream.write(" static const uint8_t presenceMask_%s = %d;\n" % (prop.name, 1 << (count % 8)))
14541461
count += 1
14551462

@@ -1527,12 +1534,14 @@ def genParentRefAssignment (self, stream, variables):
15271534
" = _parent->GetManagementObject()->getObjectId();")
15281535
return
15291536

1530-
def genSchemaMD5 (self, stream, variables):
1537+
def genSchemaMD5(self, stream, variables):
15311538
sum = self.hash.getDigest()
1532-
for idx in range (len (sum)):
1539+
for idx, val in enumerate(sum):
15331540
if idx != 0:
1534-
stream.write (",")
1535-
stream.write (hex (ord (sum[idx])))
1541+
stream.write(",")
1542+
if isinstance(val, str):
1543+
val = ord(val) # Python 2
1544+
stream.write(hex(val))
15361545

15371546
def genAssign (self, stream, variables):
15381547
for inst in self.statistics:

0 commit comments

Comments
 (0)