From 5992728392db8b987284bb0cbf5166c24a531bfc Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Sun, 27 Mar 2022 21:14:50 +0200 Subject: [PATCH] Fixed `Document.add_to_sources()` to also set `vp_source` (#431) This makes it a more useful API for plug-ins. Also, `ValueError` is no longer silenced. Fixes #412 --- CHANGELOG.md | 1 + tests/test_model.py | 21 +++++++++++++++++++++ vpype/io.py | 2 -- vpype/model.py | 20 +++++++++----------- vpype_cli/read.py | 8 ++++++-- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2062c342..6b975f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Release date: UNRELEASED ### API changes * Added `vpype_cli.FloatType()`, `vpype_cli.IntRangeType()`, and `vpype_cli.ChoiceType()` (#430) +* Changed `vpype.Document.add_to_sources()` to also modify the `vp_source` property (#431) ### Other changes diff --git a/tests/test_model.py b/tests/test_model.py index e03321b2..0ce785ec 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,5 +1,6 @@ from __future__ import annotations +import sys from typing import Iterable, Sequence import numpy as np @@ -332,3 +333,23 @@ def test_document_exists_none(): assert doc.exists(1) assert not doc.exists(2) assert not doc.exists(None) + + +def test_document_add_to_sources(tmp_path): + path = tmp_path / "some_file.txt" + path.touch() + + doc = Document() + doc.add(LineCollection([[0, 1 + 1j], [2 + 2j, 3 + 3j, 4 + 4j]]), 1) + + doc.add_to_sources(path) + assert path in doc.property(vp.METADATA_FIELD_SOURCE_LIST) + assert path == doc.property(vp.METADATA_FIELD_SOURCE) + + path2 = tmp_path / "doest_exist.txt" + doc.add_to_sources(path2) + assert path2 not in doc.property(vp.METADATA_FIELD_SOURCE_LIST) + assert path2 != doc.property(vp.METADATA_FIELD_SOURCE) + + with pytest.raises(Exception): + doc.add_to_sources(sys.stdin) diff --git a/vpype/io.py b/vpype/io.py index c7be4945..63906640 100644 --- a/vpype/io.py +++ b/vpype/io.py @@ -522,7 +522,6 @@ def _find_groups(group: svgelements.Group) -> Iterator[svgelements.Group]: source = _get_source(file) if source: - document.set_property(METADATA_FIELD_SOURCE, source) document.add_to_sources(source) return document @@ -586,7 +585,6 @@ def read_svg_by_attributes( source = _get_source(file) if source: - document.set_property(METADATA_FIELD_SOURCE, source) document.add_to_sources(source) return document diff --git a/vpype/model.py b/vpype/model.py index 8ffefab1..9daddc0c 100644 --- a/vpype/model.py +++ b/vpype/model.py @@ -13,6 +13,7 @@ from .line_index import LineIndex from .metadata import ( METADATA_FIELD_PAGE_SIZE, + METADATA_FIELD_SOURCE, METADATA_FIELD_SOURCE_LIST, METADATA_SYSTEM_FIELD_TYPES, ) @@ -610,18 +611,16 @@ def sources(self, sources: set[pathlib.Path]) -> None: def add_to_sources(self, path) -> None: """Add a path to the source list. - If ``path`` cannot be converted to a :class:`pathlib.Path` or the file doesn't exist, - it is ignored and not added to the source list. + This function sets the `vp_source` property to provided path and adds it to the + `vp_sources` property. Args: path: file path """ - try: - path = pathlib.Path(path) - if path.exists(): - self.sources |= {path} - except TypeError: - pass + if (path := pathlib.Path(path)).exists(): + path = path.absolute() + self.set_property(METADATA_FIELD_SOURCE, path) + self.sources |= {path} def clear_layer_metadata(self) -> None: """Clear all metadata from the document.""" @@ -789,8 +788,7 @@ def extend(self, doc: Document) -> None: # special treatment for page size and source list self.extend_page_size(doc.page_size) - for path in doc.sources: - self.add_to_sources(path) + self.sources |= doc.sources self.metadata.update( { k: v @@ -869,7 +867,7 @@ def rotate(self, angle: float) -> None: layer.rotate(angle) def bounds( - self, layer_ids: None | Iterable[int] = None + self, layer_ids: Iterable[int] | None = None ) -> tuple[float, float, float, float] | None: """Compute bounds of the document. diff --git a/vpype_cli/read.py b/vpype_cli/read.py index b2f4f38c..fbd9252a 100644 --- a/vpype_cli/read.py +++ b/vpype_cli/read.py @@ -192,7 +192,8 @@ def read( if file == "-": file = sys.stdin - elif not pathlib.Path(file).is_file(): + path: pathlib.Path | None = None + elif not (path := pathlib.Path(file)).is_file(): if no_fail: logging.debug("read: file doesn't exist, ignoring due to `--no-fail`") return document @@ -219,7 +220,10 @@ def read( document.add(lc, single_to_layer_id(layer, document), with_metadata=True) document.extend_page_size((width, height)) - document.add_to_sources(file) + + # vp_source is not set here, as it becomes a layer property + if path: + document.sources |= {path.absolute()} else: if len(attr) == 0: doc = vp.read_multilayer_svg(