Skip to content

Commit

Permalink
- Swapped click.Choice for ChoiceType for --orientation option …
Browse files Browse the repository at this point in the history
…of `pagerotate` command, per @abey79

- Added new tests for `pagerotate --orientation`
- Updated CHANGELOG.md
  • Loading branch information
gatesphere committed Mar 7, 2024
1 parent c0f98c0 commit 635836e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Release date: UNRELEASED

### New features and improvements

* ...
* Added a `--orientation` option to the `pagerotate` command to conditionally rotate the page to a target orientation (thanks to @gatesphere) (#705)

### Bug fixes

Expand Down
22 changes: 22 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,28 @@ def test_pagerotate_error(caplog):
assert "page size is not defined, page not rotated" in caplog.text


def test_pagerotate_orientation():
doc = vpype_cli.execute("random pagesize a4 pagerotate -o landscape")
assert doc.page_size == pytest.approx((1122.5196850393702, 793.7007874015749))

doc = vpype_cli.execute("random pagesize a4 pagerotate -cw -o landscape")
assert doc.page_size == pytest.approx((1122.5196850393702, 793.7007874015749))

doc = vpype_cli.execute("random pagesize --landscape a4 pagerotate -o portrait")
assert doc.page_size == pytest.approx((793.7007874015749, 1122.5196850393702))

doc = vpype_cli.execute("random pagesize --landscape a4 pagerotate -cw -o portrait")
assert doc.page_size == pytest.approx((793.7007874015749, 1122.5196850393702))


def test_pagerotate_orientation_error():
doc = vpype_cli.execute("random pagesize a4 pagerotate -o portrait")
assert doc.page_size == pytest.approx((793.7007874015749, 1122.5196850393702))

doc = vpype_cli.execute("random pagesize --landscape a4 pagerotate -o landscape")
assert doc.page_size == pytest.approx((1122.5196850393702, 793.7007874015749))


def test_help(runner):
res = runner.invoke(cli, "--help")

Expand Down
16 changes: 11 additions & 5 deletions vpype_cli/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

from .cli import cli
from .decorators import global_processor, layer_processor
from .types import IntegerType, LayerType, LengthType, PageSizeType, multiple_to_layer_ids
from .types import (
ChoiceType,
IntegerType,
LayerType,
LengthType,
PageSizeType,
multiple_to_layer_ids,
)

__all__ = (
"crop",
Expand Down Expand Up @@ -665,12 +672,11 @@ def layout(
@click.option(
"--orientation",
"-o",
type=click.Choice(["any", "portrait", "landscape"]),
default="any",
type=ChoiceType(["portrait", "landscape"]),
help="Conditionally rotate only if the final orientation matches this option",
)
@global_processor
def pagerotate(document: vp.Document, clockwise: bool, orientation: str):
def pagerotate(document: vp.Document, clockwise: bool, orientation: str | None):
"""Rotate the page by 90 degrees.
This command rotates the page by 90 degrees counter-clockwise. If the `--clockwise` option
Expand All @@ -689,7 +695,7 @@ def pagerotate(document: vp.Document, clockwise: bool, orientation: str):
# check orientation constraint, and do nothing if target orientation
# won't match desired result
if (orientation == "portrait" and h > w) or (orientation == "landscape" and w > h):
logging.warning("pagerotate: page already in target orientation, page not rotated.")
logging.debug("pagerotate: page already in target orientation, page not rotated")
return document

if clockwise:
Expand Down

0 comments on commit 635836e

Please sign in to comment.