mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
parent
938696032c
commit
f82527e253
@ -31,7 +31,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
# This properties are used only on controller and are not forwarded to the compute
|
# This properties are used only on controller and are not forwarded to the compute
|
||||||
CONTROLLER_ONLY_PROPERTIES = ["x", "y", "z", "symbol", "label", "console_host"]
|
CONTROLLER_ONLY_PROPERTIES = ["x", "y", "z", "width", "height", "symbol", "label", "console_host"]
|
||||||
|
|
||||||
def __init__(self, project, compute, name, node_id=None, node_type=None, **kwargs):
|
def __init__(self, project, compute, name, node_id=None, node_type=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -69,21 +69,27 @@ class Node:
|
|||||||
self._command_line = None
|
self._command_line = None
|
||||||
self._node_directory = None
|
self._node_directory = None
|
||||||
self._status = "stopped"
|
self._status = "stopped"
|
||||||
self._width = 70
|
|
||||||
self._height = 70
|
|
||||||
self._x = 0
|
self._x = 0
|
||||||
self._y = 0
|
self._y = 0
|
||||||
self._z = 0
|
self._z = 0
|
||||||
self._symbol = ":/symbols/computer.svg"
|
self._symbol = None
|
||||||
|
|
||||||
# Update node properties with additional elements
|
# Update node properties with additional elements
|
||||||
|
|
||||||
|
# This properties will be recompute
|
||||||
|
ignore_properties = ("width", "height")
|
||||||
|
|
||||||
for prop in kwargs:
|
for prop in kwargs:
|
||||||
|
if prop not in ignore_properties:
|
||||||
try:
|
try:
|
||||||
setattr(self, prop, kwargs[prop])
|
setattr(self, prop, kwargs[prop])
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
log.critical("Can't set attribute %s", prop)
|
log.critical("Can't set attribute %s", prop)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
if self._symbol is None:
|
||||||
|
self.symbol = ":/symbols/computer.svg"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
return self._id
|
return self._id
|
||||||
@ -173,18 +179,10 @@ class Node:
|
|||||||
def width(self):
|
def width(self):
|
||||||
return self._width
|
return self._width
|
||||||
|
|
||||||
@width.setter
|
|
||||||
def width(self, val):
|
|
||||||
self._width = val
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def height(self):
|
def height(self):
|
||||||
return self._height
|
return self._height
|
||||||
|
|
||||||
@height.setter
|
|
||||||
def height(self, val):
|
|
||||||
self._height = val
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def symbol(self):
|
def symbol(self):
|
||||||
return self._symbol
|
return self._symbol
|
||||||
@ -192,6 +190,11 @@ class Node:
|
|||||||
@symbol.setter
|
@symbol.setter
|
||||||
def symbol(self, val):
|
def symbol(self, val):
|
||||||
self._symbol = val
|
self._symbol = val
|
||||||
|
try:
|
||||||
|
self._width, self._height, filetype = self._project.controller.symbols.get_size(val)
|
||||||
|
# If symbol is invalid we replace it by default
|
||||||
|
except (ValueError, OSError):
|
||||||
|
self.symbol = ":/symbols/computer.svg"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def label(self):
|
def label(self):
|
||||||
|
@ -19,6 +19,7 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
from ..utils.get_resource import get_resource
|
from ..utils.get_resource import get_resource
|
||||||
|
from ..utils.picture import get_size
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
|
|
||||||
|
|
||||||
@ -29,6 +30,8 @@ class Symbols:
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.list()
|
self.list()
|
||||||
|
# Keep a cache of symbols size
|
||||||
|
self._symbol_size_cache = {}
|
||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
self._symbols_path = {}
|
self._symbols_path = {}
|
||||||
@ -68,3 +71,12 @@ class Symbols:
|
|||||||
|
|
||||||
def get_path(self, symbol_id):
|
def get_path(self, symbol_id):
|
||||||
return self._symbols_path[symbol_id]
|
return self._symbols_path[symbol_id]
|
||||||
|
|
||||||
|
def get_size(self, symbol_id):
|
||||||
|
try:
|
||||||
|
return self._symbol_size_cache[symbol_id]
|
||||||
|
except KeyError:
|
||||||
|
with open(self.get_path(symbol_id), "rb") as f:
|
||||||
|
res = get_size(f.read())
|
||||||
|
self._symbol_size_cache[symbol_id] = res
|
||||||
|
return res
|
||||||
|
@ -145,11 +145,11 @@ NODE_OBJECT_SCHEMA = {
|
|||||||
"minLength": 1
|
"minLength": 1
|
||||||
},
|
},
|
||||||
"width": {
|
"width": {
|
||||||
"description": "Width of the node",
|
"description": "Width of the node (Read only)",
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"height": {
|
"height": {
|
||||||
"description": "Height of the node",
|
"description": "Height of the node (Read only)",
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"x": {
|
"x": {
|
||||||
|
@ -171,6 +171,20 @@ def test_create_image_missing(node, compute, project, async_run):
|
|||||||
node._upload_missing_image.called is True
|
node._upload_missing_image.called is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_symbol(node):
|
||||||
|
"""
|
||||||
|
Change symbol should change the node size
|
||||||
|
"""
|
||||||
|
node.symbol = ":/symbols/dslam.svg"
|
||||||
|
assert node.symbol == ":/symbols/dslam.svg"
|
||||||
|
assert node.width == 50
|
||||||
|
assert node.height == 53
|
||||||
|
node.symbol = ":/symbols/cloud.svg"
|
||||||
|
assert node.symbol == ":/symbols/cloud.svg"
|
||||||
|
assert node.width == 159
|
||||||
|
assert node.height == 71
|
||||||
|
|
||||||
|
|
||||||
def test_update(node, compute, project, async_run, controller):
|
def test_update(node, compute, project, async_run, controller):
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
|
@ -44,3 +44,8 @@ def test_list(symbols_dir):
|
|||||||
def test_get_path():
|
def test_get_path():
|
||||||
symbols = Symbols()
|
symbols = Symbols()
|
||||||
assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg")
|
assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_size():
|
||||||
|
symbols = Symbols()
|
||||||
|
assert symbols.get_size(':/symbols/firewall.svg') == (66, 45, 'svg')
|
||||||
|
Loading…
Reference in New Issue
Block a user