Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves issue #230 use nullhandler #231

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions examples/rl2/decodegraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def fixlist(params):

dispatch = {}
expected_args = 'self token params'.split()
for key, func in globals().items():
for key, func in list(globals().items()):
if key.startswith('parse_'):
args, varargs, keywords, defaults = getargspec(func)
assert (args == expected_args and varargs is None and
Expand Down Expand Up @@ -390,7 +390,7 @@ def parsepage(cls, page, canvas=None):
self.gpath = None
self.tpath = None
self.fontdict = dict((x, FontInfo(y)) for
(x, y) in page.Resources.Font.items())
(x, y) in list(page.Resources.Font.items()))

for token in self.tokens:
info = dispatch(token)
Expand All @@ -404,14 +404,14 @@ def parsepage(cls, page, canvas=None):
delta = len(params) - len(paraminfo)
if delta:
if delta < 0:
print ('Operator %s expected %s parameters, got %s' %
(token, len(paraminfo), params))
print(('Operator %s expected %s parameters, got %s' %
(token, len(paraminfo), params)))
params[:] = []
continue
else:
print ("Unparsed parameters/commands: %s" % params[:delta])
print(("Unparsed parameters/commands: %s" % params[:delta]))
del params[:delta]
paraminfo = zip(paraminfo, params)
paraminfo = list(zip(paraminfo, params))
try:
params[:] = [x(y) for (x, y) in paraminfo]
except:
Expand All @@ -431,13 +431,13 @@ def getvalue(oldval):
name = oldval[0].__name__

def myfunc(self, token, params):
print ('%s called %s(%s)' % (token, name,
', '.join(str(x) for x in params)))
print(('%s called %s(%s)' % (token, name,
', '.join(str(x) for x in params))))
if name in undisturbed:
myfunc = oldval[0]
return myfunc, oldval[1]
return dict((x, getvalue(y))
for (x, y) in _ParseClass.dispatch.items())
for (x, y) in list(_ParseClass.dispatch.items()))

class _DebugParse(_ParseClass):
dispatch = debugdispatch()
Expand All @@ -453,5 +453,5 @@ class _DebugParse(_ParseClass):
fname, = sys.argv[1:]
pdf = PdfReader(fname, decompress=True)
for i, page in enumerate(pdf.pages):
print ('\nPage %s ------------------------------------' % i)
print(('\nPage %s ------------------------------------' % i))
parse(page)
8 changes: 4 additions & 4 deletions examples/subset_booklets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def fixpage(*pages):
INPFN, = sys.argv[1:]
OUTFN = 'booklet.' + os.path.basename(INPFN)
ALL_IPAGES = PdfReader(INPFN).pages
print 'The pdf file '+str(INPFN)+' has '+str(len(ALL_IPAGES))+' pages.'
print('The pdf file '+str(INPFN)+' has '+str(len(ALL_IPAGES))+' pages.')

#Make sure we have an even number
if len(ALL_IPAGES) & 1:
ALL_IPAGES.append(None)
print 'Inserting one more blank page to make pages number even.'
print('Inserting one more blank page to make pages number even.')
NUM_OF_ITER, ITERS_LEFT = divmod(len(ALL_IPAGES), BOOKLET_SIZE)

print 'Making '+str(NUM_OF_ITER)+' subbooklets of '+str(BOOKLET_SIZE)+' pages each.'
print('Making '+str(NUM_OF_ITER)+' subbooklets of '+str(BOOKLET_SIZE)+' pages each.')
opages = []
for iteration in range(0, NUM_OF_ITER):
ipages = ALL_IPAGES[iteration*BOOKLET_SIZE:(iteration+1)*BOOKLET_SIZE]
Expand All @@ -58,4 +58,4 @@ def fixpage(*pages):
opages.append(fixpage(ipages.pop(), ipages.pop(0)))

