Skip to content

Commit e1d65dc

Browse files
authored
STY: Rename local annotation variables (#2980)
Make the names of annotation variables more consistent and readable.
1 parent 6f541d3 commit e1d65dc

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

pypdf/_writer.py

+38-39
Original file line numberDiff line numberDiff line change
@@ -873,16 +873,16 @@ def append_pages_from_reader(
873873
def _update_field_annotation(
874874
self,
875875
field: DictionaryObject,
876-
anno: DictionaryObject,
876+
annotation: DictionaryObject,
877877
font_name: str = "",
878878
font_size: float = -1,
879879
) -> None:
880880
# Calculate rectangle dimensions
881-
_rct = cast(RectangleObject, anno[AA.Rect])
881+
_rct = cast(RectangleObject, annotation[AA.Rect])
882882
rct = RectangleObject((0, 0, abs(_rct[2] - _rct[0]), abs(_rct[3] - _rct[1])))
883883

884884
# Extract font information
885-
da = anno.get_inherited(
885+
da = annotation.get_inherited(
886886
AA.DA,
887887
cast(DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]).get(
888888
AA.DA, None
@@ -917,7 +917,7 @@ def _update_field_annotation(
917917
DictionaryObject,
918918
cast(
919919
DictionaryObject,
920-
anno.get_inherited(
920+
annotation.get_inherited(
921921
"/DR",
922922
cast(
923923
DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]
@@ -942,7 +942,7 @@ def _update_field_annotation(
942942
font_subtype, _, font_encoding, font_map = build_char_map_from_dict(
943943
200, font_res
944944
)
945-
try: # get rid of width stored in -1 key
945+
try: # remove width stored in -1 key
946946
del font_map[-1]
947947
except KeyError:
948948
pass
@@ -963,14 +963,14 @@ def _update_field_annotation(
963963
# Retrieve field text and selected values
964964
field_flags = field.get(FA.Ff, 0)
965965
if field.get(FA.FT, "/Tx") == "/Ch" and field_flags & FA.FfBits.Combo == 0:
966-
txt = "\n".join(anno.get_inherited(FA.Opt, []))
966+
txt = "\n".join(annotation.get_inherited(FA.Opt, []))
967967
sel = field.get("/V", [])
968968
if not isinstance(sel, list):
969969
sel = [sel]
970970
else: # /Tx
971971
txt = field.get("/V", "")
972972
sel = []
973-
# Escape parentheses (pdf 1.7 reference, table 3.2 Literal Strings)
973+
# Escape parentheses (PDF 1.7 reference, table 3.2, Literal Strings)
974974
txt = txt.replace("\\", "\\\\").replace("(", r"\(").replace(")", r"\)")
975975
# Generate appearance stream
976976
ap_stream = generate_appearance_stream(
@@ -987,8 +987,8 @@ def _update_field_annotation(
987987
"/Length": 0,
988988
}
989989
)
990-
if AA.AP in anno:
991-
for k, v in cast(DictionaryObject, anno[AA.AP]).get("/N", {}).items():
990+
if AA.AP in annotation:
991+
for k, v in cast(DictionaryObject, annotation[AA.AP]).get("/N", {}).items():
992992
if k not in {"/BBox", "/Length", "/Subtype", "/Type", "/Filter"}:
993993
dct[k] = v
994994

@@ -1005,16 +1005,16 @@ def _update_field_annotation(
10051005
)
10061006
}
10071007
)
1008-
if AA.AP not in anno:
1009-
anno[NameObject(AA.AP)] = DictionaryObject(
1008+
if AA.AP not in annotation:
1009+
annotation[NameObject(AA.AP)] = DictionaryObject(
10101010
{NameObject("/N"): self._add_object(dct)}
10111011
)
1012-
elif "/N" not in cast(DictionaryObject, anno[AA.AP]):
1013-
cast(DictionaryObject, anno[NameObject(AA.AP)])[
1012+
elif "/N" not in cast(DictionaryObject, annotation[AA.AP]):
1013+
cast(DictionaryObject, annotation[NameObject(AA.AP)])[
10141014
NameObject("/N")
10151015
] = self._add_object(dct)
10161016
else: # [/AP][/N] exists
1017-
n = anno[AA.AP]["/N"].indirect_reference.idnum # type: ignore
1017+
n = annotation[AA.AP]["/N"].indirect_reference.idnum # type: ignore
10181018
self._objects[n - 1] = dct
10191019
dct.indirect_reference = IndirectObject(n, 0, self)
10201020

@@ -1071,61 +1071,60 @@ def update_page_form_field_values(
10711071
if PG.ANNOTS not in page:
10721072
logger_warning("No fields to update on this page", __name__)
10731073
return
1074-
for writer_annot in page[PG.ANNOTS]: # type: ignore
1075-
writer_annot = cast(DictionaryObject, writer_annot.get_object())
1076-
if writer_annot.get("/Subtype", "") != "/Widget":
1074+
for annotation in page[PG.ANNOTS]: # type: ignore
1075+
annotation = cast(DictionaryObject, annotation.get_object())
1076+
if annotation.get("/Subtype", "") != "/Widget":
10771077
continue
1078-
if "/FT" in writer_annot and "/T" in writer_annot:
1079-
writer_parent_annot = writer_annot
1078+
if "/FT" in annotation and "/T" in annotation:
1079+
parent_annotation = annotation
10801080
else:
1081-
writer_parent_annot = writer_annot.get(
1081+
parent_annotation = annotation.get(
10821082
PG.PARENT, DictionaryObject()
10831083
).get_object()
10841084

10851085
for field, value in fields.items():
10861086
if not (
1087-
self._get_qualified_field_name(writer_parent_annot) == field
1088-
or writer_parent_annot.get("/T", None) == field
1087+
self._get_qualified_field_name(parent_annotation) == field
1088+
or parent_annotation.get("/T", None) == field
10891089
):
10901090
continue
10911091
if (
1092-
writer_parent_annot.get("/FT", None) == "/Ch"
1093-
and "/I" in writer_parent_annot
1092+
parent_annotation.get("/FT", None) == "/Ch"
1093+
and "/I" in parent_annotation
10941094
):
1095-
del writer_parent_annot["/I"]
1095+
del parent_annotation["/I"]
10961096
if flags:
1097-
writer_annot[NameObject(FA.Ff)] = NumberObject(flags)
1097+
annotation[NameObject(FA.Ff)] = NumberObject(flags)
10981098
if isinstance(value, list):
10991099
lst = ArrayObject(TextStringObject(v) for v in value)
1100-
writer_parent_annot[NameObject(FA.V)] = lst
1100+
parent_annotation[NameObject(FA.V)] = lst
11011101
elif isinstance(value, tuple):
1102-
writer_annot[NameObject(FA.V)] = TextStringObject(
1102+
annotation[NameObject(FA.V)] = TextStringObject(
11031103
value[0],
11041104
)
11051105
else:
1106-
writer_parent_annot[NameObject(FA.V)] = TextStringObject(value)
1107-
if writer_parent_annot.get(FA.FT) in ("/Btn"):
1108-
# case of Checkbox button (no /FT found in Radio widgets
1106+
parent_annotation[NameObject(FA.V)] = TextStringObject(value)
1107+
if parent_annotation.get(FA.FT) in ("/Btn"):
1108+
# Checkbox button (no /FT found in Radio widgets)
11091109
v = NameObject(value)
1110-
if v not in writer_annot[NameObject(AA.AP)][NameObject("/N")]:
1110+
if v not in annotation[NameObject(AA.AP)][NameObject("/N")]:
11111111
v = NameObject("/Off")
11121112
# other cases will be updated through the for loop
1113-
writer_annot[NameObject(AA.AS)] = v
1113+
annotation[NameObject(AA.AS)] = v
11141114
elif (
1115-
writer_parent_annot.get(FA.FT) == "/Tx"
1116-
or writer_parent_annot.get(FA.FT) == "/Ch"
1115+
parent_annotation.get(FA.FT) == "/Tx"
1116+
or parent_annotation.get(FA.FT) == "/Ch"
11171117
):
11181118
# textbox
11191119
if isinstance(value, tuple):
11201120
self._update_field_annotation(
1121-
writer_parent_annot, writer_annot, value[1], value[2]
1121+
parent_annotation, annotation, value[1], value[2]
11221122
)
11231123
else:
1124-
self._update_field_annotation(writer_parent_annot, writer_annot)
1124+
self._update_field_annotation(parent_annotation, annotation)
11251125
elif (
1126-
writer_annot.get(FA.FT) == "/Sig"
1126+
annotation.get(FA.FT) == "/Sig"
11271127
): # deprecated # not implemented yet
1128-
# signature
11291128
logger_warning("Signature forms not implemented yet", __name__)
11301129

11311130
def reattach_fields(

0 commit comments

Comments
 (0)