Closed pinkeen closed 2 years ago
I believe the internal code is using a ElementTree. I think that that actually already accepts either a string or a stream. I'll double check. Also, I would guess that duck casting in python is strongly preferred. I mean if I have something that you can open and read data from, that's a stream even if it's not strictly speaking an IOBase descendent. Looks like a duck, quacks like a duck...
def parse(source, parser=None):
"""Parse XML document into element tree.
*source* is a filename or file object containing XML data,
*parser* is an optional parser instance defaulting to XMLParser.
Return an ElementTree instance.
"""
tree = ElementTree()
tree.parse(source, parser)
return tree
It appears like the code here is limited in the Document class. It believed the only input was filename. The better correction therefore is to save self.original_filename
if the input type is a string, else None. Then it will save this input stream filename type as unknown.svg
if you try to export it at the end. And whatever input you have there should be fed directly in the ElementTree parse. I'd check whether it's a string or not rather than IOBase.
After all, the ElementTree isn't limited to IOBase objects either:
close_source = False
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
It checks whether than source has a read component and then goes ahead and opens it accordingly. If I wrote my own class that had a read and open, I could hand it to ElementTree but not to your code. Your code would decide since it isn't a IOBase it must be a str, even if it wasn't a string.
Thanks @tatarize and @pinkeen. @pinkeen, please address @tatarize's concerns.
Closing due to #176 being merged in
Resolves https://github.com/mathandy/svgpathtools/issues/138