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

Changed type of vp_sources property to set #406

Merged
merged 3 commits into from
Feb 23, 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ New features and improvements:
* The new `pens` command can apply a predefined or custom scheme on multiple layers at once. Two common schemes are built-in: `rgb` and `cmyk`. Custom schemes can be defined in the configuration file.
* The `show` and `write` commands were updated to take into account these new layer properties.

* The `read` command now records the source SVG paths in the `vp_source` and `vp_sources` system properties (see the [documentation](https://vpype.readthedocs.io/en/latest/fundamentals.html#system-properties)) (#397)
* The `read` command now records the source SVG paths in the `vp_source` and `vp_sources` system properties (see the [documentation](https://vpype.readthedocs.io/en/latest/fundamentals.html#system-properties)) (#397, #406)

* Added property substitution to CLI user input (#395)

Expand Down
4 changes: 2 additions & 2 deletions docs/fundamentals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Although there is in general no constraint on the number, name, and type of prop
* ``vp_name`` (:class:`str`): the name of a layer (layer property)
* ``vp_page_size`` (two-:class:`tuple` of :class:`float`): the page size (global property)
* ``vp_source`` (:class:`pathlib.Path`): the input file from which the geometries are created (global and/or layer property)
* ``vp_sources`` (:class:`list` of :class:`pathlib.Path`): list of all input files from which geometries are created (global property)
* ``vp_sources`` (:class:`set` of :class:`pathlib.Path`): list of all input files from which geometries are created (global property)

Many commands act on these properties. For example, the :ref:`cmd_read` command sets these properties according to the imported SVG file's content. The :ref:`cmd_color`, :ref:`cmd_penwidth`, :ref:`cmd_name`, and :ref:`cmd_pens` commands can set these properties to arbitrary values. In particular, the :ref:`cmd_pens` commands can apply a predefined set of values on multiple layers at once, for example to apply a CMYK color scheme (see :ref:`faq_custom_pen_config` for more information). The page size global property is set by the :ref:`cmd_pagesize` and :ref:`cmd_layout` commands, and used by the :ref:`cmd_write` command.

Expand All @@ -192,7 +192,7 @@ Many commands act on these properties. For example, the :ref:`cmd_read` command

The ``vp_source`` property contains the last input file used, and may be overwritten if subsequent file-based commands are used. For example, the :ref:`cmd_read` command stores the input SVG path in ``vp_source`` as layer property if the ``--layer`` option is used, or as global property otherwise. If multiple :ref:`cmd_read` commands are used, the last one may overwrite the value from the earlier ones.

To address this limitation, the :ref:`cmd_read` command also *appends* the input SVG path to the ``vp_sources`` global property. The ``vp_sources`` property therefore is a list of *all* source files involved. Third-party developers are strongly encouraged to implement a similar behavior in their file-based plug-ins.
To address this limitation, the :ref:`cmd_read` command also *appends* the input SVG path to the ``vp_sources`` global property. The ``vp_sources`` property therefore is a set of *all* source files involved. Third-party developers are strongly encouraged to implement a similar behavior in their file-based plug-ins.


SVG attributes properties
Expand Down
12 changes: 6 additions & 6 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,20 @@ def test_read_sets_source_properties():
test_file = TEST_FILE_DIRECTORY / "misc" / "multilayer.svg"
doc = vpype_cli.execute(f"read '{test_file}'")
assert doc.property(vp.METADATA_FIELD_SOURCE) == test_file
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == (test_file,)
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == {test_file}


def test_read_by_attrs_sets_source_properties():
test_file = TEST_FILE_DIRECTORY / "misc" / "multilayer.svg"
doc = vpype_cli.execute(f"read -a fill -a stroke '{test_file}'")
assert doc.property(vp.METADATA_FIELD_SOURCE) == test_file
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == (test_file,)
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == {test_file}


def test_read_single_layer_sets_source_properties():
test_file = TEST_FILE_DIRECTORY / "misc" / "multilayer.svg"
doc = vpype_cli.execute(f"read --layer 1 '{test_file}'")
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == (test_file,)
assert doc.property(vp.METADATA_FIELD_SOURCE_LIST) == {test_file}
assert len(doc.layers) == 1
assert doc.layers[1].property(vp.METADATA_FIELD_SOURCE) == test_file

Expand All @@ -461,7 +461,7 @@ def test_read_stdin_sets_source_properties(monkeypatch):

doc = vpype_cli.execute(f"read -")
assert vp.METADATA_FIELD_SOURCE not in doc.metadata
assert doc.sources == tuple()
assert doc.sources == set()


def test_read_single_layer_stdin_sets_source_properties(monkeypatch):
Expand All @@ -471,7 +471,7 @@ def test_read_single_layer_stdin_sets_source_properties(monkeypatch):
doc = vpype_cli.execute(f"read -l1 -")
assert vp.METADATA_FIELD_SOURCE not in doc.metadata
assert vp.METADATA_FIELD_SOURCE not in doc.layers[1].metadata
assert doc.sources == tuple()
assert doc.sources == set()


def test_read_by_attr_stdin_sets_source_properties(monkeypatch):
Expand All @@ -480,4 +480,4 @@ def test_read_by_attr_stdin_sets_source_properties(monkeypatch):

doc = vpype_cli.execute(f"read -a stroke -a fill -")
assert vp.METADATA_FIELD_SOURCE not in doc.metadata
assert doc.sources == tuple()
assert doc.sources == set()
2 changes: 1 addition & 1 deletion vpype/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __str__(self) -> str:
METADATA_FIELD_PEN_WIDTH: float,
METADATA_FIELD_PAGE_SIZE: tuple,
METADATA_FIELD_SOURCE: pathlib.Path,
METADATA_FIELD_SOURCE_LIST: tuple,
METADATA_FIELD_SOURCE_LIST: set,
}

# noinspection HttpUrlsUsage
Expand Down
22 changes: 17 additions & 5 deletions vpype/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
"""
import math
import pathlib
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple, Union, cast
from typing import (
Any,
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Set,
Tuple,
Union,
cast,
)

import numpy as np
from shapely.geometry import LinearRing, LineString, MultiLineString
Expand Down Expand Up @@ -600,11 +612,11 @@ def extend_page_size(self, page_size: Optional[Tuple[float, float]]) -> None:
self.page_size = page_size

@property
def sources(self) -> Tuple[pathlib.Path, ...]:
return self.metadata.get(METADATA_FIELD_SOURCE_LIST, tuple())
def sources(self) -> Set[pathlib.Path]:
return self.metadata.get(METADATA_FIELD_SOURCE_LIST, set())

@sources.setter
def sources(self, sources: Tuple[pathlib.Path, ...]) -> None:
def sources(self, sources: Set[pathlib.Path]) -> None:
self.set_property(METADATA_FIELD_SOURCE_LIST, sources)

def add_to_sources(self, path) -> None:
Expand All @@ -619,7 +631,7 @@ def add_to_sources(self, path) -> None:
try:
path = pathlib.Path(path)
if path.exists():
self.sources += (path,)
self.sources |= {path}
except TypeError:
pass

Expand Down