From 11ebcf1ef9dca01ef767834c094fc32bdd9f1391 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Thu, 31 Mar 2022 19:21:54 +0200 Subject: [PATCH] Fixed issue where `forlayer` would set `_n` improperly (#443) Fixes #434 --- CHANGELOG.md | 1 + tests/test_commands.py | 15 +++++++++++++++ vpype_cli/blocks.py | 5 +++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e4c84e2..b76a2379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Release date: UNRELEASED ### Bug fixes +* Fixed issue with `forlayer` where the `_n` variable was set improperly (#443) * 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) diff --git a/tests/test_commands.py b/tests/test_commands.py index 566c4c70..0640ee32 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -692,6 +692,21 @@ def test_forlayer_command_property_accessor(): assert doc.layers[i + 1].property("test2") == i +def test_forlayer_vars(): + vpype_cli.execute( + """ + repeat 5 + random -l new + end + eval 'cnt=0' + forlayer + eval 'assert _lid==cnt+1' + eval 'assert _i==cnt;cnt += 1' + eval 'assert _n==5' + end""" + ) + + def test_pagerotate(): doc = vpype_cli.execute("random pagesize a4 pagerotate") assert doc.page_size == pytest.approx((1122.5196850393702, 793.7007874015749)) diff --git a/vpype_cli/blocks.py b/vpype_cli/blocks.py index 077c6fda..074e7166 100644 --- a/vpype_cli/blocks.py +++ b/vpype_cli/blocks.py @@ -221,13 +221,14 @@ def forlayer(state: State, processors: Iterable[ProcessorType]) -> None: orig_doc = state.document new_doc: vp.Document = orig_doc.clone() - for i, lid in enumerate(list(orig_doc.layers)): + lids = list(orig_doc.layers) + for i, lid in enumerate(lids): with state.temp_document(keep_layer=False) as doc: doc.add(orig_doc.pop(lid), lid, with_metadata=True) variables = { "_lid": lid, "_i": i, - "_n": len(doc.layers), + "_n": len(lids), "_name": doc.layers[lid].property(vp.METADATA_FIELD_NAME) or "", "_color": doc.layers[lid].property(vp.METADATA_FIELD_COLOR), "_pen_width": doc.layers[lid].property(vp.METADATA_FIELD_PEN_WIDTH),