Skip to content

Commit

Permalink
Correct issue #227. If we are not a filename, we must be a fileobject…
Browse files Browse the repository at this point in the history
… (avoid crashing)
  • Loading branch information
tatarize committed May 14, 2023
1 parent bce539e commit b420d74
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions svgelements/svgelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3428,8 +3428,8 @@ def set(self, key, value):
self.values[key] = value
return self

def write_xml(self, f):
write(self, f)
def write_xml(self, f, **kwargs):
write(self, f, **kwargs)

def string_xml(self):
return tostring(self)
Expand Down Expand Up @@ -9355,18 +9355,26 @@ def tostring(node):
return tostring(_write_node(node), encoding="unicode")


def write(node, f, pretty=True):
def write(node, f, pretty=True, **kwargs):
"""
Gets the write node and writes to the ElementTree.write() function, all options in Element.write() are passed
along via this method. encoding, xml_declaration, short_empty_elements, etc.
"""
from xml.etree.ElementTree import ElementTree

root = _write_node(node)
if pretty:
_pretty_print(root)
tree = ElementTree(root)
if f.lower().endswith("svgz"):
import gzip
try:
if f.lower().endswith("svgz"):
import gzip

f = gzip.open(f, "wb")
tree.write(f)
f = gzip.open(f, "wb")
except AttributeError:
# might be a pathlib.Path()
pass
tree.write(f, **kwargs)


def _write_node(node, xml_tree=None, viewport_transform=None):
Expand Down

0 comments on commit b420d74

Please sign in to comment.