Item => Shape

This commit is contained in:
Julien Duponchelle committed 2016-06-21 09:49:16 +02:00
1 parent 85ebac7eb3
commit 116135b9dc
12 files changed
+125 -125

No files matched your search

+24 -24
View File
@@ -24,7 +24,7 @@ import shutil
from uuid import UUID, uuid4
from .node import Node
from .item import Item
from .shape import Shape
from .topology import project_to_topology, load_topology
from .udp_link import UDPLink
from ..config import Config
@@ -77,7 +77,7 @@ class Project:
self._allocated_node_names = set()
self._nodes = {}
self._links = {}
self._items = {}
self._shapes = {}
# Create the project on demand on the compute node
self._project_created_on_compute = set()
@@ -266,42 +266,42 @@ class Project:
return self._nodes
@property
def items(self):
def shapes(self):
"""
:returns: Dictionary of the items
:returns: Dictionary of the shapes
"""
return self._items
return self._shapes
@asyncio.coroutine
def add_item(self, item_id=None, **kwargs):
def add_shape(self, shape_id=None, **kwargs):
"""
Create an item or return an existing item
Create an shape or return an existing shape
:param kwargs: See the documentation of item
:param kwargs: See the documentation of shape
"""
if item_id not in self._items:
item = Item(self, item_id=item_id, **kwargs)
self._items[item.id] = item
self.controller.notification.emit("item.created", item.__json__())
if shape_id not in self._shapes:
shape = Shape(self, shape_id=shape_id, **kwargs)
self._shapes[shape.id] = shape
self.controller.notification.emit("shape.created", shape.__json__())
self.dump()
return item
return self._items[item_id]
return shape
return self._shapes[shape_id]
def get_item(self, item_id):
def get_shape(self, shape_id):
"""
Return the Item or raise a 404 if the item is unknown
Return the Shape or raise a 404 if the shape is unknown
"""
try:
return self._items[item_id]
return self._shapes[shape_id]
except KeyError:
raise aiohttp.web.HTTPNotFound(text="Item ID {} doesn't exist".format(item_id))
raise aiohttp.web.HTTPNotFound(text="Shape ID {} doesn't exist".format(shape_id))
@asyncio.coroutine
def delete_item(self, item_id):
item = self.get_item(item_id)
del self._items[item.id]
def delete_shape(self, shape_id):
shape = self.get_shape(shape_id)
del self._shapes[shape.id]
self.dump()
self.controller.notification.emit("item.deleted", item.__json__())
self.controller.notification.emit("shape.deleted", shape.__json__())
@asyncio.coroutine
def add_link(self, link_id=None):
@@ -397,8 +397,8 @@ class Project:
node = self.get_node(node_link["node_id"])
yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"])
for item_data in topology.get("items", []):
item = yield from self.add_item(**item_data)
for shape_data in topology.get("shapes", []):
shape = yield from self.add_shape(**shape_data)
self._status = "opened"
def dump(self):
@@ -19,18 +19,18 @@ import asyncio
import uuid
class Item:
class Shape:
"""
Item are visual element not used by the network emulation. Like
Shape are visual element not used by the network emulation. Like
text, images, rectangle... They are pure SVG elements.
"""
def __init__(self, project, item_id=None, svg=None, x=0, y=0, z=0):
def __init__(self, project, shape_id=None, svg=None, x=0, y=0, z=0):
self.svg = "<svg></svg>"
self._project = project
if item_id is None:
if shape_id is None:
self._id = str(uuid.uuid4())
else:
self._id = item_id
self._id = shape_id
self._x = x
self._y = y
self._z = z
@@ -83,7 +83,7 @@ class Item:
for prop in kwargs:
if getattr(self, prop) != kwargs[prop]:
setattr(self, prop, kwargs[prop])
self._project.controller.notification.emit("item.updated", self.__json__())
self._project.controller.notification.emit("shape.updated", self.__json__())
self._project.dump()
def __json__(self, topology_dump=False):
@@ -92,20 +92,20 @@ class Item:
"""
if topology_dump:
return {
"item_id": self._id,
"shape_id": self._id,
"x": self._x,
"y": self._y,
"z": self._z,
}
return {
"project_id": self._project.id,
"item_id": self._id,
"shape_id": self._id,
"x": self._x,
"y": self._y,
"z": self._z,
}
def __repr__(self):
return "<gns3server.controller.Item {}>".format(self._id)
return "<gns3server.controller.Shape {}>".format(self._id)
+3 -3
View File
@@ -34,7 +34,7 @@ def project_to_topology(project):
"nodes": [],
"links": [],
"computes": [],
"items": []
"shapes": []
},
"type": "topology",
"revision": GNS3_FILE_FORMAT_REVISION,
@@ -47,8 +47,8 @@ def project_to_topology(project):
data["topology"]["nodes"].append(node.__json__(topology_dump=True))
for link in project.links.values():
data["topology"]["links"].append(link.__json__(topology_dump=True))
for item in project.items.values():
data["topology"]["items"].append(item.__json__(topology_dump=True))
for shape in project.shapes.values():
data["topology"]["shapes"].append(shape.__json__(topology_dump=True))
for compute in computes:
if hasattr(compute, "__json__"):
data["topology"]["computes"].append(compute.__json__(topology_dump=True))