Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add float, int range and choice types to vpype_cli API #430

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ Release date: UNRELEASED

* Added HPGL configuration for the Calcomp Artisan plotter (thanks to Andee Collard and @ithinkido) (#418)


### Bug fixes

* Fixed issue with `write` where layer opacity was included in the `stroke` attribute instead of using `stroke-opacity`, which, although compliant, was not compatible with Inkscape (#429)


### API changes

* Added `vpype_cli.FloatType()`, `vpype_cli.IntRangeType()`, and `vpype_cli.ChoiceType()` (#430)


### Other changes

* Added support for Python 3.10 and dropped support for Python 3.7 (#417)
Expand Down
49 changes: 49 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import click
import pytest

import vpype as vp
import vpype_cli


def test_float_type():
@vpype_cli.cli.command(name="floatcmd")
@click.argument("arg", type=vpype_cli.FloatType())
@vpype_cli.generator
def cmd(arg: float) -> vp.LineCollection:
assert type(arg) is float
return vp.LineCollection()

vpype_cli.execute("floatcmd 3.5")
with pytest.raises(click.BadParameter):
vpype_cli.execute("floatcmd hello")


def test_int_range_type():
@vpype_cli.cli.command(name="intrangecmd")
@click.argument("arg", type=vpype_cli.IntRangeType(min=10, max=14))
@vpype_cli.generator
def cmd(arg: int) -> vp.LineCollection:
assert type(arg) is int
assert 10 <= arg <= 14
return vp.LineCollection()

vpype_cli.execute("intrangecmd 10")
vpype_cli.execute("intrangecmd 14")
with pytest.raises(click.BadParameter):
vpype_cli.execute("intrangecmd 12.5")
with pytest.raises(click.BadParameter):
vpype_cli.execute("intrangecmd 9")


def test_choice_type():
@vpype_cli.cli.command(name="choicecmd")
@click.argument("arg", type=vpype_cli.ChoiceType(choices=["yes", "no"]))
@vpype_cli.generator
def cmd(arg: str) -> vp.LineCollection:
assert arg in ["yes", "no"]
return vp.LineCollection()

vpype_cli.execute("choicecmd yes")
vpype_cli.execute("choicecmd no")
with pytest.raises(click.BadParameter):
vpype_cli.execute("choicecmd maybe")
5 changes: 4 additions & 1 deletion vpype_cli/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ def preprocess_argument(self, arg: Any) -> Any:
if isinstance(arg, tuple):
return tuple(self.preprocess_argument(item) for item in arg)
else:
return arg.evaluate(self) if isinstance(arg, _DeferredEvaluator) else arg
try:
return arg.evaluate(self) if isinstance(arg, _DeferredEvaluator) else arg
except Exception as exc:
raise click.BadParameter(str(exc)) from exc

def preprocess_arguments(
self, args: tuple[Any, ...], kwargs: dict[str, Any]
Expand Down
36 changes: 36 additions & 0 deletions vpype_cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ def evaluate(self, state: State) -> int:
_evaluator_class = _IntegerDeferredEvaluator


class FloatType(_DeferredEvaluatorType):
""":class:`click.ParamType` sub-class to automatically perform
:ref:`property substitution <fundamentals_property_substitution>` on user input.

Example::

>>> import click
>>> import vpype_cli
>>> import vpype
>>> @vpype_cli.cli.command(group="my commands")
... @click.argument("number", type=vpype_cli.FloatType())
... @vpype_cli.generator
... def my_command(number: float):
... pass
"""

class _FloatDeferredEvaluator(_DeferredEvaluator):
def evaluate(self, state: State) -> float:
return float(state.substitute(self._text))

name = "number"
_evaluator_class = _FloatDeferredEvaluator


class LengthType(_DeferredEvaluatorType):
""":class:`click.ParamType` sub-class to automatically converts a user-provided lengths
into CSS pixel units.
Expand Down Expand Up @@ -215,6 +239,18 @@ class FileType(_DelegatedDeferredEvaluatorType):
_delegate_class = click.File


class IntRangeType(_DelegatedDeferredEvaluatorType):
""":class:`click.File` clone which performs substitution on input."""

name = "float"
_delegate_class = click.IntRange


class ChoiceType(_DelegatedDeferredEvaluatorType):
name = "choice"
_delegate_class = click.Choice


def multiple_to_layer_ids(layers: int | list[int] | None, document: vp.Document) -> list[int]:
"""Convert multiple-layer CLI argument to list of layer IDs.

Expand Down