Skip to content

Commit 9b71d74

Browse files
watermark-pdf: Omit _merge_non_default_values()
Assisted-by: Codex
1 parent 4ec78e7 commit 9b71d74

2 files changed

Lines changed: 76 additions & 121 deletions

File tree

src/pdfrest/client.py

Lines changed: 50 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,6 @@ def _parse_retry_after_header(header_value: str | None) -> float | None:
254254
return seconds if seconds > 0 else 0.0
255255

256256

257-
def _merge_non_default_values(
258-
*,
259-
payload: dict[str, Any],
260-
values: Mapping[str, Any],
261-
defaults: Mapping[str, Any],
262-
) -> None:
263-
payload.update(
264-
{
265-
key: value
266-
for key, value in values.items()
267-
if value is not None and (key not in defaults or value != defaults[key])
268-
}
269-
)
270-
271-
272257
FileContent = IO[bytes] | bytes | str
273258
FileTuple2 = tuple[str | None, FileContent]
274259
FileTuple3 = tuple[str | None, FileContent, str | None]
@@ -3663,35 +3648,21 @@ def watermark_pdf_with_text(
36633648
payload: dict[str, Any] = {
36643649
"files": file,
36653650
"watermark_text": watermark_text,
3651+
"output": output,
3652+
"font": font,
3653+
"text_size": text_size,
3654+
"text_color_rgb": text_color_rgb,
3655+
"text_color_cmyk": text_color_cmyk,
3656+
"opacity": opacity,
3657+
"horizontal_alignment": horizontal_alignment,
3658+
"vertical_alignment": vertical_alignment,
3659+
"x": x,
3660+
"y": y,
3661+
"rotation": rotation,
3662+
"pages": pages,
3663+
"behind_page": behind_page,
36663664
}
3667-
_merge_non_default_values(
3668-
payload=payload,
3669-
values={
3670-
"output": output,
3671-
"font": font,
3672-
"text_size": text_size,
3673-
"text_color_rgb": text_color_rgb,
3674-
"text_color_cmyk": text_color_cmyk,
3675-
"opacity": opacity,
3676-
"horizontal_alignment": horizontal_alignment,
3677-
"vertical_alignment": vertical_alignment,
3678-
"x": x,
3679-
"y": y,
3680-
"rotation": rotation,
3681-
"pages": pages,
3682-
"behind_page": behind_page,
3683-
},
3684-
defaults={
3685-
"text_size": 72,
3686-
"opacity": 0.5,
3687-
"horizontal_alignment": "center",
3688-
"vertical_alignment": "center",
3689-
"x": 0,
3690-
"y": 0,
3691-
"rotation": 0,
3692-
"behind_page": False,
3693-
},
3694-
)
3665+
payload = {key: value for key, value in payload.items() if value is not None}
36953666

36963667
return self._post_file_operation(
36973668
endpoint="/watermarked-pdf",
@@ -3728,32 +3699,18 @@ def watermark_pdf_with_image(
37283699
payload: dict[str, Any] = {
37293700
"files": file,
37303701
"watermark_file": watermark_file,
3702+
"output": output,
3703+
"watermark_file_scale": watermark_file_scale,
3704+
"opacity": opacity,
3705+
"horizontal_alignment": horizontal_alignment,
3706+
"vertical_alignment": vertical_alignment,
3707+
"x": x,
3708+
"y": y,
3709+
"rotation": rotation,
3710+
"pages": pages,
3711+
"behind_page": behind_page,
37313712
}
3732-
_merge_non_default_values(
3733-
payload=payload,
3734-
values={
3735-
"output": output,
3736-
"watermark_file_scale": watermark_file_scale,
3737-
"opacity": opacity,
3738-
"horizontal_alignment": horizontal_alignment,
3739-
"vertical_alignment": vertical_alignment,
3740-
"x": x,
3741-
"y": y,
3742-
"rotation": rotation,
3743-
"pages": pages,
3744-
"behind_page": behind_page,
3745-
},
3746-
defaults={
3747-
"watermark_file_scale": 0.5,
3748-
"opacity": 0.5,
3749-
"horizontal_alignment": "center",
3750-
"vertical_alignment": "center",
3751-
"x": 0,
3752-
"y": 0,
3753-
"rotation": 0,
3754-
"behind_page": False,
3755-
},
3756-
)
3713+
payload = {key: value for key, value in payload.items() if value is not None}
37573714

37583715
return self._post_file_operation(
37593716
endpoint="/watermarked-pdf",
@@ -5590,35 +5547,21 @@ async def watermark_pdf_with_text(
55905547
payload: dict[str, Any] = {
55915548
"files": file,
55925549
"watermark_text": watermark_text,
5550+
"output": output,
5551+
"font": font,
5552+
"text_size": text_size,
5553+
"text_color_rgb": text_color_rgb,
5554+
"text_color_cmyk": text_color_cmyk,
5555+
"opacity": opacity,
5556+
"horizontal_alignment": horizontal_alignment,
5557+
"vertical_alignment": vertical_alignment,
5558+
"x": x,
5559+
"y": y,
5560+
"rotation": rotation,
5561+
"pages": pages,
5562+
"behind_page": behind_page,
55935563
}
5594-
_merge_non_default_values(
5595-
payload=payload,
5596-
values={
5597-
"output": output,
5598-
"font": font,
5599-
"text_size": text_size,
5600-
"text_color_rgb": text_color_rgb,
5601-
"text_color_cmyk": text_color_cmyk,
5602-
"opacity": opacity,
5603-
"horizontal_alignment": horizontal_alignment,
5604-
"vertical_alignment": vertical_alignment,
5605-
"x": x,
5606-
"y": y,
5607-
"rotation": rotation,
5608-
"pages": pages,
5609-
"behind_page": behind_page,
5610-
},
5611-
defaults={
5612-
"text_size": 72,
5613-
"opacity": 0.5,
5614-
"horizontal_alignment": "center",
5615-
"vertical_alignment": "center",
5616-
"x": 0,
5617-
"y": 0,
5618-
"rotation": 0,
5619-
"behind_page": False,
5620-
},
5621-
)
5564+
payload = {key: value for key, value in payload.items() if value is not None}
56225565

56235566
return await self._post_file_operation(
56245567
endpoint="/watermarked-pdf",
@@ -5655,32 +5598,18 @@ async def watermark_pdf_with_image(
56555598
payload: dict[str, Any] = {
56565599
"files": file,
56575600
"watermark_file": watermark_file,
5601+
"output": output,
5602+
"watermark_file_scale": watermark_file_scale,
5603+
"opacity": opacity,
5604+
"horizontal_alignment": horizontal_alignment,
5605+
"vertical_alignment": vertical_alignment,
5606+
"x": x,
5607+
"y": y,
5608+
"rotation": rotation,
5609+
"pages": pages,
5610+
"behind_page": behind_page,
56585611
}
5659-
_merge_non_default_values(
5660-
payload=payload,
5661-
values={
5662-
"output": output,
5663-
"watermark_file_scale": watermark_file_scale,
5664-
"opacity": opacity,
5665-
"horizontal_alignment": horizontal_alignment,
5666-
"vertical_alignment": vertical_alignment,
5667-
"x": x,
5668-
"y": y,
5669-
"rotation": rotation,
5670-
"pages": pages,
5671-
"behind_page": behind_page,
5672-
},
5673-
defaults={
5674-
"watermark_file_scale": 0.5,
5675-
"opacity": 0.5,
5676-
"horizontal_alignment": "center",
5677-
"vertical_alignment": "center",
5678-
"x": 0,
5679-
"y": 0,
5680-
"rotation": 0,
5681-
"behind_page": False,
5682-
},
5683-
)
5612+
payload = {key: value for key, value in payload.items() if value is not None}
56845613

56855614
return await self._post_file_operation(
56865615
endpoint="/watermarked-pdf",

tests/test_watermark_pdf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ def test_watermark_pdf_with_text(monkeypatch: pytest.MonkeyPatch) -> None:
2828
{
2929
"files": [input_file],
3030
"watermark_text": "Confidential",
31+
"text_size": 72,
3132
"text_color_rgb": (255, 0, 0),
33+
"opacity": 0.5,
34+
"horizontal_alignment": "center",
35+
"vertical_alignment": "center",
36+
"x": 0,
37+
"y": 0,
38+
"rotation": 0,
3239
"pages": ["1", "3-5"],
40+
"behind_page": False,
3341
"output": "watermarked",
3442
}
3543
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
@@ -90,6 +98,10 @@ def test_watermark_pdf_with_image(monkeypatch: pytest.MonkeyPatch) -> None:
9098
"files": [input_file],
9199
"watermark_file": [watermark_file],
92100
"watermark_file_scale": 0.8,
101+
"opacity": 0.5,
102+
"horizontal_alignment": "center",
103+
"vertical_alignment": "center",
104+
"x": 0,
93105
"behind_page": True,
94106
"rotation": 45,
95107
"y": 25,
@@ -321,7 +333,14 @@ async def test_async_watermark_pdf_with_text(monkeypatch: pytest.MonkeyPatch) ->
321333
{
322334
"files": [input_file],
323335
"watermark_text": "Async",
336+
"text_size": 72,
324337
"opacity": 0.6,
338+
"horizontal_alignment": "center",
339+
"vertical_alignment": "center",
340+
"x": 0,
341+
"y": 0,
342+
"rotation": 0,
343+
"behind_page": False,
325344
}
326345
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
327346

@@ -379,8 +398,15 @@ async def test_async_watermark_pdf_with_image(monkeypatch: pytest.MonkeyPatch) -
379398
{
380399
"files": [input_file],
381400
"watermark_file": [watermark_file],
401+
"watermark_file_scale": 0.5,
382402
"opacity": 0.2,
403+
"horizontal_alignment": "center",
404+
"vertical_alignment": "center",
405+
"x": 0,
406+
"y": 0,
407+
"rotation": 0,
383408
"pages": ["2-last"],
409+
"behind_page": False,
384410
}
385411
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
386412

0 commit comments

Comments
 (0)