add support for os.PathLike arguments

support-stringio-objects
Andrew Port 2022-06-05 21:47:37 -07:00
parent d3a66f0bbd
commit e8792f4d2d
2 changed files with 16 additions and 15 deletions

View File

@ -4,9 +4,13 @@ The main tool being the svg2paths() function."""
# External dependencies
from __future__ import division, absolute_import, print_function
from xml.dom.minidom import parse
from os import path as os_path, getcwd
import os
from io import StringIO
import re
try:
from os import PathLike as FilePathLike
except ImportError:
FilePathLike = str
# Internal dependencies
from .parser import parse_path
@ -173,10 +177,8 @@ def svg2paths(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_location, str):
if os_path.dirname(svg_file_location) == '':
svg_file_location = os_path.join(
getcwd(), svg_file_location)
from_filepath = isinstance(svg_file_location, str) or isinstance(svg_file_location, FilePathLike)
svg_file_location = os.path.abspath(svg_file_location) if from_filepath else svg_file_location
doc = parse(svg_file_location)

View File

@ -4,10 +4,7 @@ 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
# try:
# import pathlib
# except ImportError:
# import pathlib2 as pathlib
from sys import version_info
from svgpathtools.svg_to_paths import rect2pathd
@ -67,16 +64,18 @@ class TestSVG2Paths(unittest.TestCase):
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_string(self):
""" Test reading svg from file provided as path """
""" 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_path(self):
""" Test reading svg from file provided as pathlib POSIXPath"""
if version_info >= (3, 6):
import pathlib
paths, _ = svg2paths(pathlib.Path(__file__).parent / 'polygons.svg')
self.assertEqual(len(paths), 2)
def test_from_file_object(self):
""" Test reading svg from file object that has already been opened """