Allow file-like object as input to Documents ctor and svg2paths function

pull/176/head
FlyingSamson 2022-05-22 13:09:13 +02:00
parent 19df25b99b
commit ce43c75cd8
2 changed files with 27 additions and 15 deletions

View File

@ -224,7 +224,7 @@ def flattened_paths_from_group(group_to_flatten, root, recursive=True,
class Document: class Document:
def __init__(self, filepath=None): def __init__(self, svg_file_name_or_file=None):
"""A container for a DOM-style SVG document. """A container for a DOM-style SVG document.
The `Document` class provides a simple interface to modify and analyze 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. The output Path objects will be transformed based on their parent groups.
Args: 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 # strings are interpreted as file location everything else is treated as
self.original_filepath = filepath # file-like object and passed to the xml parser directly
if filepath is not None and os.path.dirname(filepath) == '': if isinstance(svg_file_name_or_file, str):
self.original_filepath = os.path.join(os.getcwd(), filepath) # 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')) self.tree = etree.ElementTree(Element('svg'))
else: else:
# parse svg to ElementTree object # parse svg to ElementTree object
self.tree = etree.parse(filepath) self.tree = etree.parse(svg_file_name_or_file)
self.root = self.tree.getroot() self.root = self.tree.getroot()

View File

@ -129,7 +129,7 @@ def line2pathd(l):
) )
def svg2paths(svg_file_location, def svg2paths(svg_file_name_or_file,
return_svg_attributes=False, return_svg_attributes=False,
convert_circles_to_paths=True, convert_circles_to_paths=True,
convert_ellipses_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. SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
Args: 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 return_svg_attributes (bool): Set to True and a dictionary of
svg-attributes will be extracted and returned. See also the svg-attributes will be extracted and returned. See also the
`svg2paths2()` function. `svg2paths2()` function.
@ -168,10 +170,14 @@ def svg2paths(svg_file_location,
list: The list of corresponding path attribute dictionaries. list: The list of corresponding path attribute dictionaries.
dict (optional): A dictionary of svg-attributes (see `svg2paths2()`). dict (optional): A dictionary of svg-attributes (see `svg2paths2()`).
""" """
if os_path.dirname(svg_file_location) == '': # strings are interpreted as file location everything else is treated as
svg_file_location = os_path.join(getcwd(), svg_file_location) # 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): def dom2dict(element):
"""Converts DOM elements to dictionaries of attributes.""" """Converts DOM elements to dictionaries of attributes."""
@ -230,7 +236,7 @@ def svg2paths(svg_file_location,
return path_list, attribute_dictionary_list return path_list, attribute_dictionary_list
def svg2paths2(svg_file_location, def svg2paths2(svg_file_name_or_file,
return_svg_attributes=True, return_svg_attributes=True,
convert_circles_to_paths=True, convert_circles_to_paths=True,
convert_ellipses_to_paths=True, convert_ellipses_to_paths=True,
@ -241,7 +247,7 @@ def svg2paths2(svg_file_location,
"""Convenience function; identical to svg2paths() except that """Convenience function; identical to svg2paths() except that
return_svg_attributes=True by default. See svg2paths() docstring for more return_svg_attributes=True by default. See svg2paths() docstring for more
info.""" 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, return_svg_attributes=return_svg_attributes,
convert_circles_to_paths=convert_circles_to_paths, convert_circles_to_paths=convert_circles_to_paths,
convert_ellipses_to_paths=convert_ellipses_to_paths, convert_ellipses_to_paths=convert_ellipses_to_paths,