From 5ecea4bb91139a5e07938155810515d35670dbc0 Mon Sep 17 00:00:00 2001 From: snoyer Date: Mon, 30 Dec 2024 20:47:22 +0400 Subject: [PATCH 1/3] remove `is_inkscape_label` parameter --- src/build123d/importers.py | 15 ++++++--------- tests/test_importers.py | 8 ++------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/build123d/importers.py b/src/build123d/importers.py index b9f52998..1bb16939 100644 --- a/src/build123d/importers.py +++ b/src/build123d/importers.py @@ -31,6 +31,7 @@ import os from os import PathLike, fsdecode +import re import unicodedata from math import degrees from pathlib import Path @@ -337,7 +338,6 @@ def import_svg( flip_y: bool = True, ignore_visibility: bool = False, label_by: str = "id", - is_inkscape_label: bool = False, ) -> ShapeList[Union[Wire, Face]]: """import_svg @@ -345,10 +345,9 @@ def import_svg( svg_file (Union[str, Path, TextIO]): svg file flip_y (bool, optional): flip objects to compensate for svg orientation. Defaults to True. ignore_visibility (bool, optional): Defaults to False. - label_by (str, optional): xml attribute. Defaults to "id". - is_inkscape_label (bool, optional): flag to indicate that the attribute - is an Inkscape label like `inkscape:label` - label_by would be set to - `label` in this case. Defaults to False. + label_by (str, optional): XML attribute to use for imported shapes' `label` property. + Defaults to "id". + Use `inkscape:label` to read labels set from Inkscape's "Layers and Objects" panel. Raises: ValueError: unexpected shape type @@ -357,10 +356,8 @@ def import_svg( ShapeList[Union[Wire, Face]]: objects contained in svg """ shapes = [] - label_by = ( - "{http://www.inkscape.org/namespaces/inkscape}" + label_by - if is_inkscape_label - else label_by + label_by = re.sub( + r"^inkscape:(.+)", r"{http://www.inkscape.org/namespaces/inkscape}\1", label_by ) for face_or_wire, color_and_label in import_svg_document( svg_file, diff --git a/tests/test_importers.py b/tests/test_importers.py index 7c7e4a1d..3e1155b9 100644 --- a/tests/test_importers.py +++ b/tests/test_importers.py @@ -71,13 +71,9 @@ def test_import_svg_as_buildline_code_invalid_name(self): def test_import_svg(self): svg_file = Path(__file__).parent / "../tests/svg_import_test.svg" - for tag in ["id", "label"]: + for tag in ["id", "inkscape:label"]: # Import the svg object as a ShapeList - svg = import_svg( - svg_file, - label_by=tag, - is_inkscape_label=tag == "label", - ) + svg = import_svg(svg_file, label_by=tag) # Exact the shape of the plate & holes base_faces = svg.filter_by(lambda f: "base" in f.label) From e5c976454eadcaff2c923a930f10c8689cd54e41 Mon Sep 17 00:00:00 2001 From: snoyer Date: Tue, 31 Dec 2024 17:18:23 +0400 Subject: [PATCH 2/3] deprecate param without breaking API yet --- src/build123d/importers.py | 9 +++++++++ tests/test_importers.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/build123d/importers.py b/src/build123d/importers.py index 1bb16939..5d854554 100644 --- a/src/build123d/importers.py +++ b/src/build123d/importers.py @@ -36,6 +36,7 @@ from math import degrees from pathlib import Path from typing import Optional, TextIO, Union +import warnings from OCP.BRep import BRep_Builder from OCP.BRepGProp import BRepGProp @@ -338,6 +339,7 @@ def import_svg( flip_y: bool = True, ignore_visibility: bool = False, label_by: str = "id", + is_inkscape_label: bool | None = None, # TODO remove for `1.0` release ) -> ShapeList[Union[Wire, Face]]: """import_svg @@ -355,6 +357,13 @@ def import_svg( Returns: ShapeList[Union[Wire, Face]]: objects contained in svg """ + if is_inkscape_label is not None: # TODO remove for `1.0` release + msg = "`is_inkscape_label` parameter is deprecated" + if is_inkscape_label: + label_by = "inkscape:" + label_by + msg += f", use `label_by={label_by!r}` instead" + warnings.warn(msg, stacklevel=2) + shapes = [] label_by = re.sub( r"^inkscape:(.+)", r"{http://www.inkscape.org/namespaces/inkscape}\1", label_by diff --git a/tests/test_importers.py b/tests/test_importers.py index 3e1155b9..0fa7088d 100644 --- a/tests/test_importers.py +++ b/tests/test_importers.py @@ -84,6 +84,24 @@ def test_import_svg(self): self.assertEqual(len(list(hole_faces)), 2) self.assertEqual(len(list(test_wires)), 1) + def test_import_svg_deprecated_param(self): # TODO remove for `1.0` release + svg_file = Path(__file__).parent / "../tests/svg_import_test.svg" + + with self.assertWarns(UserWarning): + svg = import_svg(svg_file, label_by="label", is_inkscape_label=True) + + # Exact the shape of the plate & holes + base_faces = svg.filter_by(lambda f: "base" in f.label) + hole_faces = svg.filter_by(lambda f: "hole" in f.label) + test_wires = svg.filter_by(lambda f: "wire" in f.label) + + self.assertEqual(len(list(base_faces)), 1) + self.assertEqual(len(list(hole_faces)), 2) + self.assertEqual(len(list(test_wires)), 1) + + with self.assertWarns(UserWarning): + svg = import_svg(svg_file, is_inkscape_label=False) + def test_import_svg_colors(self): svg_file = StringIO( '' From 393d64ea3fe8efbd1a5b06eca6ce615865fad070 Mon Sep 17 00:00:00 2001 From: snoyer Date: Tue, 14 Jan 2025 20:08:15 +0400 Subject: [PATCH 3/3] add typing for common param values --- src/build123d/importers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/build123d/importers.py b/src/build123d/importers.py index 364b62bb..49793069 100644 --- a/src/build123d/importers.py +++ b/src/build123d/importers.py @@ -35,7 +35,7 @@ import unicodedata from math import degrees from pathlib import Path -from typing import Optional, TextIO, Union +from typing import Literal, Optional, TextIO, Union import warnings from OCP.BRep import BRep_Builder @@ -338,7 +338,7 @@ def import_svg( *, flip_y: bool = True, ignore_visibility: bool = False, - label_by: str = "id", + label_by: Literal["id", "class", "inkscape:label"] | str = "id", is_inkscape_label: bool | None = None, # TODO remove for `1.0` release ) -> ShapeList[Wire | Face]: """import_svg