From 1771fbfb06e3a1ec92628859d2bb2c9dec85adf4 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 15:30:48 +0200 Subject: [PATCH] Add factory method for creating from string holding svg object --- svgpathtools/document.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/svgpathtools/document.py b/svgpathtools/document.py index 8371c85..3d2b7c1 100644 --- a/svgpathtools/document.py +++ b/svgpathtools/document.py @@ -41,6 +41,7 @@ import xml.etree.ElementTree as etree from xml.etree.ElementTree import Element, SubElement, register_namespace from xml.dom.minidom import parseString import warnings +from io import StringIO from tempfile import gettempdir from time import time @@ -257,6 +258,18 @@ class Document: self.root = self.tree.getroot() + @staticmethod + def from_svg_string(svg_string): + """Factory method for creating a document from a string holding a svg + object + """ + # wrap string into StringIO object + svg_file_obj = StringIO(svg_string) + # reset cursor to the beginning of the buffer + svg_file_obj.seek(0) + # create document from file object + return Document(svg_file_obj) + def paths(self, group_filter=lambda x: True, path_filter=lambda x: True, path_conversions=CONVERSIONS): """Returns a list of all paths in the document.