Skip to content

Commit

Permalink
DEP: PEP8 renaming (#905)
Browse files Browse the repository at this point in the history
PdfReader: 
* getXmpMetadata / xmpMetadata ➔ xmp_metadata
* get_outlines ➔ _get_outlines (use outlines property instead)

Field attributes:
* additionalActions ➔ additional_actions
* defaultValue ➔ default_value
* mappingName ➔ mapping_name
* altName ➔ alternate_name
* fieldType ➔ field_type

StreamObject: 
* decodedSelf : decoded_self
* flateEncode  ➔ flate_encode

Other:

* Destination: getDestArray ➔ dest_array
* RectangleObject: ensureIsNumber ➔ _ensure_is_number
* TreeObject: addChild / removeChild  ➔ add_child / remove_child
* DecodedStreamObject: getData / setData  ➔ get_data / set_data

See #900
  • Loading branch information
MartinThoma authored May 26, 2022
1 parent 4fdbb3b commit c774ab0
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 120 deletions.
2 changes: 1 addition & 1 deletion PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
33 changes: 24 additions & 9 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")])
Expand Down
10 changes: 5 additions & 5 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")}
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down
Loading

0 comments on commit c774ab0

Please sign in to comment.