PdfWriter(OUTFN).addpages(opages).write()
print 'It took '+ str(round(time.time()-START, 2))+' seconds to make the pdf subbooklets changes.'
print('It took '+ str(round(time.time()-START, 2))+' seconds to make the pdf subbooklets changes.')
2 changes: 1 addition & 1 deletion pdfrw/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (C) 2017 Jon Lund Steffensen
# MIT license -- See LICENSE.txt for details

from __future__ import division


import hashlib
import struct
Expand Down
9 changes: 1 addition & 8 deletions pdfrw/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@

import logging


fmt = logging.Formatter('[%(levelname)s] %(filename)s:%(lineno)d %(message)s')

handler = logging.StreamHandler()
handler.setFormatter(fmt)

log = logging.getLogger('pdfrw')
log.setLevel(logging.WARNING)
log.addHandler(handler)
log.addHandler(logging.NullHandler())


class PdfError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion pdfrw/findobjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def find_objects(source, valid_types=(PdfName.XObject, None),
if isinstance(obj, PdfDict):
if obj.Type in valid_types and obj.Subtype in valid_subtypes:
yield obj
obj = [y for (x, y) in sorted(obj.iteritems())
obj = [y for (x, y) in sorted(obj.items())
if x not in no_follow]
else:
# TODO: This forces resolution of any indirect objects in
Expand Down
10 changes: 5 additions & 5 deletions pdfrw/objects/pdfdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,20 @@ def iteritems(self, dictiter=iteritems,
yield key, value

def items(self):
return list(self.iteritems())
return list(self.items())

def itervalues(self):
for key, value in self.iteritems():
for key, value in self.items():
yield value

def values(self):
return list((value for key, value in self.iteritems()))
return list((value for key, value in self.items()))

def keys(self):
return list((key for key, value in self.iteritems()))
return list((key for key, value in self.items()))

def __iter__(self):
for key, value in self.iteritems():
for key, value in self.items():
yield key

def iterkeys(self):
Expand Down
22 changes: 11 additions & 11 deletions pdfrw/objects/pdfstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
A literal string may encode bytes almost unmolested. The caveat is
that if a byte has the same value as a parenthesis, it must be escaped
so that the tokenizer knows the string is not finished. This is accomplished
by using the ASCII backslash ("\") as an escape character. Of course,
by using the ASCII backslash ("\\") as an escape character. Of course,
now any backslash appearing in the data must likewise be escaped.

Hexadecimal strings
Expand Down Expand Up @@ -117,7 +117,7 @@
in literal strings is to not escape parentheses. This only works, and is
only allowed, when the parentheses are properly balanced. For example,
"((Hello))" is a valid encoding for a literal string, but "((Hello)" is not;
the latter case should be encoded "(\(Hello)"
the latter case should be encoded "(\\(Hello)"

Encoding text into strings
==========================
Expand Down Expand Up @@ -291,13 +291,13 @@ def find_pdfdocencoding(encoding):
decoding_map = dict((x, x) for x in decoding_map)

# Add in the special Unicode characters
decoding_map.update(zip(range(0x18, 0x20), (
0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC)))
decoding_map.update(zip(range(0x80, 0x9F), (
decoding_map.update(list(zip(list(range(0x18, 0x20)), (
0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC))))
decoding_map.update(list(zip(list(range(0x80, 0x9F)), (
0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044,
0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C, 0x201D, 0x2018,
0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x0141, 0x0152, 0x0160,
0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E)))
0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E))))
decoding_map[0xA0] = 0x20AC

# Make the encoding map from the decoding map
Expand Down Expand Up @@ -350,7 +350,7 @@ def init_unescapes(cls):
cls.unescape_func = unescape_func

unescape_dict = dict(((chr(x), chr(x)) for x in range(0x100)))
unescape_dict.update(zip('nrtbf', '\n\r\t\b\f'))
unescape_dict.update(list(zip('nrtbf', '\n\r\t\b\f')))
unescape_dict['\r'] = ''
unescape_dict['\n'] = ''
unescape_dict['\r\n'] = ''
Expand All @@ -372,12 +372,12 @@ def decode_literal(self):
Possible string escapes from the spec:
(PDF 1.7 Reference, section 3.2.3, page 53)

1. \[nrtbf\()]: simple escapes
1. \\[nrtbf\\()]: simple escapes
2. \\d{1,3}: octal. Must be zero-padded to 3 digits
if followed by digit
3. \<end of line>: line continuation. We don't know the EOL
3. \\<end of line>: line continuation. We don't know the EOL
marker used in the PDF, so accept \r, \n, and \r\n.
4. Any other character following \ escape -- the backslash
4. Any other character following \\ escape -- the backslash
is swallowed.
"""
result = (self.unescape_func or self.init_unescapes())(self[1:-1])
Expand Down Expand Up @@ -543,7 +543,7 @@ def from_unicode(cls, source, text_encoding='auto',
return cls.from_bytes(raw, encoding)

@classmethod
def encode(cls, source, uni_type = type(u''), isinstance=isinstance):
def encode(cls, source, uni_type = type(''), isinstance=isinstance):
""" The encode() constructor is a legacy function that is
also a convenience for the PdfWriter.
"""
Expand Down
4 changes: 2 additions & 2 deletions pdfrw/pagemerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def do_xobjs(xobj_list, restore_first=False):
if xobjs is None:
xobjs = resources.XObject = PdfDict()
else:
allkeys = xobjs.keys()
allkeys = list(xobjs.keys())
if allkeys:
keys = (x for x in allkeys if x.startswith('/pdfrw_'))
keys = (x for x in keys if x[7:].isdigit())
Expand Down Expand Up @@ -246,5 +246,5 @@ def xobj_box(self):
''' Return the smallest box that encloses every object
in the list.
'''
a, b, c, d = zip(*(xobj.box for xobj in self))
a, b, c, d = list(zip(*(xobj.box for xobj in self)))
return PdfArray((min(a), min(b), max(c), max(d)))
26 changes: 13 additions & 13 deletions pdfrw/pdfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def readdict(self, source, PdfDict=PdfDict):
'''
specialget = self.special.get
result = PdfDict()
next = source.next
next = source.__next__

tok = next()
while tok != '>>':
Expand Down Expand Up @@ -198,7 +198,7 @@ def loadindirect(self, key, PdfDict=PdfDict,
ok = ok and objid[2] == 'obj'
if not ok:
source.floc = offset
source.next()
next(source)
objheader = '%d %d obj' % (objnum, gennum)
fdata = source.fdata
offset2 = (fdata.find('\n' + objheader) + 1 or
Expand All @@ -214,7 +214,7 @@ def loadindirect(self, key, PdfDict=PdfDict,

# Read the object, and call special code if it starts
# an array or dictionary
obj = source.next()
obj = next(source)
func = self.special.get(obj)
if func is not None:
obj = func(source)
Expand All @@ -225,7 +225,7 @@ def loadindirect(self, key, PdfDict=PdfDict,
# Mark the object as indirect, and
# just return it if it is a simple object.
obj.indirect = key
tok = source.next()
tok = next(source)
if tok == 'endobj':
return obj

Expand Down Expand Up @@ -271,13 +271,13 @@ def decrypt_all(self):

if self.crypt_filters is not None:
crypt.decrypt_objects(
self.indirect_objects.values(), self.stream_crypt_filter,
list(self.indirect_objects.values()), self.stream_crypt_filter,
self.crypt_filters)

def uncompress(self):
self.read_all()

uncompress(self.indirect_objects.values())
uncompress(list(self.indirect_objects.values()))

def load_stream_objects(self, object_streams):
# read object streams
Expand All @@ -299,7 +299,7 @@ def load_stream_objects(self, object_streams):

for obj in objs:
objsource = PdfTokens(obj.stream, 0, False)
next = objsource.next
next = objsource.__next__
offsets = []
firstoffset = int(obj.First)
while objsource.floc < firstoffset:
Expand Down Expand Up @@ -329,7 +329,7 @@ def findxref(self, fdata):
if startloc < 0:
raise PdfParseError('Did not find "startxref" at end of file')
source = PdfTokens(fdata, startloc, False, self.verbose)
tok = source.next()
tok = next(source)
assert tok == 'startxref' # (We just checked this...)
tableloc = source.next_default()
if not tableloc.isdigit():
Expand All @@ -353,7 +353,7 @@ def readint(s, lengths):
offset = next

setdefault = source.obj_offsets.setdefault
next = source.next
next = source.__next__
# check for xref stream object
objid = source.multiple(3)
ok = len(objid) == 3
Expand All @@ -376,7 +376,7 @@ def readint(s, lengths):
stream = stream if stream is not old_strm else convert_store(old_strm)
num_pairs = obj.Index or PdfArray(['0', obj.Size])
num_pairs = [int(x) for x in num_pairs]
num_pairs = zip(num_pairs[0::2], num_pairs[1::2])
num_pairs = list(zip(num_pairs[0::2], num_pairs[1::2]))
entry_sizes = [int(x) for x in obj.W]
if len(entry_sizes) != 3:
source.exception('Invalid entry size')
Expand All @@ -399,7 +399,7 @@ def parse_xref_table(self, source, int=int, range=range):
''' Parse (one of) the cross-reference file section(s)
'''
setdefault = source.obj_offsets.setdefault
next = source.next
next = source.__next__
# plain xref table
start = source.floc
try:
Expand Down Expand Up @@ -448,7 +448,7 @@ def parse_xref_table(self, source, int=int, range=range):
def parsexref(self, source):
''' Parse (one of) the cross-reference file section(s)
'''
next = source.next
next = source.__next__
try:
tok = next()
except StopIteration:
Expand Down Expand Up @@ -619,7 +619,7 @@ def __init__(self, fname=None, fdata=None, decompress=False,
trailer, is_stream = self.parsexref(source)
prev = trailer.Prev
if prev is None:
token = source.next()
token = next(source)
if token != 'startxref' and not xref_list:
source.warning('Expected "startxref" '
'at end of xref table')
Expand Down
6 changes: 3 additions & 3 deletions pdfrw/pdfwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def format_obj(obj):
if compress and obj.stream:
do_compress([obj])
pairs = sorted((getattr(x, 'encoded', None) or x, y)
for (x, y) in obj.iteritems())
for (x, y) in obj.items())
myarray = []
for key, value in pairs:
myarray.append(key)
Expand Down Expand Up @@ -380,6 +380,6 @@ def make_canonical(self):
if isinstance(obj, PdfArray):
workitems += obj
else:
workitems += obj.values()
workitems += list(obj.values())

replaceable = set(vars())
replaceable = set(vars())
2 changes: 1 addition & 1 deletion pdfrw/py23_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
zlib = None

try:
unicode = unicode
str = str
except NameError:

def convert_load(s):
Expand Down
4 changes: 2 additions & 2 deletions pdfrw/toreportlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _makedict(rldoc, pdfobj):
rlobj = rldoc.Reference(rlobj)
pdfobj.derived_rl_obj[rldoc] = rlobj, None

for key, value in pdfobj.iteritems():
for key, value in pdfobj.items():
rldict[key[1:]] = makerl_recurse(rldoc, value)

return rlobj
Expand All @@ -85,7 +85,7 @@ def _makestream(rldoc, pdfobj, xobjtype=PdfName.XObject):
result = rldoc.Reference(rlobj, fullname)
pdfobj.derived_rl_obj[rldoc] = result, shortname

for key, value in pdfobj.iteritems():
for key, value in pdfobj.items():
rldict[key[1:]] = makerl_recurse(rldoc, value)

return result
Expand Down
Loading