Skip to content

Commit 66ca573

Browse files
committed
Handle case when no watermark config is available appropriately
1 parent 6e15fcb commit 66ca573

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/dso/api.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def WatermarkedFile(output_file: Path | str, **kwargs):
136136
Yields
137137
------
138138
Temporary filename to which the non-watermarked file needs to be written.
139+
If no watermark configuration is present (neither in params nor in kwargs),
140+
yields the output file path directly without applying any watermark.
139141
140142
Example
141143
-------
@@ -151,8 +153,11 @@ def WatermarkedFile(output_file: Path | str, **kwargs):
151153

152154
watermark_config.update(kwargs)
153155

154-
with NamedTemporaryFile(suffix=output_file.suffix) as f:
155-
try:
156-
yield f.name
157-
finally:
158-
Watermarker.add_watermark(f.name, output_file, **watermark_config)
156+
if not watermark_config:
157+
yield output_file
158+
else:
159+
with NamedTemporaryFile(suffix=output_file.suffix) as f:
160+
try:
161+
yield f.name
162+
finally:
163+
Watermarker.add_watermark(f.name, output_file, **watermark_config)

tests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,21 @@ def test_watermarked_file(quarto_stage, tmp_path, format):
6464

6565
assert output_file.exists()
6666
assert output_file.stat().st_size > 0
67+
68+
69+
def test_watermarked_file_no_config(quarto_stage, tmp_path):
70+
"""Test that WatermarkedFile yields output path directly when no watermark config is present."""
71+
chdir(quarto_stage)
72+
# Ensure no watermark config is set
73+
dso.api.CONFIG.dso_config = {}
74+
75+
output_file = tmp_path / "output.png"
76+
77+
with WatermarkedFile(output_file) as f:
78+
# When no watermark config, should yield the output file path directly
79+
assert Path(f) == output_file
80+
img = Image.new("RGB", (100, 100), color=(73, 109, 137))
81+
img.save(f)
82+
83+
assert output_file.exists()
84+
assert output_file.stat().st_size > 0

0 commit comments

Comments
 (0)