2016-06-20 19:45:31 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-13 17:13:14 +03:00
|
|
|
import hashlib
|
2016-06-20 19:45:31 +03:00
|
|
|
import asyncio
|
2016-07-13 17:13:14 +03:00
|
|
|
import base64
|
2016-06-20 19:45:31 +03:00
|
|
|
import uuid
|
2016-07-13 17:13:14 +03:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
|
|
|
|
from gns3server.utils.picture import get_size
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2016-06-20 19:45:31 +03:00
|
|
|
|
|
|
|
|
2016-06-23 12:17:23 +03:00
|
|
|
class Drawing:
|
2016-06-20 19:45:31 +03:00
|
|
|
"""
|
2016-06-23 12:17:23 +03:00
|
|
|
Drawing are visual element not used by the network emulation. Like
|
2016-06-20 19:45:31 +03:00
|
|
|
text, images, rectangle... They are pure SVG elements.
|
|
|
|
"""
|
2016-06-22 19:14:14 +03:00
|
|
|
|
2018-08-08 10:33:10 +03:00
|
|
|
def __init__(self, project, drawing_id=None, svg="<svg></svg>", x=0, y=0, z=2, rotation=0):
|
2016-06-20 19:45:31 +03:00
|
|
|
self._project = project
|
2016-06-23 12:17:23 +03:00
|
|
|
if drawing_id is None:
|
2016-06-20 19:45:31 +03:00
|
|
|
self._id = str(uuid.uuid4())
|
|
|
|
else:
|
2016-06-23 12:17:23 +03:00
|
|
|
self._id = drawing_id
|
2017-02-28 12:22:53 +02:00
|
|
|
self._svg = "<svg></svg>"
|
2016-07-13 17:13:14 +03:00
|
|
|
self.svg = svg
|
2016-06-20 19:45:31 +03:00
|
|
|
self._x = x
|
|
|
|
self._y = y
|
|
|
|
self._z = z
|
2016-06-21 20:39:00 +03:00
|
|
|
self._rotation = rotation
|
2016-06-20 19:45:31 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._id
|
|
|
|
|
2016-07-13 17:13:14 +03:00
|
|
|
@property
|
|
|
|
def ressource_filename(self):
|
|
|
|
"""
|
|
|
|
If the svg content has been dump to an external file return is name otherwise None
|
|
|
|
"""
|
|
|
|
if "<svg" not in self._svg:
|
|
|
|
return self._svg
|
|
|
|
return None
|
|
|
|
|
2016-06-20 19:45:31 +03:00
|
|
|
@property
|
|
|
|
def svg(self):
|
2016-07-13 17:13:14 +03:00
|
|
|
if "<svg" not in self._svg:
|
|
|
|
try:
|
|
|
|
filename = os.path.basename(self._svg)
|
|
|
|
with open(os.path.join(self._project.pictures_directory, filename), "rb") as f:
|
|
|
|
data = f.read()
|
|
|
|
try:
|
|
|
|
return data.decode()
|
|
|
|
except UnicodeError:
|
2016-08-17 11:49:31 +03:00
|
|
|
width, height, filetype = get_size(data)
|
2016-09-02 13:11:15 +03:00
|
|
|
return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"{height}\" width=\"{width}\">\n<image height=\"{height}\" width=\"{width}\" xlink:href=\"data:image/{filetype};base64,{b64}\" />\n</svg>".format(b64=base64.b64encode(data).decode(), filetype=filetype, width=width, height=height)
|
2016-07-13 17:13:14 +03:00
|
|
|
except OSError:
|
|
|
|
log.warning("Image file %s missing", filename)
|
|
|
|
return "<svg></svg>"
|
2016-06-20 19:45:31 +03:00
|
|
|
return self._svg
|
|
|
|
|
|
|
|
@svg.setter
|
|
|
|
def svg(self, value):
|
2016-07-13 17:13:14 +03:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2016-10-28 11:06:02 +03:00
|
|
|
try:
|
|
|
|
root = ET.fromstring(value)
|
|
|
|
except ET.ParseError as e:
|
|
|
|
log.error("Can't parse SVG: {}".format(e))
|
|
|
|
return
|
2016-07-13 17:13:14 +03:00
|
|
|
# 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):
|
2018-03-12 08:38:50 +02:00
|
|
|
with open(file_path, "wb") as f:
|
2016-07-13 17:13:14 +03:00
|
|
|
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):
|
2017-01-06 10:42:56 +02:00
|
|
|
with open(file_path, "w+", encoding="utf-8") as f:
|
2016-07-13 17:13:14 +03:00
|
|
|
f.write(value)
|
|
|
|
self._svg = filename
|
|
|
|
else:
|
|
|
|
self._svg = value
|
2016-06-20 19:45:31 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def x(self):
|
|
|
|
return self._x
|
|
|
|
|
|
|
|
@x.setter
|
|
|
|
def x(self, val):
|
|
|
|
self._x = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def y(self):
|
|
|
|
return self._y
|
|
|
|
|
|
|
|
@y.setter
|
|
|
|
def y(self, val):
|
|
|
|
self._y = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def z(self):
|
|
|
|
return self._z
|
|
|
|
|
|
|
|
@z.setter
|
|
|
|
def z(self, val):
|
|
|
|
self._z = val
|
|
|
|
|
2016-06-21 20:39:00 +03:00
|
|
|
@property
|
|
|
|
def rotation(self):
|
|
|
|
return self._rotation
|
|
|
|
|
|
|
|
@rotation.setter
|
|
|
|
def rotation(self, val):
|
|
|
|
self._rotation = val
|
|
|
|
|
2016-06-20 19:45:31 +03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def update(self, **kwargs):
|
|
|
|
"""
|
2016-06-23 12:17:23 +03:00
|
|
|
Update the drawing
|
2016-06-20 19:45:31 +03:00
|
|
|
|
2016-06-23 12:17:23 +03:00
|
|
|
:param kwargs: Drawing properties
|
2016-06-20 19:45:31 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Update node properties with additional elements
|
2016-06-22 18:46:00 +03:00
|
|
|
svg_changed = False
|
2016-06-20 19:45:31 +03:00
|
|
|
for prop in kwargs:
|
2016-09-02 12:46:08 +03:00
|
|
|
if prop == "drawing_id":
|
2016-09-02 13:11:15 +03:00
|
|
|
pass # No good reason to change a drawing_id
|
2016-09-02 12:46:08 +03:00
|
|
|
elif getattr(self, prop) != kwargs[prop]:
|
2016-06-22 18:46:00 +03:00
|
|
|
if prop == "svg":
|
|
|
|
# To avoid spamming client with large data we don't send the svg if the SVG didn't change
|
|
|
|
svg_changed = True
|
2016-06-20 19:45:31 +03:00
|
|
|
setattr(self, prop, kwargs[prop])
|
2016-06-22 18:46:00 +03:00
|
|
|
data = self.__json__()
|
|
|
|
if not svg_changed:
|
|
|
|
del data["svg"]
|
2018-08-16 17:31:56 +03:00
|
|
|
self._project.controller.notification.project_emit("drawing.updated", data)
|
2016-06-20 19:45:31 +03:00
|
|
|
self._project.dump()
|
|
|
|
|
|
|
|
def __json__(self, topology_dump=False):
|
|
|
|
"""
|
|
|
|
:param topology_dump: Filter to keep only properties require for saving on disk
|
|
|
|
"""
|
|
|
|
if topology_dump:
|
|
|
|
return {
|
2016-06-23 12:17:23 +03:00
|
|
|
"drawing_id": self._id,
|
2016-06-20 19:45:31 +03:00
|
|
|
"x": self._x,
|
|
|
|
"y": self._y,
|
|
|
|
"z": self._z,
|
2016-06-21 20:39:00 +03:00
|
|
|
"rotation": self._rotation,
|
2016-06-21 13:19:12 +03:00
|
|
|
"svg": self._svg
|
2016-06-20 19:45:31 +03:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
"project_id": self._project.id,
|
2016-06-23 12:17:23 +03:00
|
|
|
"drawing_id": self._id,
|
2016-06-20 19:45:31 +03:00
|
|
|
"x": self._x,
|
|
|
|
"y": self._y,
|
|
|
|
"z": self._z,
|
2016-06-21 20:39:00 +03:00
|
|
|
"rotation": self._rotation,
|
2016-07-13 17:13:14 +03:00
|
|
|
"svg": self.svg
|
2016-06-20 19:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-06-23 12:17:23 +03:00
|
|
|
return "<gns3server.controller.Drawing {}>".format(self._id)
|