From a2cef35580a21435a714bee1f30e06c04b0c41ea Mon Sep 17 00:00:00 2001 From: Edwar Baron Date: Fri, 9 Jan 2015 13:46:56 -0430 Subject: [PATCH] support python 3 --- xhtml2pdf/parser.py | 27 +++++++++++++++++++++------ xhtml2pdf/util.py | 7 ++++--- xhtml2pdf/xhtml2pdf_reportlab.py | 6 +++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/xhtml2pdf/parser.py b/xhtml2pdf/parser.py index 85f98d63..d9d132ff 100644 --- a/xhtml2pdf/parser.py +++ b/xhtml2pdf/parser.py @@ -30,7 +30,17 @@ import html5lib import logging import re -import types + +import sys +#support python 3 +#import types +if sys.version[0] == '2': + StringTypes = (str,unicode) +else: + StringTypes = (str,) +TupleType = tuple +ListType = list + import xhtml2pdf.w3c.cssDOMElementInterface as cssDOMElementInterface import xml.dom.minidom @@ -66,11 +76,16 @@ def pisaGetAttributes(c, tag, attributes): block, adef = TAGS[tag] adef["id"] = STRING # print block, adef - for k, v in adef.iteritems(): + try: + iteritems = adef.iteritems() + except Exception: + iteritems = iter(adef.items()) + + for k, v in iteritems: nattrs[k] = None # print k, v # defaults, wenn vorhanden - if type(v) == types.TupleType: + if type(v) == TupleType: if v[1] == MUST: if k not in attrs: log.warn(c.warning("Attribute '%s' must be set!", k)) @@ -84,7 +99,7 @@ def pisaGetAttributes(c, tag, attributes): dfl = None if nv is not None: - if type(v) == types.ListType: + if type(v) == ListType: nv = nv.strip().lower() if nv not in v: #~ raise PML_EXCEPTION, "attribute '%s' of wrong value, allowed is one of: %s" % (k, repr(v)) @@ -647,8 +662,8 @@ def pisaParser(src, context, default_css="", xhtml=False, encoding=None, xml_out else: parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom")) - if type(src) in types.StringTypes: - if type(src) is types.UnicodeType: + if type(src) in StringTypes: + if type(src) is UnicodeType: # If an encoding was provided, do not change it. if not encoding: encoding = "utf-8" diff --git a/xhtml2pdf/util.py b/xhtml2pdf/util.py index 5b4aa3bd..8bcb7845 100644 --- a/xhtml2pdf/util.py +++ b/xhtml2pdf/util.py @@ -75,7 +75,6 @@ except: renderSVG = None - #========================================================================= # Memoize decorator #========================================================================= @@ -102,7 +101,10 @@ def __init__(self, func): def __call__(self, *args, **kwargs): # Make sure the following line is not actually slower than what you're # trying to memoize - args_plus = tuple(kwargs.iteritems()) + if sys.version[0] == '2': + args_plus = tuple(kwargs.iteritems()) + else: + args_plus = tuple(iter(kwargs.items())) key = (args, args_plus) try: if key not in self.cache: @@ -119,7 +121,6 @@ def ErrorMsg(): Helper to get a nice traceback as string """ import traceback - import sys type = value = tb = limit = None type, value, tb = sys.exc_info() diff --git a/xhtml2pdf/xhtml2pdf_reportlab.py b/xhtml2pdf/xhtml2pdf_reportlab.py index 9814a724..c8b75dce 100644 --- a/xhtml2pdf/xhtml2pdf_reportlab.py +++ b/xhtml2pdf/xhtml2pdf_reportlab.py @@ -27,9 +27,11 @@ from xhtml2pdf.reportlab_paragraph import Paragraph from xhtml2pdf.util import getUID, getBorderStyle +import sys + #support python 3 #from types import StringType, TupleType, ListType, IntType -StringType = str +StringType = str TupleType = tuple ListType = list IntType = int @@ -46,8 +48,6 @@ class StringIO(object): import copy import logging import reportlab.pdfbase.pdfform as pdfform -import sys - try: import PIL.Image as PILImage