diff options
author | James R. Barlow <james@purplerock.ca> | 2021-03-20 15:02:26 -0700 |
---|---|---|
committer | James R. Barlow <james@purplerock.ca> | 2021-03-20 15:02:26 -0700 |
commit | 1ed9fc1395febea0a4c023bc738b863316af117d (patch) | |
tree | d3d2dfcf00a69a3d48090d989c2678e5c8f92b96 | |
parent | 9177503fe5fc73947241524bd24ff83ae58a132e (diff) |
make_page_destination: make kwargs explicit
And adjust tests for coverage.
-rw-r--r-- | src/pikepdf/models/outlines.py | 35 | ||||
-rw-r--r-- | tests/test_outlines.py | 8 |
2 files changed, 39 insertions, 4 deletions
diff --git a/src/pikepdf/models/outlines.py b/src/pikepdf/models/outlines.py index 8a81e17..4dd55d2 100644 --- a/src/pikepdf/models/outlines.py +++ b/src/pikepdf/models/outlines.py @@ -39,7 +39,12 @@ def make_page_destination( pdf: Pdf, page_num: int, page_location: Optional[Union[PageLocation, str]] = None, - **kwargs, + *, + left: Optional[float] = None, + top: Optional[float] = None, + right: Optional[float] = None, + bottom: Optional[float] = None, + zoom: Optional[float] = None, ) -> Array: """ Creates a destination ``Array`` with reference to a Pdf document's page number. @@ -48,8 +53,30 @@ def make_page_destination( pdf: PDF document object. page_num: Page number (zero-based). page_location: Optional page location, as a string or :enum:`PageLocation`. - kwargs: Optional keyword arguments for the page location, e.g. ``top``. + left, top, right, bottom, zoom: Optional keyword arguments for specifying + a position on the page, used in conjunction with the page fit style + specified by *page_location*. """ + return _make_page_destination( + pdf, + page_num, + page_location=page_location, + left=left, + top=top, + right=right, + bottom=bottom, + zoom=zoom, + ) + + +def _make_page_destination( + pdf: Pdf, + page_num: int, + page_location: Optional[Union[PageLocation, str]] = None, + **kwargs, +) -> Array: + kwargs = {k: v for k, v in kwargs.items() if v is not None} + res = [pdf.pages[page_num]] if page_location: if isinstance(page_location, PageLocation): @@ -60,7 +87,9 @@ def make_page_destination( try: loc_key = PageLocation[loc_str] except KeyError: - raise ValueError(f"Invalid or unsupported page location type {loc_str}") + raise ValueError( + f"Invalid or unsupported page location type {loc_str}" + ) from None res.append(Name(f'/{loc_str}')) dest_arg_names = PAGE_LOCATION_ARGS.get(loc_key) if dest_arg_names: diff --git a/tests/test_outlines.py b/tests/test_outlines.py index 9823668..e606cea 100644 --- a/tests/test_outlines.py +++ b/tests/test_outlines.py @@ -390,7 +390,7 @@ def test_dest_or_action(outlines_doc): @settings(deadline=timedelta(milliseconds=750)) @given( page_num=st.integers(0, 1), - page_loc=st.sampled_from(PageLocation), + page_loc=st.sampled_from(list(PageLocation) + ['invalid']), kwargs=st.dictionaries( st.sampled_from(list(sorted(ALL_PAGE_LOCATION_KWARGS))), st.integers(0, 10000) ), @@ -405,6 +405,12 @@ def test_page_destination(resources, page_num, page_loc, kwargs): # fail with Pdf.open(resources / 'outlines.pdf') as doc: page_ref = doc.pages[page_num] + + if page_loc == 'invalid': + with pytest.raises(ValueError, match='unsupported page location'): + make_page_destination(doc, page_num, page_loc, **kwargs) + return + dest = make_page_destination(doc, page_num, page_loc, **kwargs) if isinstance(page_loc, PageLocation): loc_str = page_loc.name |