29 Utility functions for PDF library.
31 __author__ =
"Mathieu Fenniak"
32 __author_email__ =
"biziqe@mathieu.fenniak.net"
38 import __builtin__
as builtins
43 xrange_fn = getattr(builtins,
"xrange", range)
44 _basestring = getattr(builtins,
"basestring", str)
46 bytes_type = type(bytes())
47 string_type = getattr(builtins,
"unicode", str)
48 int_types = (int, long)
if sys.version_info[0] < 3
else (int,)
53 """Test if arg is a string. Compatible with Python 2 and 3."""
54 return isinstance(s, _basestring)
58 """Test if arg is an int. Compatible with Python 2 and 3."""
59 return isinstance(n, int_types)
63 """Test if arg is a bytes instance. Compatible with Python 2 and 3."""
64 return isinstance(b, bytes_type)
69 file = filename.replace(
"/",
"\\").rsplit(
"\\", 1)[1]
70 return "%s: %s [%s:%s]\n" % (category.__name__, message, file, lineno)
75 Reads non-whitespace characters and returns them.
76 Stops upon encountering whitespace or when maxchars is reached.
81 if tok.isspace()
or not tok:
84 if len(txt) == maxchars:
91 Finds and reads the next non-whitespace character (ignores whitespace).
94 while tok
in WHITESPACES:
101 Similar to readNonWhitespace, but returns a Boolean if more than
102 one whitespace character was read.
106 while tok
in WHITESPACES:
116 while tok
not in (
b_(
'\n'),
b_(
'\r')):
122 Reads until the regular expression pattern matched (ignore the match)
123 Raise PdfStreamError on premature end-of-file.
124 :param bool ignore_eof: If true, ignore end-of-line and return immediately
128 tok = stream.read(16)
131 if ignore_eof ==
True:
135 m = regex.search(tok)
137 name += tok[:m.start()]
138 stream.seek(m.start()-len(tok), 1)
153 if isinstance(index, slice):
154 indices =
xrange_fn(*index.indices(len(self)))
156 return cls(indices.__len__,
lambda idx: self[indices[idx]])
158 raise TypeError(
"sequence indices must be integers")
162 index = len_self + index
163 if index < 0
or index >= len_self:
164 raise IndexError(
"sequence index out of range")
169 S = [i
for i
in range(256)]
172 j = (j + S[i] +
ord_(key[i % len(key)])) % 256
173 S[i], S[j] = S[j], S[i]
176 for x
in range(len(plaintext)):
179 S[i], S[j] = S[j], S[i]
180 t = S[(S[i] + S[j]) % 256]
181 retval +=
b_(chr(
ord_(plaintext[x]) ^ t))
186 return [[sum([float(i)*float(j)
187 for i, j
in zip(row, col)]
188 )
for col
in zip(*b)]
193 """Creates text file showing current location in context."""
196 stream.seek(-RADIUS, 1)
197 outputDoc = open(
'PyPDF2_pdfLocation.txt',
'w')
198 outputDoc.write(stream.read(RADIUS))
199 outputDoc.write(
'HERE')
200 outputDoc.write(stream.read(RADIUS))
202 stream.seek(-RADIUS, 1)
209 class PdfReadError(PyPdfError):
225 if sys.version_info[0] < 3:
238 r = s.encode(
'latin-1')
245 if sys.version_info[0] < 3:
246 return unicode(s,
'unicode_escape')
252 if sys.version_info[0] < 3:
256 return b.decode(
'latin-1')
262 if sys.version_info[0] < 3
or type(b) == str:
269 if sys.version_info[0] < 3:
276 if sys.version_info[0] < 3:
283 if sys.version_info[0] < 3:
284 return b.encode(
'hex')
287 coder = codecs.getencoder(
'hex_codec')
292 return hex(num).replace(
'L',
'')
295 WHITESPACES = [
b_(x)
for x
in [
' ',
'\n',
'\r',
'\t',
'\x00']]