def svg(self, value):
        """
        Set SVG field value.

        If the svg has embed base64 element we will extract them
        to disk in order to avoid duplication of content
        """

        if len(value) < 500:
            self._svg = value
            return

        try:
            root = ET.fromstring(value)
        except ET.ParseError as e:
            log.error("Can't parse SVG: {}".format(e))
            return
        # SVG is the default namespace no need to prefix it
        ET.register_namespace('xmlns', "http://www.w3.org/2000/svg")
        ET.register_namespace('xmlns:xlink', "http://www.w3.org/1999/xlink")

        if len(root.findall("{http://www.w3.org/2000/svg}image")) == 1:
            href = "{http://www.w3.org/1999/xlink}href"
            elem = root.find("{http://www.w3.org/2000/svg}image")
            if elem.get(href, "").startswith("data:image/"):
                changed = True
                data = elem.get(href, "")
                extension = re.sub(r"[^a-z0-9]", "", data.split(";")[0].split("/")[1].lower())

                data = base64.decodebytes(data.split(",", 1)[1].encode())

                # We compute an hash of the image file to avoid duplication
                filename = hashlib.md5(data).hexdigest() + "." + extension
                elem.set(href, filename)

                file_path = os.path.join(self._project.pictures_directory, filename)
                if not os.path.exists(file_path):
                    with open(file_path, "wb") as f:
                        f.write(data)
                value = filename

        # We dump also large svg on disk to keep .gns3 small
        if len(value) > 1000:
            filename = hashlib.md5(value.encode()).hexdigest() + ".svg"
            file_path = os.path.join(self._project.pictures_directory, filename)
            if not os.path.exists(file_path):
                with open(file_path, "w+", encoding="utf-8") as f:
                    f.write(value)
            self._svg = filename
        else:
            self._svg = value
def svg ( self , value ) : if len ( value ) < 500 : self . _svg = value return try : root = ET . fromstring ( value ) except ET . ParseError as e : log . error ( "Can't parse SVG: {}" . format ( e ) ) return ET . register_namespace ( 'xmlns' , "http://www.w3.org/2000/svg" ) ET . register_namespace ( 'xmlns:xlink' , "http://www.w3.org/1999/xlink" ) if len ( root . findall ( "{http://www.w3.org/2000/svg}image" ) ) == 1 : href = "{http://www.w3.org/1999/xlink}href" elem = root . find ( "{http://www.w3.org/2000/svg}image" ) if elem . get ( href , "" ) . startswith ( "data:image/" ) : changed = True data = elem . get ( href , "" ) extension = re . sub ( r"[^a-z0-9]" , "" , data . split ( ";" ) [ 0 ] . split ( "/" ) [ 1 ] . lower ( ) ) data = base64 . decodebytes ( data . split ( "," , 1 ) [ 1 ] . encode ( ) ) filename = hashlib . md5 ( data ) . hexdigest ( ) + "." + extension elem . set ( href , filename ) file_path = os . path . join ( self . _project . pictures_directory , filename ) if not os . path . exists ( file_path ) : with open ( file_path , "wb" ) as f : f . write ( data ) value = filename if len ( value ) > 1000 : filename = hashlib . md5 ( value . encode ( ) ) . hexdigest ( ) + ".svg" file_path = os . path . join ( self . _project . pictures_directory , filename ) if not os . path . exists ( file_path ) : with open ( file_path , "w+" , encoding = "utf-8" ) as f : f . write ( value ) self . _svg = filename else : self . _svg = value
Set SVG field value.

        If the svg has embed base64 element we will extract them
        to disk in order to avoid duplication of content