Skip to content

Commit

Permalink
support python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ebar0n committed Jan 9, 2015
1 parent e77190b commit a2cef35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
27 changes: 21 additions & 6 deletions xhtml2pdf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions xhtml2pdf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
except:
renderSVG = None


#=========================================================================
# Memoize decorator
#=========================================================================
Expand All @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions xhtml2pdf/xhtml2pdf_reportlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a2cef35

Please sign in to comment.