restore support for PosixPath inputs for Document

support-stringio-objects
Andrew Port 2022-06-05 21:00:07 -07:00
parent a989c9831d
commit 356d86df78
3 changed files with 40 additions and 19 deletions

View File

@ -55,9 +55,13 @@ from .path import *
# To maintain forward/backward compatibility
try:
str = basestring
string = basestring
except NameError:
pass
string = str
try:
from os import PathLike
except ImportError:
PathLike = string
# Let xml.etree.ElementTree know about the SVG namespace
SVG_NAMESPACE = {'svg': 'http://www.w3.org/2000/svg'}
@ -239,16 +243,11 @@ class Document:
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(filepath, str):
# remember location of original svg file if any
self.original_filepath = filepath
if os.path.dirname(filepath) == '':
self.original_filepath = os.path.join(
os.getcwd(), filepath)
from_filepath = isinstance(filepath, string) or isinstance(filepath, PathLike)
self.original_filepath = os.path.abspath(filepath) if from_filepath else None
if filepath is None:
self.tree = etree.ElementTree(Element('svg'))
@ -280,7 +279,7 @@ class Document:
def paths_from_group(self, group, recursive=True, group_filter=lambda x: True,
path_filter=lambda x: True, path_conversions=CONVERSIONS):
if all(isinstance(s, str) for s in group):
if all(isinstance(s, string) for s in group):
# If we're given a list of strings, assume it represents a
# nested sequence
group = self.get_group(group)
@ -325,7 +324,7 @@ class Document:
path_svg = path.d()
elif is_path_segment(path):
path_svg = Path(path).d()
elif isinstance(path, str):
elif isinstance(path, string):
# Assume this is a valid d-string.
# TODO: Should we sanity check the input string?
path_svg = path

View File

@ -2,25 +2,36 @@ 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 io import open # overrides build-in open for compatibility with python2
from os.path import join, dirname
try:
import pathlib
except ImportError:
import pathlib2 as pathlib
class TestDocument(unittest.TestCase):
def test_from_file_path(self):
""" Test reading svg from file provided as path """
def test_from_file_path_string(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_path(self):
"""Test reading svg from file provided as path"""
doc = Document(pathlib.Path(__file__).parent / '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 """
"""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 """
"""Test reading svg object contained in a StringIO object"""
with open(join(dirname(__file__), 'polygons.svg'),
'r', encoding='utf-8') as file:
# read entire file into string
@ -33,7 +44,7 @@ class TestDocument(unittest.TestCase):
self.assertEqual(len(doc.paths()), 2)
def test_from_string(self):
""" Test reading svg object contained in a string"""
"""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

View File

@ -2,11 +2,16 @@ 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 io import open # overrides build-in open for compatibility with python2
from os.path import join, dirname
# try:
# import pathlib
# except ImportError:
# import pathlib2 as pathlib
from svgpathtools.svg_to_paths import rect2pathd
class TestSVG2Paths(unittest.TestCase):
def test_svg2paths_polygons(self):
@ -61,12 +66,18 @@ class TestSVG2Paths(unittest.TestCase):
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")
def test_from_file_path(self):
def test_from_file_path_string(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_path(self):
# """ Test reading svg from file provided as path """
# paths, _ = svg2paths(pathlib.Path(__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: