diff options
author | Olivier Galibert <galibert@pobox.com> | 2015-07-30 11:44:44 +0200 |
---|---|---|
committer | Olivier Galibert <galibert@pobox.com> | 2015-07-30 11:44:43 +0200 |
commit | 4b1e08c1bb282af1f3b09d992bf006a58908eee6 (patch) | |
tree | 4e38421b8aa166abc1d2f674743af7874ff238b7 /src/build | |
parent | 93bd0f0daf4bce350dc66071799edb38812a8d2f (diff) |
python: Make all scripts python 2/3 compatible
Diffstat (limited to 'src/build')
-rw-r--r-- | src/build/makelist.py | 2 | ||||
-rw-r--r-- | src/build/png.py | 27 | ||||
-rw-r--r-- | src/build/png2bdc.py | 48 |
3 files changed, 42 insertions, 35 deletions
diff --git a/src/build/makelist.py b/src/build/makelist.py index 1f94814bf88..144dc636c8d 100644 --- a/src/build/makelist.py +++ b/src/build/makelist.py @@ -11,7 +11,7 @@ drivlist = [] def parse_file(srcfile): try: - fp = open(srcfile, 'rb') + fp = open(srcfile, 'rt') except IOError: sys.stderr.write("Unable to open source file '%s'\n" % srcfile) return 1 diff --git a/src/build/png.py b/src/build/png.py index f096a1dbdf5..8d504ec8ea0 100644 --- a/src/build/png.py +++ b/src/build/png.py @@ -143,15 +143,16 @@ And now, my famous members """ # http://www.python.org/doc/2.2.3/whatsnew/node5.html -from __future__ import generators +from __future__ import generators,print_function __version__ = "0.0.17" from array import array -try: # See :pyver:old - import itertools +try: + from itertools import imap except ImportError: - pass + imap = map +from itertools import starmap import math # http://www.python.org/doc/2.4.4/lib/module-operator.html import operator @@ -1650,11 +1651,10 @@ class Reader: spb = 8//self.bitdepth out = array('B') mask = 2**self.bitdepth - 1 - shifts = map(self.bitdepth.__mul__, reversed(range(spb))) + shifts = list(map(self.bitdepth.__mul__, reversed(range(spb)))) for o in raw: - out.extend(map(lambda i: mask&(o>>i), shifts)) + out.extend(list(map(lambda i: mask&(o>>i), shifts))) return out[:width] - return itertools.imap(asvalues, rows) def serialtoflat(self, bytes, width=None): @@ -1915,8 +1915,8 @@ class Reader: while True: try: type, data = self.chunk(lenient=lenient) - except ValueError, e: - raise ChunkError(e.args[0]) + except ValueError: + raise ChunkError(sys.exc_info()[1].args[0]) if type == 'IEND': # http://www.w3.org/TR/PNG/#11IEND break @@ -1947,7 +1947,6 @@ class Reader: self.preamble(lenient=lenient) raw = iterdecomp(iteridat()) - if self.interlace: raw = array('B', itertools.chain(*raw)) arraycode = 'BH'[self.bitdepth>8] @@ -2062,10 +2061,10 @@ class Reader: meta['alpha'] = bool(self.trns) meta['bitdepth'] = 8 meta['planes'] = 3 + bool(self.trns) - plte = self.palette() + plte = list(self.palette()) def iterpal(pixels): for row in pixels: - row = map(plte.__getitem__, row) + row = list(map(plte.__getitem__, row)) yield array('B', itertools.chain(*row)) pixels = iterpal(pixels) elif self.trns: @@ -2779,5 +2778,5 @@ def _main(argv): if __name__ == '__main__': try: _main(sys.argv) - except Error, e: - print >>sys.stderr, e + except Error: + print (sys.exc_info()[1], file=e) diff --git a/src/build/png2bdc.py b/src/build/png2bdc.py index 58a5983ce97..4ffeb4c01ab 100644 --- a/src/build/png2bdc.py +++ b/src/build/png2bdc.py @@ -59,6 +59,13 @@ import os import png import sys +if sys.version_info >= (3,): + def b2p(v): + return bytes([v]) +else: + def b2p(v): + return chr(v) + ######################################## ## Helper classes @@ -117,27 +124,27 @@ def renderFontSaveCached(font, filename, hash32): CACHED_HEADER_SIZE = 16 try: - fp.write('f') - fp.write('o') - fp.write('n') - fp.write('t') - fp.write(chr(hash32 >> 24 & 0xff)) - fp.write(chr(hash32 >> 16 & 0xff)) - fp.write(chr(hash32 >> 8 & 0xff)) - fp.write(chr(hash32 >> 0 & 0xff)) - fp.write(chr(font.height >> 8 & 0xff)) - fp.write(chr(font.height >> 0 & 0xff)) - fp.write(chr(font.yOffs >> 8 & 0xff)) - fp.write(chr(font.yOffs >> 0 & 0xff)) - fp.write(chr(numChars >> 24 & 0xff)) - fp.write(chr(numChars >> 16 & 0xff)) - fp.write(chr(numChars >> 8 & 0xff)) - fp.write(chr(numChars >> 0 & 0xff)) + fp.write(b'f') + fp.write(b'o') + fp.write(b'n') + fp.write(b't') + fp.write(b2p(hash32 >> 24 & 0xff)) + fp.write(b2p(hash32 >> 16 & 0xff)) + fp.write(b2p(hash32 >> 8 & 0xff)) + fp.write(b2p(hash32 >> 0 & 0xff)) + fp.write(b2p(font.height >> 8 & 0xff)) + fp.write(b2p(font.height >> 0 & 0xff)) + fp.write(b2p(font.yOffs >> 8 & 0xff)) + fp.write(b2p(font.yOffs >> 0 & 0xff)) + fp.write(b2p(numChars >> 24 & 0xff)) + fp.write(b2p(numChars >> 16 & 0xff)) + fp.write(b2p(numChars >> 8 & 0xff)) + fp.write(b2p(numChars >> 0 & 0xff)) # Write a blank table at first (?) charTable = [0]*(numChars * CACHED_CHAR_SIZE) for i in range(numChars * CACHED_CHAR_SIZE): - fp.write(chr(charTable[i])) + fp.write(b2p(charTable[i])) # Loop over all characters tableIndex = 0 @@ -173,7 +180,7 @@ def renderFontSaveCached(font, filename, hash32): # Write the data for j in range(len(dBuffer)): - fp.write(chr(dBuffer[j])) + fp.write(b2p(dBuffer[j])) destIndex = tableIndex * CACHED_CHAR_SIZE charTable[destIndex + 0] = i >> 8 & 0xff @@ -193,12 +200,13 @@ def renderFontSaveCached(font, filename, hash32): # Seek back to the beginning and rewrite the table fp.seek(CACHED_HEADER_SIZE, 0) for i in range(numChars * CACHED_CHAR_SIZE): - fp.write(chr(charTable[i])) + fp.write(b2p(charTable[i])) fp.close() return 0 except: + print(sys.exc_info[1]) return 1 @@ -347,7 +355,7 @@ def main(): except: sys.stderr.write("Error reading PNG file.\n") return 1 - + error = bitmapToChars(pngObject, font) if error: return 1 |