add Python 3 support

main
josch 9 years ago
parent d217f1403f
commit b143867a72

@ -16,6 +16,8 @@ setup (
'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: CPython',
'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)', 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'Programming Language :: Python', 'Programming Language :: Python',

@ -38,17 +38,19 @@ def warning_out(message):
def parse(cont, indent=1): def parse(cont, indent=1):
if type(cont) is dict: if type(cont) is dict:
return "<<\n"+"\n".join( return b"<<\n"+b"\n".join(
[4 * indent * " " + "%s %s" % (k, parse(v, indent+1)) [4 * indent * b" " + k.encode("utf8") + b" " + parse(v, indent+1)
for k, v in cont.items()])+"\n"+4*(indent-1)*" "+">>" for k, v in cont.items()])+b"\n"+4*(indent-1)*b" "+b">>"
elif type(cont) is int or type(cont) is float: elif type(cont) is int or type(cont) is float:
return str(cont) return str(cont).encode("utf8")
elif isinstance(cont, obj): elif isinstance(cont, obj):
return "%d 0 R"%cont.identifier return ("%d 0 R"%cont.identifier).encode("utf8")
elif type(cont) is str: elif type(cont) is str:
return cont.encode("utf8")
elif type(cont) is bytes:
return cont return cont
elif type(cont) is list: elif type(cont) is list:
return "[ "+" ".join([parse(c, indent) for c in cont])+" ]" return b"[ "+b" ".join([parse(c, indent) for c in cont])+b" ]"
class obj(object): class obj(object):
def __init__(self, content, stream=None): def __init__(self, content, stream=None):
@ -58,11 +60,11 @@ class obj(object):
def tostring(self): def tostring(self):
if self.stream: if self.stream:
return ( return (
"%d 0 obj " % self.identifier + ("%d 0 obj " % self.identifier).encode("utf8") +
parse(self.content) + parse(self.content) +
"\nstream\n" + self.stream + "\nendstream\nendobj\n") b"\nstream\n" + self.stream + b"\nendstream\nendobj\n")
else: else:
return "%d 0 obj "%self.identifier+parse(self.content)+" endobj\n" return ("%d 0 obj "%self.identifier).encode("utf8")+parse(self.content)+b" endobj\n"
class pdfdoc(object): class pdfdoc(object):
@ -149,7 +151,7 @@ class pdfdoc(object):
"/Length": len(imgdata) "/Length": len(imgdata)
}, imgdata) }, imgdata)
text = "q\n%f 0 0 %f 0 0 cm\n/Im0 Do\nQ"%(pdf_x, pdf_y) text = ("q\n%f 0 0 %f 0 0 cm\n/Im0 Do\nQ"%(pdf_x, pdf_y)).encode('utf8')
content = obj({ content = obj({
"/Length": len(text) "/Length": len(text)
@ -178,23 +180,23 @@ class pdfdoc(object):
xreftable = list() xreftable = list()
result = "%%PDF-1.%d\n"%self.version result = ("%%PDF-1.%d\n"%self.version).encode("utf8")
xreftable.append("0000000000 65535 f \n") xreftable.append(b"0000000000 65535 f \n")
for o in self.objects: for o in self.objects:
xreftable.append("%010d 00000 n \n"%len(result)) xreftable.append(("%010d 00000 n \n"%len(result)).encode("utf8"))
result += o.tostring() result += o.tostring()
xrefoffset = len(result) xrefoffset = len(result)
result += "xref\n" result += b"xref\n"
result += "0 %d\n"%len(xreftable) result += ("0 %d\n"%len(xreftable)).encode("utf8")
for x in xreftable: for x in xreftable:
result += x result += x
result += "trailer\n" result += b"trailer\n"
result += parse({"/Size": len(xreftable), "/Info": self.info, "/Root": self.catalog})+"\n" result += parse({"/Size": len(xreftable), "/Info": self.info, "/Root": self.catalog})+b"\n"
result += "startxref\n" result += b"startxref\n"
result += "%d\n"%xrefoffset result += ("%d\n"%xrefoffset).encode("utf8")
result += "%%EOF\n" result += b"%%EOF\n"
return result return result
def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None, def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None,

@ -20,6 +20,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/>.
import struct import struct
import sys
def getBox(data, byteStart, noBytes): def getBox(data, byteStart, noBytes):
boxLengthValue = struct.unpack(">I", data[byteStart:byteStart+4])[0] boxLengthValue = struct.unpack(">I", data[byteStart:byteStart+4])[0]
@ -85,6 +86,6 @@ def parsejp2(data):
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
width, height, colorspace = parsejp2(open(sys.argv[1]).read()) width, height, colorspace = parsejp2(open(sys.argv[1]).read())
print "width = %d"%width sys.stdout.write("width = %d"%width)
print "height = %d"%height sys.stdout.write("height = %d"%height)
print "colorspace = %s"%colorspace sys.stdout.write("colorspace = %s"%colorspace)

Loading…
Cancel
Save