From ce43c75cd8f5ce13a446a014fbbaec9b1df73296 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 13:09:13 +0200 Subject: [PATCH 01/13] Allow file-like object as input to Documents ctor and svg2paths function --- svgpathtools/document.py | 22 ++++++++++++++-------- svgpathtools/svg_to_paths.py | 20 +++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 1dd9077..8371c85 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -224,7 +224,7 @@ def flattened_paths_from_group(group_to_flatten, root, recursive=True, class Document: - def __init__(self, filepath=None): + def __init__(self, svg_file_name_or_file=None): """A container for a DOM-style SVG document. The `Document` class provides a simple interface to modify and analyze @@ -235,19 +235,25 @@ class Document: The output Path objects will be transformed based on their parent groups. Args: - filepath (str): The filepath of the DOM-style object. + svg_file_name_or_file (str or file-like): The filepath of the + DOM-style object or a file-like object containing it. """ + self.original_filepath = None - # remember location of original svg file - self.original_filepath = filepath - if filepath is not None and os.path.dirname(filepath) == '': - self.original_filepath = os.path.join(os.getcwd(), filepath) + # strings are interpreted as file location everything else is treated as + # file-like object and passed to the xml parser directly + if isinstance(svg_file_name_or_file, str): + # remember location of original svg file if any + self.original_filepath = svg_file_name_or_file + if os.path.dirname(svg_file_name_or_file) == '': + self.original_filepath = os.path.join( + os.getcwd(), svg_file_name_or_file) - if filepath is None: + if svg_file_name_or_file is None: self.tree = etree.ElementTree(Element('svg')) else: # parse svg to ElementTree object - self.tree = etree.parse(filepath) + self.tree = etree.parse(svg_file_name_or_file) self.root = self.tree.getroot() diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index f4ca07b..a3a68c9 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -129,7 +129,7 @@ def line2pathd(l): ) -def svg2paths(svg_file_location, +def svg2paths(svg_file_name_or_file, return_svg_attributes=False, convert_circles_to_paths=True, convert_ellipses_to_paths=True, @@ -144,7 +144,9 @@ def svg2paths(svg_file_location, SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements. Args: - svg_file_location (string): the location of the svg file + svg_file_name_or_file (string or file-like object): the location of the + svg file on disk or a file-like object containing the content of a + svg file return_svg_attributes (bool): Set to True and a dictionary of svg-attributes will be extracted and returned. See also the `svg2paths2()` function. @@ -168,10 +170,14 @@ def svg2paths(svg_file_location, list: The list of corresponding path attribute dictionaries. dict (optional): A dictionary of svg-attributes (see `svg2paths2()`). """ - if os_path.dirname(svg_file_location) == '': - svg_file_location = os_path.join(getcwd(), svg_file_location) + # strings are interpreted as file location everything else is treated as + # file-like object and passed to the xml parser directly + if isinstance(svg_file_name_or_file, str): + if os_path.dirname(svg_file_name_or_file) == '': + svg_file_name_or_file = os_path.join( + getcwd(), svg_file_name_or_file) - doc = parse(svg_file_location) + doc = parse(svg_file_name_or_file) def dom2dict(element): """Converts DOM elements to dictionaries of attributes.""" @@ -230,7 +236,7 @@ def svg2paths(svg_file_location, return path_list, attribute_dictionary_list -def svg2paths2(svg_file_location, +def svg2paths2(svg_file_name_or_file, return_svg_attributes=True, convert_circles_to_paths=True, convert_ellipses_to_paths=True, @@ -241,7 +247,7 @@ def svg2paths2(svg_file_location, """Convenience function; identical to svg2paths() except that return_svg_attributes=True by default. See svg2paths() docstring for more info.""" - return svg2paths(svg_file_location=svg_file_location, + return svg2paths(svg_file_name_or_file=svg_file_name_or_file, return_svg_attributes=return_svg_attributes, convert_circles_to_paths=convert_circles_to_paths, convert_ellipses_to_paths=convert_ellipses_to_paths, From ccdd10212c64d06970975467a9eadf1b388c3f24 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 13:32:09 +0200 Subject: [PATCH 02/13] Add unit tests for reading from different sources in svg2paths --- test/test_svg2paths.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 5a0dab0..b5cf5b4 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -1,6 +1,7 @@ from __future__ import division, absolute_import, print_function import unittest from svgpathtools import * +from io import StringIO from os.path import join, dirname from svgpathtools.svg_to_paths import rect2pathd @@ -57,4 +58,33 @@ class TestSVG2Paths(unittest.TestCase): non_rounded = {"x":"10", "y":"10", "width":"100","height":"100"} self.assertEqual(rect2pathd(non_rounded), 'M10.0 10.0 L 110.0 10.0 L 110.0 110.0 L 10.0 110.0 z') rounded = {"x":"10", "y":"10", "width":"100","height":"100", "rx":"15", "ry": "12"} - self.assertEqual(rect2pathd(rounded), "M 25.0 10.0 L 95.0 10.0 A 15.0 12.0 0 0 1 110.0 22.0 L 110.0 98.0 A 15.0 12.0 0 0 1 95.0 110.0 L 25.0 110.0 A 15.0 12.0 0 0 1 10.0 98.0 L 10.0 22.0 A 15.0 12.0 0 0 1 25.0 10.0 z") \ No newline at end of file + self.assertEqual(rect2pathd(rounded), "M 25.0 10.0 L 95.0 10.0 A 15.0 12.0 0 0 1 110.0 22.0 L 110.0 98.0 A 15.0 12.0 0 0 1 95.0 110.0 L 25.0 110.0 A 15.0 12.0 0 0 1 10.0 98.0 L 10.0 22.0 A 15.0 12.0 0 0 1 25.0 10.0 z") + + def test_from_file_path(self): + """ Test reading svg from file provided as path """ + paths, _ = svg2paths(join(dirname(__file__), 'polygons.svg')) + + self.assertEqual(len(paths), 2) + + def test_from_file_object(self): + """ Test reading svg from file object that has already been opened """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + paths, _ = svg2paths(file) + + self.assertEqual(len(paths), 2) + + def test_from_stringio(self): + """ Test reading svg object contained in an StringIO object """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + # prepare stringio object + file_as_stringio = StringIO() + # paste file content into it + file_as_stringio.write(file_content) + # reset curser to its beginning + file_as_stringio.seek(0) + + paths, _ = svg2paths(file_as_stringio) + + self.assertEqual(len(paths), 2) From 50b335f3da4d9f6ea06568bb22f0e8d5bb9632d7 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 14:37:27 +0200 Subject: [PATCH 03/13] Add convenience functions for converting svgs contained in a string to paths --- svgpathtools/__init__.py | 2 +- svgpathtools/svg_to_paths.py | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/svgpathtools/__init__.py b/svgpathtools/__init__.py index f043731..91f1afb 100644 --- a/svgpathtools/__init__.py +++ b/svgpathtools/__init__.py @@ -17,6 +17,6 @@ from .document import (Document, CONVERSIONS, CONVERT_ONLY_PATHS, from .svg_io_sax import SaxDocument try: - from .svg_to_paths import svg2paths, svg2paths2 + from .svg_to_paths import svg2paths, svg2paths2, svg_string2paths, svg_string2paths2 except ImportError: pass diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index a3a68c9..f8d5d88 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -5,6 +5,7 @@ The main tool being the svg2paths() function.""" from __future__ import division, absolute_import, print_function from xml.dom.minidom import parse from os import path as os_path, getcwd +from io import StringIO import re # Internal dependencies @@ -255,3 +256,49 @@ def svg2paths2(svg_file_name_or_file, convert_polylines_to_paths=convert_polylines_to_paths, convert_polygons_to_paths=convert_polygons_to_paths, convert_rectangles_to_paths=convert_rectangles_to_paths) + + +def svg_string2paths(svg_string, + return_svg_attributes=False, + convert_circles_to_paths=True, + convert_ellipses_to_paths=True, + convert_lines_to_paths=True, + convert_polylines_to_paths=True, + convert_polygons_to_paths=True, + convert_rectangles_to_paths=True): + """Convenience function; identical to svg2paths() except that it takes the + svg object as string. See svg2paths() docstring for more + info.""" + # wrap string into StringIO object + svg_file_obj = StringIO(svg_string) + # reset cursor to the beginning of the buffer + svg_file_obj.seek(0) + return svg2paths(svg_file_name_or_file=svg_file_obj, + return_svg_attributes=return_svg_attributes, + convert_circles_to_paths=convert_circles_to_paths, + convert_ellipses_to_paths=convert_ellipses_to_paths, + convert_lines_to_paths=convert_lines_to_paths, + convert_polylines_to_paths=convert_polylines_to_paths, + convert_polygons_to_paths=convert_polygons_to_paths, + convert_rectangles_to_paths=convert_rectangles_to_paths) + + +def svg_string2paths2(svg_string, + return_svg_attributes=True, + convert_circles_to_paths=True, + convert_ellipses_to_paths=True, + convert_lines_to_paths=True, + convert_polylines_to_paths=True, + convert_polygons_to_paths=True, + convert_rectangles_to_paths=True): + """Convenience function; identical to svg2paths2() except that it takes the + svg object as string. See svg2paths() docstring for more + info.""" + return svg_string2paths(svg_string=svg_string, + return_svg_attributes=return_svg_attributes, + convert_circles_to_paths=convert_circles_to_paths, + convert_ellipses_to_paths=convert_ellipses_to_paths, + convert_lines_to_paths=convert_lines_to_paths, + convert_polylines_to_paths=convert_polylines_to_paths, + convert_polygons_to_paths=convert_polygons_to_paths, + convert_rectangles_to_paths=convert_rectangles_to_paths) From 33f4639bbf2f526f6a0dade6d351cb91f129cffd Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 14:51:16 +0200 Subject: [PATCH 04/13] Add tests for functions taking svg objects as string --- test/test_svg2paths.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index b5cf5b4..06220bb 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -74,7 +74,7 @@ class TestSVG2Paths(unittest.TestCase): self.assertEqual(len(paths), 2) def test_from_stringio(self): - """ Test reading svg object contained in an StringIO object """ + """ Test reading svg object contained in a StringIO object """ with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string file_content: str = file.read() @@ -88,3 +88,23 @@ class TestSVG2Paths(unittest.TestCase): paths, _ = svg2paths(file_as_stringio) self.assertEqual(len(paths), 2) + + def test_from_string_without_svg_attrs(self): + """ Test reading svg object contained in a string without svg attributes""" + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + + paths, _ = svg_string2paths(file_content) + + self.assertEqual(len(paths), 2) + + def test_from_string_with_svg_attrs(self): + """ Test reading svg object contained in a string with svg attributes""" + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + + paths, _, _ = svg_string2paths2(file_content) + + self.assertEqual(len(paths), 2) From 1771fbfb06e3a1ec92628859d2bb2c9dec85adf4 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 15:30:48 +0200 Subject: [PATCH 05/13] Add factory method for creating from string holding svg object --- svgpathtools/document.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 8371c85..3d2b7c1 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -41,6 +41,7 @@ import xml.etree.ElementTree as etree from xml.etree.ElementTree import Element, SubElement, register_namespace from xml.dom.minidom import parseString import warnings +from io import StringIO from tempfile import gettempdir from time import time @@ -257,6 +258,18 @@ class Document: self.root = self.tree.getroot() + @staticmethod + def from_svg_string(svg_string): + """Factory method for creating a document from a string holding a svg + object + """ + # wrap string into StringIO object + svg_file_obj = StringIO(svg_string) + # reset cursor to the beginning of the buffer + svg_file_obj.seek(0) + # create document from file object + return Document(svg_file_obj) + def paths(self, group_filter=lambda x: True, path_filter=lambda x: True, path_conversions=CONVERSIONS): """Returns a list of all paths in the document. From a743e0293c3ca04e7b5e4cdb7059085e2b4fdb6c Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 15:46:56 +0200 Subject: [PATCH 06/13] Add tests for creating from file location, file, StringIO, and string --- test/test_document.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/test_document.py diff --git a/test/test_document.py b/test/test_document.py new file mode 100644 index 0000000..f1004bb --- /dev/null +++ b/test/test_document.py @@ -0,0 +1,45 @@ +from __future__ import division, absolute_import, print_function +import unittest +from svgpathtools import * +from io import StringIO +from os.path import join, dirname + +class TestDocument(unittest.TestCase): + def test_from_file_path(self): + """ Test reading svg from file provided as path """ + doc = Document(join(dirname(__file__), 'polygons.svg')) + + self.assertEqual(len(doc.paths()), 2) + + def test_from_file_object(self): + """ Test reading svg from file object that has already been opened """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + doc = Document(file) + + self.assertEqual(len(doc.paths()), 2) + + def test_from_stringio(self): + """ Test reading svg object contained in a StringIO object """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + # prepare stringio object + file_as_stringio = StringIO() + # paste file content into it + file_as_stringio.write(file_content) + # reset curser to its beginning + file_as_stringio.seek(0) + + doc = Document(file_as_stringio) + + self.assertEqual(len(doc.paths()), 2) + + def test_from_string_without_svg_attrs(self): + """ Test reading svg object contained in a string without svg attributes""" + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + + doc = Document.from_svg_string(file_content) + + self.assertEqual(len(doc.paths()), 2) From 68e0d1f30d34f2efc95ed95c11927461852c7956 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 15:51:03 +0200 Subject: [PATCH 07/13] Fix tests for old python versions not supporting type hints --- test/test_document.py | 4 ++-- test/test_svg2paths.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_document.py b/test/test_document.py index f1004bb..6b4a795 100644 --- a/test/test_document.py +++ b/test/test_document.py @@ -22,7 +22,7 @@ class TestDocument(unittest.TestCase): """ Test reading svg object contained in a StringIO object """ with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string - file_content: str = file.read() + file_content = file.read() # prepare stringio object file_as_stringio = StringIO() # paste file content into it @@ -38,7 +38,7 @@ class TestDocument(unittest.TestCase): """ Test reading svg object contained in a string without svg attributes""" with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string - file_content: str = file.read() + file_content = file.read() doc = Document.from_svg_string(file_content) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 06220bb..8944586 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -77,7 +77,7 @@ class TestSVG2Paths(unittest.TestCase): """ Test reading svg object contained in a StringIO object """ with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string - file_content: str = file.read() + file_content = file.read() # prepare stringio object file_as_stringio = StringIO() # paste file content into it @@ -93,7 +93,7 @@ class TestSVG2Paths(unittest.TestCase): """ Test reading svg object contained in a string without svg attributes""" with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string - file_content: str = file.read() + file_content = file.read() paths, _ = svg_string2paths(file_content) @@ -103,7 +103,7 @@ class TestSVG2Paths(unittest.TestCase): """ Test reading svg object contained in a string with svg attributes""" with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: # read entire file into string - file_content: str = file.read() + file_content = file.read() paths, _, _ = svg_string2paths2(file_content) From 02a223c2202284456eccb74ddea6f760e24a24ed Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 16:17:43 +0200 Subject: [PATCH 08/13] Fix fileio for test compatibility with python2.7 --- test/test_document.py | 11 +++++++---- test/test_svg2paths.py | 10 +++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/test/test_document.py b/test/test_document.py index 6b4a795..8837e8b 100644 --- a/test/test_document.py +++ b/test/test_document.py @@ -2,6 +2,7 @@ from __future__ import division, absolute_import, print_function import unittest from svgpathtools import * from io import StringIO +from io import open # overrides build-in open for compatibility with python2 from os.path import join, dirname class TestDocument(unittest.TestCase): @@ -20,7 +21,8 @@ class TestDocument(unittest.TestCase): def test_from_stringio(self): """ Test reading svg object contained in a StringIO object """ - with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + with open(join(dirname(__file__), 'polygons.svg'), + 'r', encoding='utf-8') as file: # read entire file into string file_content = file.read() # prepare stringio object @@ -34,9 +36,10 @@ class TestDocument(unittest.TestCase): self.assertEqual(len(doc.paths()), 2) - def test_from_string_without_svg_attrs(self): - """ Test reading svg object contained in a string without svg attributes""" - with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + def test_from_string(self): + """ Test reading svg object contained in a string""" + with open(join(dirname(__file__), 'polygons.svg'), + 'r', encoding='utf-8') as file: # read entire file into string file_content = file.read() diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 8944586..1a48bd7 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -2,6 +2,7 @@ from __future__ import division, absolute_import, print_function import unittest from svgpathtools import * from io import StringIO +from io import open # overrides build-in open for compatibility with python2 from os.path import join, dirname from svgpathtools.svg_to_paths import rect2pathd @@ -75,7 +76,8 @@ class TestSVG2Paths(unittest.TestCase): def test_from_stringio(self): """ Test reading svg object contained in a StringIO object """ - with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + with open(join(dirname(__file__), 'polygons.svg'), + 'r', encoding='utf-8') as file: # read entire file into string file_content = file.read() # prepare stringio object @@ -91,7 +93,8 @@ class TestSVG2Paths(unittest.TestCase): def test_from_string_without_svg_attrs(self): """ Test reading svg object contained in a string without svg attributes""" - with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + with open(join(dirname(__file__), 'polygons.svg'), + 'r', encoding='utf-8') as file: # read entire file into string file_content = file.read() @@ -101,7 +104,8 @@ class TestSVG2Paths(unittest.TestCase): def test_from_string_with_svg_attrs(self): """ Test reading svg object contained in a string with svg attributes""" - with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + with open(join(dirname(__file__), 'polygons.svg'), + 'r', encoding='utf-8') as file: # read entire file into string file_content = file.read() From a473ee3f4cae5f8b339cd1ed5371d334824a0e22 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Tue, 24 May 2022 18:15:52 +0200 Subject: [PATCH 09/13] Remove unnecessary seek commands --- svgpathtools/document.py | 2 -- svgpathtools/svg_to_paths.py | 2 -- test/test_document.py | 6 +----- test/test_svg2paths.py | 6 +----- 4 files changed, 2 insertions(+), 14 deletions(-) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 3d2b7c1..2ee0d69 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -265,8 +265,6 @@ class Document: """ # wrap string into StringIO object svg_file_obj = StringIO(svg_string) - # reset cursor to the beginning of the buffer - svg_file_obj.seek(0) # create document from file object return Document(svg_file_obj) diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index f8d5d88..617e622 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -271,8 +271,6 @@ def svg_string2paths(svg_string, info.""" # wrap string into StringIO object svg_file_obj = StringIO(svg_string) - # reset cursor to the beginning of the buffer - svg_file_obj.seek(0) return svg2paths(svg_file_name_or_file=svg_file_obj, return_svg_attributes=return_svg_attributes, convert_circles_to_paths=convert_circles_to_paths, diff --git a/test/test_document.py b/test/test_document.py index 8837e8b..d32bb3b 100644 --- a/test/test_document.py +++ b/test/test_document.py @@ -26,11 +26,7 @@ class TestDocument(unittest.TestCase): # read entire file into string file_content = file.read() # prepare stringio object - file_as_stringio = StringIO() - # paste file content into it - file_as_stringio.write(file_content) - # reset curser to its beginning - file_as_stringio.seek(0) + file_as_stringio = StringIO(file_content) doc = Document(file_as_stringio) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 1a48bd7..4533cc9 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -81,11 +81,7 @@ class TestSVG2Paths(unittest.TestCase): # read entire file into string file_content = file.read() # prepare stringio object - file_as_stringio = StringIO() - # paste file content into it - file_as_stringio.write(file_content) - # reset curser to its beginning - file_as_stringio.seek(0) + file_as_stringio = StringIO(file_content) paths, _ = svg2paths(file_as_stringio) From db5200f460f9a570ea8b15cfc59a213aa9bcc978 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Wed, 25 May 2022 17:39:10 +0200 Subject: [PATCH 10/13] Switch back to previous function parameter names --- svgpathtools/document.py | 16 ++++++++-------- svgpathtools/svg_to_paths.py | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 2ee0d69..974b1f3 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -225,7 +225,7 @@ def flattened_paths_from_group(group_to_flatten, root, recursive=True, class Document: - def __init__(self, svg_file_name_or_file=None): + def __init__(self, filepath=None): """A container for a DOM-style SVG document. The `Document` class provides a simple interface to modify and analyze @@ -236,25 +236,25 @@ class Document: The output Path objects will be transformed based on their parent groups. Args: - svg_file_name_or_file (str or file-like): The filepath of the + filepath (str or file-like): The filepath of the DOM-style object or a file-like object containing it. """ self.original_filepath = None # strings are interpreted as file location everything else is treated as # file-like object and passed to the xml parser directly - if isinstance(svg_file_name_or_file, str): + if isinstance(filepath, str): # remember location of original svg file if any - self.original_filepath = svg_file_name_or_file - if os.path.dirname(svg_file_name_or_file) == '': + self.original_filepath = filepath + if os.path.dirname(filepath) == '': self.original_filepath = os.path.join( - os.getcwd(), svg_file_name_or_file) + os.getcwd(), filepath) - if svg_file_name_or_file is None: + if filepath is None: self.tree = etree.ElementTree(Element('svg')) else: # parse svg to ElementTree object - self.tree = etree.parse(svg_file_name_or_file) + self.tree = etree.parse(filepath) self.root = self.tree.getroot() diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index 617e622..98d2b1d 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -130,7 +130,7 @@ def line2pathd(l): ) -def svg2paths(svg_file_name_or_file, +def svg2paths(svg_file_location, return_svg_attributes=False, convert_circles_to_paths=True, convert_ellipses_to_paths=True, @@ -145,7 +145,7 @@ def svg2paths(svg_file_name_or_file, SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements. Args: - svg_file_name_or_file (string or file-like object): the location of the + svg_file_location (string or file-like object): the location of the svg file on disk or a file-like object containing the content of a svg file return_svg_attributes (bool): Set to True and a dictionary of @@ -173,12 +173,12 @@ def svg2paths(svg_file_name_or_file, """ # strings are interpreted as file location everything else is treated as # file-like object and passed to the xml parser directly - if isinstance(svg_file_name_or_file, str): - if os_path.dirname(svg_file_name_or_file) == '': - svg_file_name_or_file = os_path.join( - getcwd(), svg_file_name_or_file) + if isinstance(svg_file_location, str): + if os_path.dirname(svg_file_location) == '': + svg_file_location = os_path.join( + getcwd(), svg_file_location) - doc = parse(svg_file_name_or_file) + doc = parse(svg_file_location) def dom2dict(element): """Converts DOM elements to dictionaries of attributes.""" @@ -237,7 +237,7 @@ def svg2paths(svg_file_name_or_file, return path_list, attribute_dictionary_list -def svg2paths2(svg_file_name_or_file, +def svg2paths2(svg_file_location, return_svg_attributes=True, convert_circles_to_paths=True, convert_ellipses_to_paths=True, @@ -248,7 +248,7 @@ def svg2paths2(svg_file_name_or_file, """Convenience function; identical to svg2paths() except that return_svg_attributes=True by default. See svg2paths() docstring for more info.""" - return svg2paths(svg_file_name_or_file=svg_file_name_or_file, + return svg2paths(svg_file_location=svg_file_location, return_svg_attributes=return_svg_attributes, convert_circles_to_paths=convert_circles_to_paths, convert_ellipses_to_paths=convert_ellipses_to_paths, @@ -271,7 +271,7 @@ def svg_string2paths(svg_string, info.""" # wrap string into StringIO object svg_file_obj = StringIO(svg_string) - return svg2paths(svg_file_name_or_file=svg_file_obj, + return svg2paths(svg_file_location=svg_file_obj, return_svg_attributes=return_svg_attributes, convert_circles_to_paths=convert_circles_to_paths, convert_ellipses_to_paths=convert_ellipses_to_paths, From aacd5fa96d57676d2e7a5ee35215cc9e082e5d08 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Wed, 25 May 2022 17:45:31 +0200 Subject: [PATCH 11/13] Remove second version of function returning the svg_attributes by default --- svgpathtools/svg_to_paths.py | 21 --------------------- test/test_svg2paths.py | 15 ++------------- 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index 98d2b1d..d8c9a4b 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -279,24 +279,3 @@ def svg_string2paths(svg_string, convert_polylines_to_paths=convert_polylines_to_paths, convert_polygons_to_paths=convert_polygons_to_paths, convert_rectangles_to_paths=convert_rectangles_to_paths) - - -def svg_string2paths2(svg_string, - return_svg_attributes=True, - convert_circles_to_paths=True, - convert_ellipses_to_paths=True, - convert_lines_to_paths=True, - convert_polylines_to_paths=True, - convert_polygons_to_paths=True, - convert_rectangles_to_paths=True): - """Convenience function; identical to svg2paths2() except that it takes the - svg object as string. See svg2paths() docstring for more - info.""" - return svg_string2paths(svg_string=svg_string, - return_svg_attributes=return_svg_attributes, - convert_circles_to_paths=convert_circles_to_paths, - convert_ellipses_to_paths=convert_ellipses_to_paths, - convert_lines_to_paths=convert_lines_to_paths, - convert_polylines_to_paths=convert_polylines_to_paths, - convert_polygons_to_paths=convert_polygons_to_paths, - convert_rectangles_to_paths=convert_rectangles_to_paths) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 4533cc9..faeccc1 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -87,8 +87,8 @@ class TestSVG2Paths(unittest.TestCase): self.assertEqual(len(paths), 2) - def test_from_string_without_svg_attrs(self): - """ Test reading svg object contained in a string without svg attributes""" + def test_from_string(self): + """ Test reading svg object contained in a string """ with open(join(dirname(__file__), 'polygons.svg'), 'r', encoding='utf-8') as file: # read entire file into string @@ -97,14 +97,3 @@ class TestSVG2Paths(unittest.TestCase): paths, _ = svg_string2paths(file_content) self.assertEqual(len(paths), 2) - - def test_from_string_with_svg_attrs(self): - """ Test reading svg object contained in a string with svg attributes""" - with open(join(dirname(__file__), 'polygons.svg'), - 'r', encoding='utf-8') as file: - # read entire file into string - file_content = file.read() - - paths, _, _ = svg_string2paths2(file_content) - - self.assertEqual(len(paths), 2) From 2fc016d48ff9ef3412d4720a9d2cafc60770fb57 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Wed, 25 May 2022 17:58:14 +0200 Subject: [PATCH 12/13] Make factory method a classmethod --- svgpathtools/document.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 974b1f3..3b98599 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -258,8 +258,8 @@ class Document: self.root = self.tree.getroot() - @staticmethod - def from_svg_string(svg_string): + @classmethod + def from_svg_string(cls, svg_string): """Factory method for creating a document from a string holding a svg object """ From 07f46d41f865b2bcc1c054cce35814dc8ffd32c4 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Wed, 25 May 2022 19:24:50 +0200 Subject: [PATCH 13/13] Rename svg_string2paths to svgstr2paths --- svgpathtools/__init__.py | 2 +- svgpathtools/svg_to_paths.py | 2 +- test/test_svg2paths.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/svgpathtools/__init__.py b/svgpathtools/__init__.py index 91f1afb..7e5da65 100644 --- a/svgpathtools/__init__.py +++ b/svgpathtools/__init__.py @@ -17,6 +17,6 @@ from .document import (Document, CONVERSIONS, CONVERT_ONLY_PATHS, from .svg_io_sax import SaxDocument try: - from .svg_to_paths import svg2paths, svg2paths2, svg_string2paths, svg_string2paths2 + from .svg_to_paths import svg2paths, svg2paths2, svgstr2paths except ImportError: pass diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py index d8c9a4b..dec0e44 100644 --- a/svgpathtools/svg_to_paths.py +++ b/svgpathtools/svg_to_paths.py @@ -258,7 +258,7 @@ def svg2paths2(svg_file_location, convert_rectangles_to_paths=convert_rectangles_to_paths) -def svg_string2paths(svg_string, +def svgstr2paths(svg_string, return_svg_attributes=False, convert_circles_to_paths=True, convert_ellipses_to_paths=True, diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index faeccc1..67a5751 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -94,6 +94,6 @@ class TestSVG2Paths(unittest.TestCase): # read entire file into string file_content = file.read() - paths, _ = svg_string2paths(file_content) + paths, _ = svgstr2paths(file_content) self.assertEqual(len(paths), 2)