Repository: abey79/vpype

Issue:
document.add_to_sources() should also set VP_SOURCE



Discussion:

Resolving patch:
diff --git a/vpype/io.py b/vpype/io.py
--- 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
--- 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
--- 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(


Test patch:
diff --git a/tests/test_model.py b/tests/test_model.py
--- 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)