mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
refactor: ruff autofixes
This commit is contained in:
455 files changed
+12322
-14762
No files matched your search
@@ -657,9 +657,7 @@ class Project:
|
||||
return node
|
||||
|
||||
@open_required
|
||||
async def add_node(
|
||||
self, compute, name, node_id, dump=True, node_type=None, allow_missing_image=False, **kwargs
|
||||
):
|
||||
async def add_node(self, compute, name, node_id, dump=True, node_type=None, allow_missing_image=False, **kwargs):
|
||||
"""
|
||||
Create a node or return an existing node
|
||||
|
||||
@@ -846,10 +844,7 @@ class Project:
|
||||
try:
|
||||
await link.update_filters(link_data["filters"])
|
||||
except ControllerError as e:
|
||||
log.warning(
|
||||
"Dropping invalid filters on link %s: %s",
|
||||
link_data.get("link_id"), e
|
||||
)
|
||||
log.warning("Dropping invalid filters on link %s: %s", link_data.get("link_id"), e)
|
||||
# Restore traffic-insight markers directly into link state (mirrors how
|
||||
# filters are restored via update_filters). The capture_node_id persisted
|
||||
# last time is reused for NIO routing; no side resolution is possible here
|
||||
@@ -865,7 +860,9 @@ class Project:
|
||||
if not result.get("valid"):
|
||||
log.warning(
|
||||
"Dropping marker %s on link %s: invalid BPF (%s)",
|
||||
name, link_data.get("link_id"), result.get("error")
|
||||
name,
|
||||
link_data.get("link_id"),
|
||||
result.get("error"),
|
||||
)
|
||||
continue
|
||||
link._markers[name] = {
|
||||
@@ -935,7 +932,9 @@ class Project:
|
||||
if not result.get("valid"):
|
||||
log.warning(
|
||||
"Dropping marker %s on link %s: invalid BPF (%s)",
|
||||
name, link_data.get("link_id"), result.get("error")
|
||||
name,
|
||||
link_data.get("link_id"),
|
||||
result.get("error"),
|
||||
)
|
||||
continue
|
||||
link._markers[name] = {
|
||||
@@ -996,7 +995,9 @@ class Project:
|
||||
link._deferred = True
|
||||
log.info(
|
||||
"Project '%s' [%s]: deferring link %s until missing image(s) are resolved",
|
||||
self._name, self._id, link.id,
|
||||
self._name,
|
||||
self._id,
|
||||
link.id,
|
||||
)
|
||||
return None
|
||||
# Apply project-level marker definitions onto the link's memory
|
||||
@@ -1151,7 +1152,8 @@ class Project:
|
||||
self._marker_definitions[name]["paused"] = True
|
||||
marker_name = f"global-{name}"
|
||||
affected = [
|
||||
link for link in self._links.values()
|
||||
link
|
||||
for link in self._links.values()
|
||||
if marker_name in link.markers and link.markers[marker_name].get("inherited_from") == name
|
||||
]
|
||||
await self._marker_apply_concurrently(
|
||||
@@ -1170,7 +1172,8 @@ class Project:
|
||||
self._marker_definitions[name]["paused"] = False
|
||||
marker_name = f"global-{name}"
|
||||
affected = [
|
||||
link for link in self._links.values()
|
||||
link
|
||||
for link in self._links.values()
|
||||
if marker_name in link.markers and link.markers[marker_name].get("inherited_from") == name
|
||||
]
|
||||
await self._marker_apply_concurrently(
|
||||
@@ -1199,9 +1202,7 @@ class Project:
|
||||
"""
|
||||
result = validate_bpf_syntax(bpf)
|
||||
if not result.get("valid"):
|
||||
raise ControllerError(
|
||||
f"Marker definition '{name}': invalid BPF — {result.get('error', 'unknown error')}"
|
||||
)
|
||||
raise ControllerError(f"Marker definition '{name}': invalid BPF — {result.get('error', 'unknown error')}")
|
||||
|
||||
def _validate_marker_definition_direction(self, name, direction):
|
||||
"""
|
||||
@@ -1223,7 +1224,9 @@ class Project:
|
||||
"For a capture-node-relative direction on a single link, use a per-link marker."
|
||||
)
|
||||
|
||||
async def create_marker_definition(self, name, bpf, tag=None, direction=None, color=None, highlight_duration=None, data_link_type="DLT_EN10MB"):
|
||||
async def create_marker_definition(
|
||||
self, name, bpf, tag=None, direction=None, color=None, highlight_duration=None, data_link_type="DLT_EN10MB"
|
||||
):
|
||||
"""
|
||||
Create a project-level marker definition and fan out to every existing
|
||||
link that has a capable node. Links without a capable node are silently
|
||||
@@ -1231,26 +1234,32 @@ class Project:
|
||||
"""
|
||||
|
||||
if name in self._marker_definitions:
|
||||
raise ControllerError(
|
||||
f"Marker definition '{name}' already exists in this project"
|
||||
)
|
||||
raise ControllerError(f"Marker definition '{name}' already exists in this project")
|
||||
|
||||
self._validate_marker_definition_bpf(name, bpf)
|
||||
self._validate_marker_definition_direction(name, direction)
|
||||
self._marker_definitions[name] = {"bpf": bpf, "tag": tag, "direction": direction, "color": color, "highlight_duration": highlight_duration, "data_link_type": data_link_type, "paused": False}
|
||||
self._marker_definitions[name] = {
|
||||
"bpf": bpf,
|
||||
"tag": tag,
|
||||
"direction": direction,
|
||||
"color": color,
|
||||
"highlight_duration": highlight_duration,
|
||||
"data_link_type": data_link_type,
|
||||
"paused": False,
|
||||
}
|
||||
await self._apply_def_to_all_links(name)
|
||||
self.dump()
|
||||
self.emit_notification("project.updated", self.asdict())
|
||||
|
||||
async def update_marker_definition(self, name, bpf=None, tag=None, direction=_UNSET, color=None, highlight_duration=None, data_link_type=_UNSET):
|
||||
async def update_marker_definition(
|
||||
self, name, bpf=None, tag=None, direction=_UNSET, color=None, highlight_duration=None, data_link_type=_UNSET
|
||||
):
|
||||
"""
|
||||
Update a marker definition and sync every inherited copy on every link.
|
||||
"""
|
||||
|
||||
if name not in self._marker_definitions:
|
||||
raise ControllerNotFoundError(
|
||||
f"Marker definition '{name}' not found in this project"
|
||||
)
|
||||
raise ControllerNotFoundError(f"Marker definition '{name}' not found in this project")
|
||||
|
||||
d = self._marker_definitions[name]
|
||||
if bpf is not None:
|
||||
@@ -1270,9 +1279,9 @@ class Project:
|
||||
|
||||
# Links that currently carry an inherited copy of this definition.
|
||||
affected = [
|
||||
link for link in self._links.values()
|
||||
if f"global-{name}" in link.markers
|
||||
and link.markers[f"global-{name}"].get("inherited_from") == name
|
||||
link
|
||||
for link in self._links.values()
|
||||
if f"global-{name}" in link.markers and link.markers[f"global-{name}"].get("inherited_from") == name
|
||||
]
|
||||
|
||||
if data_link_type is not _UNSET:
|
||||
@@ -1291,9 +1300,15 @@ class Project:
|
||||
for link in affected:
|
||||
try:
|
||||
await link.update_marker(
|
||||
f"global-{name}", bpf=d["bpf"], tag=d.get("tag"), direction=d.get("direction"),
|
||||
color=d.get("color"), highlight_duration=d.get("highlight_duration"), inherited=True,
|
||||
dump=False, memory_only=True
|
||||
f"global-{name}",
|
||||
bpf=d["bpf"],
|
||||
tag=d.get("tag"),
|
||||
direction=d.get("direction"),
|
||||
color=d.get("color"),
|
||||
highlight_duration=d.get("highlight_duration"),
|
||||
inherited=True,
|
||||
dump=False,
|
||||
memory_only=True,
|
||||
)
|
||||
except ControllerError as e:
|
||||
log.warning("Failed to sync marker global-%s on link %s: %s", name, link.id, e)
|
||||
@@ -1307,16 +1322,14 @@ class Project:
|
||||
"""
|
||||
|
||||
if name not in self._marker_definitions:
|
||||
raise ControllerNotFoundError(
|
||||
f"Marker definition '{name}' not found in this project"
|
||||
)
|
||||
raise ControllerNotFoundError(f"Marker definition '{name}' not found in this project")
|
||||
|
||||
del self._marker_definitions[name]
|
||||
|
||||
affected = [
|
||||
link for link in self._links.values()
|
||||
if f"global-{name}" in link.markers
|
||||
and link.markers[f"global-{name}"].get("inherited_from") == name
|
||||
link
|
||||
for link in self._links.values()
|
||||
if f"global-{name}" in link.markers and link.markers[f"global-{name}"].get("inherited_from") == name
|
||||
]
|
||||
for link in affected:
|
||||
try:
|
||||
@@ -1382,9 +1395,7 @@ class Project:
|
||||
)
|
||||
|
||||
if per_compute:
|
||||
await asyncio.gather(
|
||||
*[_dispatch(c, n) for c, n in per_compute.items()]
|
||||
)
|
||||
await asyncio.gather(*[_dispatch(c, n) for c, n in per_compute.items()])
|
||||
|
||||
async def apply_defs_to_new_link(self, link):
|
||||
"""
|
||||
@@ -1403,10 +1414,7 @@ class Project:
|
||||
# after; per-def dumps here would be N full topology writes.
|
||||
await link.inherit_marker(def_name, d, dump=False)
|
||||
except ControllerError as e:
|
||||
log.warning(
|
||||
"Marker definition '%s' could not be applied to new link %s: %s",
|
||||
def_name, link.id, e
|
||||
)
|
||||
log.warning("Marker definition '%s' could not be applied to new link %s: %s", def_name, link.id, e)
|
||||
|
||||
async def _marker_apply_concurrently(self, links, operation, fail_msg):
|
||||
"""
|
||||
@@ -1428,10 +1436,7 @@ class Project:
|
||||
if not links:
|
||||
return
|
||||
_t0 = time.time()
|
||||
log.info(
|
||||
"Project '%s' [%s]: fanning out marker operation to %d links...",
|
||||
self._name, self._id, len(links)
|
||||
)
|
||||
log.info("Project '%s' [%s]: fanning out marker operation to %d links...", self._name, self._id, len(links))
|
||||
sem = asyncio.Semaphore(32)
|
||||
|
||||
async def guarded(link):
|
||||
@@ -1442,10 +1447,7 @@ class Project:
|
||||
log.warning(fail_msg(link, e))
|
||||
|
||||
await asyncio.gather(*(guarded(link) for link in links))
|
||||
log.info(
|
||||
"Project '%s' [%s]: marker fan-out done in %.2fs",
|
||||
self._name, self._id, time.time() - _t0
|
||||
)
|
||||
log.info("Project '%s' [%s]: marker fan-out done in %.2fs", self._name, self._id, time.time() - _t0)
|
||||
|
||||
@property
|
||||
def snapshots(self):
|
||||
@@ -1512,7 +1514,7 @@ class Project:
|
||||
for snapshot in self._snapshots.values():
|
||||
self._snapshot_conf.append(snapshot.asdict())
|
||||
try:
|
||||
with open(self._snapshot_conf_path, 'w+') as f:
|
||||
with open(self._snapshot_conf_path, "w+") as f:
|
||||
json.dump(self._snapshot_conf, f, indent=4)
|
||||
except OSError as e:
|
||||
log.error("Cannot write snapshot config '{}': {}".format(self._snapshot_conf_path, e))
|
||||
@@ -1874,8 +1876,7 @@ class Project:
|
||||
result = validate_bpf_syntax(bpf)
|
||||
if not result.get("valid"):
|
||||
log.warning(
|
||||
"Dropping marker definition '%s' on load: invalid BPF (%s)",
|
||||
def_name, result.get("error")
|
||||
"Dropping marker definition '%s' on load: invalid BPF (%s)", def_name, result.get("error")
|
||||
)
|
||||
continue
|
||||
clean_defs[def_name] = d
|
||||
@@ -1986,9 +1987,7 @@ class Project:
|
||||
)
|
||||
|
||||
if per_compute:
|
||||
await asyncio.gather(
|
||||
*[_dispatch_batch(c, n) for c, n in per_compute.items()]
|
||||
)
|
||||
await asyncio.gather(*[_dispatch_batch(c, n) for c, n in per_compute.items()])
|
||||
|
||||
# Finalise every link: wire node/port back-references, mark created,
|
||||
# notify clients, and apply project-level marker definitions.
|
||||
@@ -2096,12 +2095,7 @@ class Project:
|
||||
# Do not compress the exported project when duplicating
|
||||
with aiozipstream.ZipFile(compression=zipfile.ZIP_STORED) as zstream:
|
||||
await export_project(
|
||||
zstream,
|
||||
self,
|
||||
tmpdir,
|
||||
keep_compute_ids=True,
|
||||
include_snapshots=True,
|
||||
allow_all_nodes=True
|
||||
zstream, self, tmpdir, keep_compute_ids=True, include_snapshots=True, allow_all_nodes=True
|
||||
)
|
||||
|
||||
# export the project to a temporary location
|
||||
@@ -2120,7 +2114,7 @@ class Project:
|
||||
f,
|
||||
name=name,
|
||||
reset_mac_addresses=reset_mac_addresses,
|
||||
keep_compute_ids=True
|
||||
keep_compute_ids=True,
|
||||
)
|
||||
|
||||
log.info(f"Project '{project.name}' duplicated in {time.time() - begin:.4f} seconds")
|
||||
@@ -2161,8 +2155,12 @@ class Project:
|
||||
else:
|
||||
new_project_path = p_work.joinpath(new_project_id)
|
||||
# copy dir
|
||||
await wait_run_in_executor(shutil.copytree, self.path, new_project_path.as_posix(), symlinks=True, ignore_dangling_symlinks=True)
|
||||
log.info("Project content copied from '{}' to '{}' in {}s".format(self.path, new_project_path, time.time() - t0))
|
||||
await wait_run_in_executor(
|
||||
shutil.copytree, self.path, new_project_path.as_posix(), symlinks=True, ignore_dangling_symlinks=True
|
||||
)
|
||||
log.info(
|
||||
"Project content copied from '{}' to '{}' in {}s".format(self.path, new_project_path, time.time() - t0)
|
||||
)
|
||||
|
||||
# Read the topology file using the actual filename (self._filename), not self.name
|
||||
# This handles the case where a project has been renamed but we need to read the actual file
|
||||
@@ -2181,7 +2179,7 @@ class Project:
|
||||
regenerate_topology_ids(topology, new_project_path, reset_mac_addresses)
|
||||
|
||||
# dump the updated .gns3 project file
|
||||
dot_gns3_path = new_project_path.joinpath('{}.gns3'.format(project_name))
|
||||
dot_gns3_path = new_project_path.joinpath("{}.gns3".format(project_name))
|
||||
topology["project_id"] = new_project_id
|
||||
with open(dot_gns3_path, "w+") as f:
|
||||
json.dump(topology, f, indent=4, sort_keys=True)
|
||||
@@ -2359,13 +2357,7 @@ class Project:
|
||||
data["z"] = z
|
||||
data["locked"] = False # duplicated node must not be locked
|
||||
new_node_uuid = str(uuid.uuid4())
|
||||
new_node = await self.add_node(
|
||||
node.compute,
|
||||
node.name,
|
||||
new_node_uuid,
|
||||
node_type=node_type,
|
||||
**data
|
||||
)
|
||||
new_node = await self.add_node(node.compute, node.name, new_node_uuid, node_type=node_type, **data)
|
||||
try:
|
||||
await node.post("/duplicate", timeout=None, data={"destination_node_id": new_node_uuid})
|
||||
except ControllerNotFoundError:
|
||||
|
||||
Reference in new issue
Block a user