diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index 26c84dc46..1f37ab6ff 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -923,7 +923,7 @@ def compress_content_streams(self): if content is not None: if not isinstance(content, ContentStream): content = ContentStream(content, self.pdf) - self[NameObject(PG.CONTENTS)] = content.flateEncode() + self[NameObject(PG.CONTENTS)] = content.flate_encode() def compressContentStreams(self): """ diff --git a/PyPDF2/_reader.py b/PyPDF2/_reader.py index 6680ca2ac..6fc8bd587 100644 --- a/PyPDF2/_reader.py +++ b/PyPDF2/_reader.py @@ -389,7 +389,7 @@ def xmp_metadata(self): """ try: self._override_encryption = True - return self.trailer[TK.ROOT].getXmpMetadata() + return self.trailer[TK.ROOT].xmp_metadata finally: self._override_encryption = False @@ -729,9 +729,9 @@ def getNamedDestinations(self, tree=None, retval=None): @property def outlines(self): """Read-only property.""" - return self.get_outlines() + return self._get_outlines() - def get_outlines(self, node=None, outlines=None): + def _get_outlines(self, node=None, outlines=None): """ Retrieve the document outline present in the document. @@ -768,7 +768,7 @@ def get_outlines(self, node=None, outlines=None): # check for sub-outlines if "/First" in node: sub_outlines = [] - self.get_outlines(node["/First"], sub_outlines) + self._get_outlines(node["/First"], sub_outlines) if sub_outlines: outlines.append(sub_outlines) @@ -778,18 +778,33 @@ def get_outlines(self, node=None, outlines=None): return outlines + def get_outlines(self, node=None, outlines=None): + """ + .. deprecated:: 1.28.0 + + Use the property :py:attr:`outlines` instead. + """ + warnings.warn( + "get_outlines will be removed in PyPDF2 2.0.0. " + "Use the property :py:attr:`outlines` instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self._get_outlines(node, outlines) + def getOutlines(self, node=None, outlines=None): """ .. deprecated:: 1.28.0 - Use :meth:`get_outlines` instead. + Use the property :py:attr:`outlines` instead. """ warnings.warn( - "getOutlines will be removed in PyPDF2 2.0.0. Use get_outlines instead.", + "getOutlines will be removed in PyPDF2 2.0.0. " + "Use the property 'outlines' instead.", PendingDeprecationWarning, stacklevel=2, ) - return self.get_outlines(node, outlines) + return self._get_outlines(node, outlines) def _get_page_number_by_indirect(self, indirect_ref): """Generate _pageId2Num""" @@ -1073,7 +1088,7 @@ def _get_object_from_stream(self, indirect_reference): assert obj_stm["/Type"] == "/ObjStm" # /N is the number of indirect objects in the stream assert idx < obj_stm["/N"] - stream_data = BytesIO(b_(obj_stm.getData())) + stream_data = BytesIO(b_(obj_stm.get_data())) for i in range(obj_stm["/N"]): readNonWhitespace(stream_data) stream_data.seek(-1, 1) @@ -1515,7 +1530,7 @@ def _read_pdf15_xref_stream(self, stream): xrefstream = read_object(stream, self) assert xrefstream["/Type"] == "/XRef" self.cache_indirect_object(generation, idnum, xrefstream) - stream_data = BytesIO(b_(xrefstream.getData())) + stream_data = BytesIO(b_(xrefstream.get_data())) # Index pairs specify the subsections in the dictionary. If # none create one subsection that spans everything. idx_pairs = xrefstream.get("/Index", [0, xrefstream.get("/Size")]) diff --git a/PyPDF2/_writer.py b/PyPDF2/_writer.py index f4144ee9f..9db67416a 100644 --- a/PyPDF2/_writer.py +++ b/PyPDF2/_writer.py @@ -966,7 +966,7 @@ def add_bookmark_destination(self, dest, parent=None): parent = outline_ref parent = parent.get_object() - parent.addChild(dest_ref, self) + parent.add_child(dest_ref, self) return dest_ref @@ -1002,7 +1002,7 @@ def add_bookmark_dict(self, bookmark, parent=None): parent = outline_ref parent = parent.get_object() - parent.addChild(bookmark_ref, self) + parent.add_child(bookmark_ref, self) return bookmark_ref @@ -1053,7 +1053,7 @@ def add_bookmark( dest = Destination( NameObject("/" + title + " bookmark"), page_ref, NameObject(fit), *zoom_args ) - dest_array = dest.getDestArray() + dest_array = dest.dest_array action.update( {NameObject("/D"): dest_array, NameObject("/S"): NameObject("/GoTo")} ) @@ -1089,7 +1089,7 @@ def add_bookmark( bookmark_ref = self._add_object(bookmark) parent = parent.get_object() - parent.addChild(bookmark_ref, self) + parent.add_child(bookmark_ref, self) return bookmark_ref @@ -1470,7 +1470,7 @@ def add_link(self, pagenum, pagedest, rect, border=None, fit="/Fit", *args): dest = Destination( NameObject("/LinkName"), page_dest, NameObject(fit), *zoom_args ) # TODO: create a better name for the link - dest_array = dest.getDestArray() + dest_array = dest.dest_array lnk = DictionaryObject() lnk.update( diff --git a/PyPDF2/filters.py b/PyPDF2/filters.py index 60051b4b9..207a95281 100644 --- a/PyPDF2/filters.py +++ b/PyPDF2/filters.py @@ -553,7 +553,7 @@ def _xobj_to_image(x_object_obj): from PyPDF2.constants import GraphicsStateParameters as G size = (x_object_obj[IA.WIDTH], x_object_obj[IA.HEIGHT]) - data = x_object_obj.getData() + data = x_object_obj.get_data() if x_object_obj[IA.COLOR_SPACE] == ColorSpaces.DEVICE_RGB: mode = "RGB" else: @@ -564,7 +564,7 @@ def _xobj_to_image(x_object_obj): extension = ".png" img = Image.frombytes(mode, size, data) if G.S_MASK in x_object_obj: # add alpha channel - alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].getData()) + alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].get_data()) img.putalpha(alpha) img_byte_arr = io.BytesIO() img.save(img_byte_arr, format="PNG") diff --git a/PyPDF2/generic.py b/PyPDF2/generic.py index e95d0abe6..613e99891 100644 --- a/PyPDF2/generic.py +++ b/PyPDF2/generic.py @@ -704,7 +704,8 @@ def setdefault(self, key, value=None): def __getitem__(self, key): return dict.__getitem__(self, key).get_object() - def getXmpMetadata(self): + @property + def xmp_metadata(self): """ Retrieve XMP (Extensible Metadata Platform) data relevant to the this object, if available. @@ -725,15 +726,31 @@ def getXmpMetadata(self): self[NameObject("/Metadata")] = metadata return metadata + def getXmpMetadata(self): # XmpInformation + """ + .. deprecated:: 1.28.3 + Use :meth:`xmp_metadata` instead. + """ + warnings.warn( + "getXmpMetadata will be removed in PyPDF2 2.0.0. " + "Use xmp_metadata instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.xmp_metadata + @property def xmpMetadata(self): """ - Read-only property that accesses the {@link - #DictionaryObject.getXmpData getXmpData} function. -
- Stability: Added in v1.12, will exist for all future v1.x releases.
+ .. deprecated:: 1.28.3
+ Use :meth:`xmp_metadata` instead.
"""
- return self.getXmpMetadata()
+ warnings.warn(
+ "xmpMetadata will be removed in PyPDF2 2.0.0. Use xmp_metadata instead.",
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ return self.xmp_metadata
def write_to_stream(self, stream, encryption_key):
stream.write(b_("<<\n"))
@@ -888,6 +905,14 @@ def children(self):
child = child["/Next"]
def addChild(self, child, pdf):
+ warnings.warn(
+ DEPR_MSG.format("addChild", "add_child"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ self.add_child(child, pdf)
+
+ def add_child(self, child, pdf): # PdfReader
child_obj = child.get_object()
child = pdf.getReference(child_obj)
assert isinstance(child, IndirectObject)
@@ -913,6 +938,14 @@ def addChild(self, child, pdf):
child_obj[NameObject("/Parent")] = parent_ref
def removeChild(self, child):
+ warnings.warn(
+ DEPR_MSG.format("removeChild", "remove_child"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ self.remove_child(child)
+
+ def remove_child(self, child):
child_obj = child.get_object()
if NameObject("/Parent") not in child_obj:
@@ -1000,7 +1033,25 @@ def emptyTree(self):
class StreamObject(DictionaryObject):
def __init__(self):
self._data = None
- self.decodedSelf = None
+ self.decoded_self = None
+
+ @property
+ def decodedSelf(self):
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ return self.decoded_self
+
+ @decodedSelf.setter
+ def decodedSelf(self, value):
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ self.decoded_self = value
def write_to_stream(self, stream, encryption_key):
self[NameObject(SA.LENGTH)] = NumberObject(len(self._data))
@@ -1026,6 +1077,14 @@ def initializeFromDictionary(data):
return retval
def flateEncode(self):
+ warnings.warn(
+ DEPR_MSG.format("flateEncode", "flate_encode"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ return self.flate_encode()
+
+ def flate_encode(self):
if SA.FILTER in self:
f = self[SA.FILTER]
if isinstance(f, ArrayObject):
@@ -1044,21 +1103,55 @@ def flateEncode(self):
class DecodedStreamObject(StreamObject):
+ def get_data(self):
+ return self._data
+
+ def set_data(self, data):
+ self._data = data
+
def getData(self):
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
return self._data
def setData(self, data):
- self._data = data
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ self.set_data(data)
class EncodedStreamObject(StreamObject):
def __init__(self):
- self.decodedSelf = None
+ self.decoded_self = None
- def getData(self):
- if self.decodedSelf:
+ @property
+ def decodedSelf(self):
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ return self.decoded_self
+
+ @decodedSelf.setter
+ def decodedSelf(self, value):
+ warnings.warn(
+ DEPR_MSG.format("decodedSelf", "decoded_self"),
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ self.decoded_self = value
+
+ def get_data(self):
+ if self.decoded_self:
# cached version of decoded object
- return self.decodedSelf.getData()
+ return self.decoded_self.get_data()
else:
# create decoded object
decoded = DecodedStreamObject()
@@ -1067,10 +1160,10 @@ def getData(self):
for key, value in list(self.items()):
if key not in (SA.LENGTH, SA.FILTER, SA.DECODE_PARMS):
decoded[key] = value
- self.decodedSelf = decoded
+ self.decoded_self = decoded
return decoded._data
- def setData(self, data):
+ def set_data(self, data):
raise PdfReadError("Creating EncodedStreamObject is not currently supported")
@@ -1084,10 +1177,10 @@ def __init__(self, stream, pdf):
if isinstance(stream, ArrayObject):
data = b_("")
for s in stream:
- data += b_(s.get_object().getData())
+ data += b_(s.get_object().get_data())
stream = BytesIO(b_(data))
else:
- stream = BytesIO(b_(stream.getData()))
+ stream = BytesIO(b_(stream.get_data()))
self.__parseContentStream(stream)
def __parseContentStream(self, stream):
@@ -1223,13 +1316,21 @@ def __init__(self, arr):
# must have four points
assert len(arr) == 4
# automatically convert arr[x] into NumberObject(arr[x]) if necessary
- ArrayObject.__init__(self, [self.ensureIsNumber(x) for x in arr])
+ ArrayObject.__init__(self, [self._ensure_is_number(x) for x in arr])
- def ensureIsNumber(self, value):
+ def _ensure_is_number(self, value):
if not isinstance(value, (NumberObject, FloatObject)):
value = FloatObject(value)
return value
+ def ensureIsNumber(self, value):
+ warnings.warn(
+ "ensureIsNumber will be removed in PyPDF2 2.0.0. ",
+ PendingDeprecationWarning,
+ stacklevel=2,
+ )
+ return self._ensure_is_number(value)
+
def __repr__(self):
return "RectangleObject(%s)" % repr(list(self))
@@ -1323,7 +1424,7 @@ def lower_left(self):
@lower_left.setter
def lower_left(self, value):
- self[0], self[1] = [self.ensureIsNumber(x) for x in value]
+ self[0], self[1] = [self._ensure_is_number(x) for x in value]
@property
def lower_right(self):
@@ -1335,7 +1436,7 @@ def lower_right(self):
@lower_right.setter
def lower_right(self, value):
- self[2], self[1] = [self.ensureIsNumber(x) for x in value]
+ self[2], self[1] = [self._ensure_is_number(x) for x in value]
@property
def upper_left(self):
@@ -1347,7 +1448,7 @@ def upper_left(self):
@upper_left.setter
def upper_left(self, value):
- self[0], self[3] = [self.ensureIsNumber(x) for x in value]
+ self[0], self[3] = [self._ensure_is_number(x) for x in value]
@property
def upper_right(self):
@@ -1359,7 +1460,7 @@ def upper_right(self):
@upper_right.setter
def upper_right(self, value):
- self[2], self[3] = [self.ensureIsNumber(x) for x in value]
+ self[2], self[3] = [self._ensure_is_number(x) for x in value]
def getLowerLeft(self):
warnings.warn(
@@ -1407,7 +1508,7 @@ def setLowerRight(self, value):
PendingDeprecationWarning,
stacklevel=2,
)
- self[2], self[1] = [self.ensureIsNumber(x) for x in value]
+ self[2], self[1] = [self._ensure_is_number(x) for x in value]
def setUpperLeft(self, value):
warnings.warn(
@@ -1415,7 +1516,7 @@ def setUpperLeft(self, value):
PendingDeprecationWarning,
stacklevel=2,
)
- self[0], self[3] = [self.ensureIsNumber(x) for x in value]
+ self[0], self[3] = [self._ensure_is_number(x) for x in value]
def setUpperRight(self, value):
warnings.warn(
@@ -1423,7 +1524,7 @@ def setUpperRight(self, value):
PendingDeprecationWarning,
stacklevel=2,
)
- self[2], self[3] = [self.ensureIsNumber(x) for x in value]
+ self[2], self[3] = [self._ensure_is_number(x) for x in value]
@property
def width(self):
@@ -1517,7 +1618,7 @@ def upperRight(self, value):
class Field(TreeObject):
"""
A class representing a field dictionary. This class is accessed through
- :meth:`getFields()