Rename __json__() to asdict()

This commit is contained in:
grossmj committed 2021-04-17 23:34:28 +09:30
1 parent 44074ff7c9
commit cefab8d362
89 files changed
+286 -282

No files matched your search

+3 -3
View File
@@ -355,12 +355,12 @@ class Controller:
if connect:
# call compute.connect() later to give time to the controller to be fully started
asyncio.get_event_loop().call_later(1, lambda: asyncio.ensure_future(compute.connect()))
self.notification.controller_emit("compute.created", compute.__json__())
self.notification.controller_emit("compute.created", compute.asdict())
return compute
else:
if connect:
await self._computes[compute_id].connect()
self.notification.controller_emit("compute.updated", self._computes[compute_id].__json__())
self.notification.controller_emit("compute.updated", self._computes[compute_id].asdict())
return self._computes[compute_id]
async def close_compute_projects(self, compute):
@@ -399,7 +399,7 @@ class Controller:
await compute.close()
del self._computes[compute_id]
# self.save()
self.notification.controller_emit("compute.deleted", compute.__json__())
self.notification.controller_emit("compute.deleted", compute.asdict())
@property
def notification(self):
+1 -1
View File
@@ -60,7 +60,7 @@ class Appliance:
def symbol(self, new_symbol):
self._data["symbol"] = new_symbol
def __json__(self):
def asdict(self):
"""
Appliance data (a hash)
"""
+1 -1
View File
@@ -104,7 +104,7 @@ class ApplianceManager:
try:
with open(path, encoding="utf-8") as f:
appliance = Appliance(appliance_id, json.load(f), builtin=builtin)
json_data = appliance.__json__() # Check if loaded without error
json_data = appliance.asdict() # Check if loaded without error
if appliance.status != "broken":
self._appliances[appliance.id] = appliance
if not appliance.symbol or appliance.symbol.startswith(":/symbols/"):
+7 -7
View File
@@ -162,7 +162,7 @@ class Compute:
if self._http_session and not self._http_session.closed:
await self._http_session.close()
self._connected = False
self._controller.notification.controller_emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.asdict())
self._controller.save()
async def close(self):
@@ -290,7 +290,7 @@ class Compute:
def disk_usage_percent(self):
return self._disk_usage_percent
def __json__(self, topology_dump=False):
def asdict(self, topology_dump=False):
"""
:param topology_dump: Filter to keep only properties require for saving on disk
"""
@@ -444,7 +444,7 @@ class Compute:
self._connected = True
self._connection_failure = 0
self._last_error = None
self._controller.notification.controller_emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.asdict())
async def _connect_notification(self):
"""
@@ -466,7 +466,7 @@ class Compute:
self._memory_usage_percent = event["memory_usage_percent"]
self._disk_usage_percent = event["disk_usage_percent"]
# FIXME: slow down number of compute events
self._controller.notification.controller_emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.asdict())
else:
await self._controller.notification.dispatch(
action, event, project_id=project_id, compute_id=self.id
@@ -494,7 +494,7 @@ class Compute:
self._cpu_usage_percent = None
self._memory_usage_percent = None
self._disk_usage_percent = None
self._controller.notification.controller_emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.asdict())
def _getUrl(self, path):
host = self._host
@@ -523,8 +523,8 @@ class Compute:
if data == {}:
data = None
elif data is not None:
if hasattr(data, "__json__"):
data = json.dumps(data.__json__())
if hasattr(data, "asdict"):
data = json.dumps(data.asdict())
elif isinstance(data, aiohttp.streams.EmptyStreamReader):
data = None
# Stream the request
+2 -2
View File
@@ -193,13 +193,13 @@ class Drawing:
# To avoid spamming client with large data we don't send the svg if the SVG didn't change
svg_changed = True
setattr(self, prop, kwargs[prop])
data = self.__json__()
data = self.asdict()
if not svg_changed:
del data["svg"]
self._project.emit_notification("drawing.updated", data)
self._project.dump()
def __json__(self, topology_dump=False):
def asdict(self, topology_dump=False):
"""
:param topology_dump: Filter to keep only properties require for saving on disk
"""
+1 -1
View File
@@ -257,7 +257,7 @@ class GNS3VM:
return self._engines["remote"]
raise NotImplementedError(f"The engine {engine} for the GNS3 VM is not supported")
def __json__(self):
def asdict(self):
return self._settings
@locking
+7 -7
View File
@@ -160,14 +160,14 @@ class Link:
self._filters = new_filters
if self._created:
await self.update()
self._project.emit_notification("link.updated", self.__json__())
self._project.emit_notification("link.updated", self.asdict())
self._project.dump()
async def update_suspend(self, value):
if value != self._suspended:
self._suspended = value
await self.update()
self._project.emit_notification("link.updated", self.__json__())
self._project.emit_notification("link.updated", self.asdict())
self._project.dump()
@property
@@ -231,7 +231,7 @@ class Link:
n["node"].add_link(self)
n["port"].link = self
self._created = True
self._project.emit_notification("link.created", self.__json__())
self._project.emit_notification("link.created", self.asdict())
if dump:
self._project.dump()
@@ -244,7 +244,7 @@ class Link:
label = node_data.get("label")
if label:
port["label"] = label
self._project.emit_notification("link.updated", self.__json__())
self._project.emit_notification("link.updated", self.asdict())
self._project.dump()
async def create(self):
@@ -286,7 +286,7 @@ class Link:
self._capturing = True
self._capture_file_name = capture_file_name
self._project.emit_notification("link.updated", self.__json__())
self._project.emit_notification("link.updated", self.asdict())
async def stop_capture(self):
"""
@@ -294,7 +294,7 @@ class Link:
"""
self._capturing = False
self._project.emit_notification("link.updated", self.__json__())
self._project.emit_notification("link.updated", self.asdict())
def pcap_streaming_url(self):
"""
@@ -417,7 +417,7 @@ class Link:
def __hash__(self):
return hash(self._id)
def __json__(self, topology_dump=False):
def asdict(self, topology_dump=False):
"""
:param topology_dump: Filter to keep only properties require for saving on disk
"""
+5 -5
View File
@@ -419,7 +419,7 @@ class Node:
# When updating properties used only on controller we don't need to call the compute
update_compute = False
old_json = self.__json__()
old_json = self.asdict()
compute_properties = None
# Update node properties with additional elements
@@ -449,9 +449,9 @@ class Node:
data = self._node_data(properties=compute_properties)
response = await self.put(None, data=data)
await self.parse_node_response(response.json)
elif old_json != self.__json__():
elif old_json != self.asdict():
# We send notif only if object has changed
self.project.emit_notification("node.updated", self.__json__())
self.project.emit_notification("node.updated", self.asdict())
self.project.dump()
async def parse_node_response(self, response):
@@ -777,7 +777,7 @@ class Node:
return False
return self.id == other.id and other.project.id == self.project.id
def __json__(self, topology_dump=False):
def asdict(self, topology_dump=False):
"""
:param topology_dump: Filter to keep only properties required for saving on disk
"""
@@ -817,7 +817,7 @@ class Node:
"status": self._status,
"console_host": str(self._compute.console_host),
"node_directory": self._node_directory,
"ports": [port.__json__() for port in self.ports]
"ports": [port.asdict() for port in self.ports]
}
topology.update(additional_data)
return topology
+1 -1
View File
@@ -98,7 +98,7 @@ class Notification:
project = self._controller.get_project(event["project_id"])
node = project.get_node(event["node_id"])
await node.parse_node_response(event)
self.project_emit("node.updated", node.__json__())
self.project_emit("node.updated", node.asdict())
except ControllerError: # Project closing
return
elif action == "ping":
+1 -1
View File
@@ -93,7 +93,7 @@ class Port:
def short_name(self, val):
self._short_name = val
def __json__(self):
def asdict(self):
info = {
"name": self._name,
"short_name": self.short_name,
+11 -11
View File
@@ -169,14 +169,14 @@ class Project:
:param kwargs: Project properties
"""
old_json = self.__json__()
old_json = self.asdict()
for prop in kwargs:
setattr(self, prop, kwargs[prop])
# We send notif only if object has changed
if old_json != self.__json__():
self.emit_notification("project.updated", self.__json__())
if old_json != self.asdict():
self.emit_notification("project.updated", self.asdict())
self.dump()
# update on computes
@@ -589,7 +589,7 @@ class Project:
node = await self._create_node(compute, name, node_id, node_type, **kwargs)
else:
node = await self._create_node(compute, name, node_id, node_type, **kwargs)
self.emit_notification("node.created", node.__json__())
self.emit_notification("node.created", node.asdict())
if dump:
self.dump()
return node
@@ -618,7 +618,7 @@ class Project:
# refresh the compute IDs list
self._computes = [n.compute.id for n in self.nodes.values()]
self.dump()
self.emit_notification("node.deleted", node.__json__())
self.emit_notification("node.deleted", node.asdict())
@open_required
def get_node(self, node_id):
@@ -683,7 +683,7 @@ class Project:
if drawing_id not in self._drawings:
drawing = Drawing(self, drawing_id=drawing_id, **kwargs)
self._drawings[drawing.id] = drawing
self.emit_notification("drawing.created", drawing.__json__())
self.emit_notification("drawing.created", drawing.asdict())
if dump:
self.dump()
return drawing
@@ -706,7 +706,7 @@ class Project:
raise ControllerError(f"Drawing ID {drawing_id} cannot be deleted because it is locked")
del self._drawings[drawing.id]
self.dump()
self.emit_notification("drawing.deleted", drawing.__json__())
self.emit_notification("drawing.deleted", drawing.asdict())
@open_required
async def add_link(self, link_id=None, dump=True):
@@ -733,7 +733,7 @@ class Project:
if force_delete is False:
raise
self.dump()
self.emit_notification("link.deleted", link.__json__())
self.emit_notification("link.deleted", link.asdict())
@open_required
def get_link(self, link_id):
@@ -810,7 +810,7 @@ class Project:
self._clean_pictures()
self._status = "closed"
if not ignore_notification:
self.emit_notification("project.closed", self.__json__())
self.emit_notification("project.closed", self.asdict())
self.reset()
self._closing = False
@@ -1180,7 +1180,7 @@ class Project:
if node.status != "stopped" and not node.is_always_running():
raise ControllerError("Cannot duplicate node data while the node is running")
data = copy.deepcopy(node.__json__(topology_dump=True))
data = copy.deepcopy(node.asdict(topology_dump=True))
# Some properties like internal ID should not be duplicated
for unique_property in (
"node_id",
@@ -1220,7 +1220,7 @@ class Project:
"snapshots": len(self._snapshots),
}
def __json__(self):
def asdict(self):
return {
"name": self._name,
"project_id": self._id,
+2 -2
View File
@@ -133,10 +133,10 @@ class Snapshot:
except (OSError, PermissionError) as e:
raise ControllerError(str(e))
await project.open()
self._project.emit_notification("snapshot.restored", self.__json__())
self._project.emit_notification("snapshot.restored", self.asdict())
return self._project
def __json__(self):
def asdict(self):
return {
"snapshot_id": self._id,
"name": self._name,
+12 -8
View File
@@ -30,6 +30,10 @@ from ..version import __version__
from ..utils.qt import qt_font_to_style
from ..compute.dynamips import PLATFORMS_DEFAULT_RAM
from .controller_error import ControllerError
from .compute import Compute
from .drawing import Drawing
from .node import Node
from .link import Link
from gns3server.schemas.controller.topology import Topology
from gns3server.schemas.compute.dynamips_nodes import DynamipsCreate
@@ -89,23 +93,23 @@ def project_to_topology(project):
}
for node in project.nodes.values():
if hasattr(node, "__json__"):
data["topology"]["nodes"].append(node.__json__(topology_dump=True))
if isinstance(node, Node):
data["topology"]["nodes"].append(node.asdict(topology_dump=True))
else:
data["topology"]["nodes"].append(node)
for link in project.links.values():
if hasattr(link, "__json__"):
data["topology"]["links"].append(link.__json__(topology_dump=True))
if isinstance(link, Link):
data["topology"]["links"].append(link.asdict(topology_dump=True))
else:
data["topology"]["links"].append(link)
for drawing in project.drawings.values():
if hasattr(drawing, "__json__"):
data["topology"]["drawings"].append(drawing.__json__(topology_dump=True))
if isinstance(drawing, Drawing):
data["topology"]["drawings"].append(drawing.asdict(topology_dump=True))
else:
data["topology"]["drawings"].append(drawing)
for compute in project.computes:
if hasattr(compute, "__json__"):
compute = compute.__json__(topology_dump=True)
if isinstance(compute, Compute):
compute = compute.asdict(topology_dump=True)
if compute["compute_id"] not in (
"vm",
"local",