Skip to content

Commit

Permalink
Fixed Document.add_to_sources() to also set vp_source (#431)
Browse files Browse the repository at this point in the history
This makes it a more useful API for plug-ins. Also, `ValueError` is no longer silenced.

Fixes #412
  • Loading branch information
abey79 authored Mar 27, 2022
1 parent b3907ed commit 5992728
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from typing import Iterable, Sequence

import numpy as np
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions vpype/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions vpype_cli/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 5992728

Please sign in to comment.