Skip to content

Commit

Permalink
fix static errors raised by pyflakes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Jan 18, 2016
1 parent 74c1366 commit 1d271b7
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -38,6 +38,7 @@ htmlcov
.mr.developer.cfg
.project
.pydevproject
.venv

# Complexity
output/*.html
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -5,3 +5,4 @@ httplib2==0.7.6
nose==1.3.3
pyPdf2==1.19
reportlab>=3.0
six
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -35,7 +35,7 @@
maintainer_email="hello+pleaseleavemealone@darylyu.com",
url="http://www.xhtml2pdf.com",
keywords="PDF, HTML, XHTML, XML, CSS",
install_requires=["html5lib", "httplib2", "pyPdf2", "Pillow", "reportlab>=3.0"],
install_requires=["html5lib", "httplib2", "pyPdf2", "Pillow", "reportlab>=3.0", "six"],
include_package_data=True,
packages=find_packages(exclude=["tests", "tests.*"]),
# test_suite = "tests", They're not even working yet
Expand Down
18 changes: 7 additions & 11 deletions xhtml2pdf/context.py
Expand Up @@ -17,13 +17,9 @@
import os
import re
import reportlab
import sys
#support python 3
#from types import StringTypes, TupleType, ListType
if sys.version[0] == '2':
StringTypes = (str,unicode)
else:
StringTypes = (str,)
import six


TupleType = tuple
ListType = list
try:
Expand Down Expand Up @@ -586,7 +582,7 @@ def toParagraphStyle(self, first):

def addTOC(self):
styles = []
for i in xrange(20):
for i in six.range(20):
self.node.attributes["class"] = "pdftoclevel%d" % i
self.cssAttr = xhtml2pdf.parser.CSSCollect(self.node, self)
xhtml2pdf.parser.CSS2Frag(self, {
Expand Down Expand Up @@ -847,11 +843,11 @@ def getFontName(self, names, default="helvetica"):
"""
# print names, self.fontList
if type(names) is not ListType:
if type(names) not in StringTypes:
if type(names) not in six.string_types:
names = str(names)
names = names.strip().split(",")
for name in names:
if type(name) not in StringTypes:
if type(name) not in six.string_types:
name = str(name)
font = self.fontList.get(name.strip().lower(), None)
if font is not None:
Expand All @@ -861,7 +857,7 @@ def getFontName(self, names, default="helvetica"):
def registerFont(self, fontname, alias=[]):
self.fontList[str(fontname).lower()] = str(fontname)
for a in alias:
if type(fontname) not in StringTypes:
if type(fontname) not in six.string_types:
fontname = str(fontname)
self.fontList[str(a)] = fontname

Expand Down
16 changes: 4 additions & 12 deletions xhtml2pdf/parser.py
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
from __future__ import print_function, unicode_literals
from html5lib import treebuilders, inputstream
from xhtml2pdf.default import TAGS, STRING, INT, BOOL, SIZE, COLOR, FILE
from xhtml2pdf.default import STRING, INT, BOOL, SIZE, COLOR, FILE
from xhtml2pdf.default import BOX, POS, MUST, FONT
from xhtml2pdf.util import getSize, getBool, toList, getColor, getAlign
from xhtml2pdf.util import getBox, getPos, pisaTempFile
Expand All @@ -30,18 +30,10 @@
import html5lib
import logging
import re

import sys
#support python 3
#import types
if sys.version[0] == '2':
StringTypes = (str,unicode)
else:
StringTypes = (str,)
import six

import xhtml2pdf.w3c.cssDOMElementInterface as cssDOMElementInterface
import xml.dom.minidom
from six import text_type

CSSAttrCache = {}

Expand Down Expand Up @@ -279,7 +271,7 @@ def CSSCollect(node, c):
return node.cssAttrs

def lower(sequence):
if type(sequence) in StringTypes:
if type(sequence) in six.string_types:
return sequence.lower()
else:
return sequence[0].lower()
Expand Down Expand Up @@ -673,7 +665,7 @@ def pisaParser(src, context, default_css="", xhtml=False, encoding=None, xml_out
else:
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"))

if isinstance(src, text_type):
if isinstance(src, six.text_type):
# If an encoding was provided, do not change it.
if not encoding:
encoding = "utf-8"
Expand Down
8 changes: 5 additions & 3 deletions xhtml2pdf/pdf.py
Expand Up @@ -14,11 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from xhtml2pdf.util import pisaTempFile, getFile, PyPDF2

import six
import logging


from xhtml2pdf.util import pisaTempFile, getFile, PyPDF2


log = logging.getLogger("xhtml2pdf")


Expand Down Expand Up @@ -50,7 +52,7 @@ def join(self, file=None):
output = PyPDF2.PdfFileWriter()
for pdffile in self.files:
input = PyPDF2.PdfFileReader(pdffile)
for pageNumber in xrange(input.getNumPages()):
for pageNumber in six.range(input.getNumPages()):
output.addPage(input.getPage(pageNumber))

if file is not None:
Expand Down
9 changes: 5 additions & 4 deletions xhtml2pdf/pisa.py
Expand Up @@ -7,6 +7,7 @@
import glob
import logging
import os
import six
import sys
import tempfile
try:
Expand Down Expand Up @@ -122,7 +123,7 @@ def getFileName(self, name, relative=None):
suffix = new_suffix
path = tempfile.mktemp(prefix="pisa-", suffix=suffix)
ufile = urllib2.urlopen(url)
tfile = file(path, "wb")
tfile = open(path, "wb")
while True:
data = ufile.read(1024)
if not data:
Expand Down Expand Up @@ -252,7 +253,7 @@ def execute():

if o in ("-c", "--css"):
# CSS
css = file(a, "r").read()
css = open(a, "r").read()

if o in ("--css-dump",):
# CSS dump
Expand Down Expand Up @@ -321,7 +322,7 @@ def execute():
if dest_part.lower().endswith(".html") or dest_part.lower().endswith(".htm"):
dest_part = ".".join(src.split(".")[:-1])
dest = dest_part + "." + format.lower()
for i in xrange(10):
for i in six.range(10):
try:
open(dest, "wb").close()
break
Expand Down Expand Up @@ -353,7 +354,7 @@ def execute():
if not quiet:
print ("Converting %s to %s...") % (src, dest)

pdf = pisaDocument(
pisaDocument(
fsrc,
fdest,
debug=debug,
Expand Down
17 changes: 8 additions & 9 deletions xhtml2pdf/reportlab_paragraph.py
Expand Up @@ -7,6 +7,8 @@

#validate version sys.version[0] == 2 -> is python 2
#validate version sys.version[0] == 3 -> is python 3
import re
import six
import sys

###############################################################
Expand Down Expand Up @@ -37,7 +39,6 @@ def __init__(self,stream):
from reportlab.lib.textsplit import ALL_CANNOT_START
from copy import deepcopy
from reportlab.lib.abag import ABag
import re


PARAGRAPH_DEBUG = False
Expand Down Expand Up @@ -616,12 +617,12 @@ def splitLines0(frags, widths):
"""

#initialise the algorithm
lines = []
lineNum = 0
maxW = widths[lineNum]
i = -1
l = len(frags)
lim = start = 0
text = frags[0]
while 1:
#find a non whitespace character
while i < l:
Expand Down Expand Up @@ -1449,7 +1450,6 @@ def breakLinesCJK(self, width):
#preserving splitting algorithm
return f.clone(kind=0, lines=self.blPara.lines)
lines = []
lineno = 0

self.height = 0

Expand Down Expand Up @@ -1597,23 +1597,22 @@ def drawPara(self, debug=0):
if link: _do_link_line(0, dx, ws, tx)

#now the middle of the paragraph, aligned with the left margin which is our origin.
for i in xrange(1, nLines):
for i in six.range(1, nLines):
ws = lines[i][0]
t_off = dpl(tx, _offsets[i], ws, lines[i][1], noJustifyLast and i == lim)
if dpl != _justifyDrawParaLine: ws = 0
if underline: _do_under_line(i, t_off + leftIndent, ws, tx)
if strike: _do_under_line(i, t_off + leftIndent, ws, tx, lm=0.125)
if link: _do_link_line(i, t_off + leftIndent, ws, tx)
else:
for i in xrange(1, nLines):
for i in six.range(1, nLines):
dpl(tx, _offsets[i], lines[i][0], lines[i][1], noJustifyLast and i == lim)
else:
f = lines[0]
cur_y = self.height - getattr(f, 'ascent', f.fontSize) # TODO fix XPreformatted to remove this hack
# default?
dpl = _leftDrawParaLineX
if bulletText:
oo = offset
offset = _drawBullet(canvas, offset, cur_y, bulletText, style)
if alignment == TA_LEFT:
dpl = _leftDrawParaLineX
Expand Down Expand Up @@ -1662,7 +1661,7 @@ def drawPara(self, debug=0):
_do_post_text(tx)

#now the middle of the paragraph, aligned with the left margin which is our origin.
for i in xrange(1, nLines):
for i in six.range(1, nLines):
f = lines[i]
dpl(tx, _offsets[i], f, noJustifyLast and i == lim)
_do_post_text(tx)
Expand Down Expand Up @@ -1718,7 +1717,7 @@ def dumpParagraphLines(P):
words = line[1]
nwords = len(words)
print ('line%d: %d(%s)\n ') % (l, nwords, str(getattr(line, 'wordCount', 'Unknown'))),
for w in xrange(nwords):
for w in six.range(nwords):
print ("%d:'%s'") % (w, getattr(words[w], 'text', words[w])),
print()

Expand All @@ -1733,7 +1732,7 @@ def dumpParagraphFrags(P):
print ('dumpParagraphFrags(<Paragraph @ %d>) minWidth() = %.2f') % (id(P), P.minWidth())
frags = P.frags
n = len(frags)
for l in xrange(n):
for l in six.range(n):
print ("frag%d: '%s' %s") % (
l, frags[l].text, ' '.join(['%s=%s' % (k, getattr(frags[l], k)) for k in frags[l].__dict__ if k != text]))

Expand Down
5 changes: 3 additions & 2 deletions xhtml2pdf/tables.py
Expand Up @@ -5,6 +5,7 @@
from xhtml2pdf.xhtml2pdf_reportlab import PmlTable, PmlKeepInFrame
import copy
import logging
import six

# Copyright 2010 Dirk Holtwick, holtwick.it
#
Expand Down Expand Up @@ -267,8 +268,8 @@ def start(self, c):
if begin != end:
#~ print begin, end
tdata.add_style(('SPAN', begin, end))
for x in xrange(begin[0], end[0] + 1):
for y in xrange(begin[1], end[1] + 1):
for x in six.range(begin[0], end[0] + 1):
for y in six.range(begin[1], end[1] + 1):
if x != begin[0] or y != begin[1]:
tdata.add_empty(x, y)

Expand Down
9 changes: 4 additions & 5 deletions xhtml2pdf/tags.py
Expand Up @@ -14,10 +14,9 @@
import logging
import re
import warnings
import six
import string

from six import text_type

# Copyright 2010 Dirk Holtwick, holtwick.it
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -183,7 +182,7 @@ class pisaTagH6(pisaTagP):

def listDecimal(c):
c.listCounter += 1
return text_type("%d." % c.listCounter)
return six.text_type("%d." % c.listCounter)


roman_numeral_map = (
Expand Down Expand Up @@ -215,7 +214,7 @@ def int_to_roman(i):
def listUpperRoman(c):
c.listCounter += 1
roman = int_to_roman(c.listCounter)
return unicode("%s." % roman)
return six.text_type("%s." % roman)


def listLowerRoman(c):
Expand All @@ -232,7 +231,7 @@ def listUpperAlpha(c):
# this will probably fail for anything past the 2nd time
alpha = string.ascii_uppercase[index - 26]
alpha *= 2
return unicode("%s." % alpha)
return six.text_type("%s." % alpha)


def listLowerAlpha(c):
Expand Down

0 comments on commit 1d271b7

Please sign in to comment.