mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
refactor: use explicit conversion in f-strings
This commit is contained in:
32 files changed
+65
-65
No files matched your search
@@ -527,7 +527,7 @@ def tool_node(state: dict, config: RunnableConfig | None = None):
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Tool %s failed: %s", tool_name, e, exc_info=True)
|
||||
observation = f"Error: {str(e)}"
|
||||
observation = f"Error: {e!s}"
|
||||
|
||||
# Serialize observation to JSON string if it's not already a string
|
||||
# This ensures ToolMessage.content is always JSON format, not Python
|
||||
|
||||
@@ -175,7 +175,7 @@ class Gns3Connector:
|
||||
else:
|
||||
raise HTTPError(f"v3 API authentication failed: {response.status_code} - {response.text}")
|
||||
except Exception as e:
|
||||
raise HTTPError(f"v3 API authentication error: {str(e)}") from e
|
||||
raise HTTPError(f"v3 API authentication error: {e!s}") from e
|
||||
|
||||
def _is_token_expired(self) -> bool:
|
||||
"""
|
||||
@@ -273,7 +273,7 @@ class Gns3Connector:
|
||||
except Exception:
|
||||
# If JSON parsing fails, return error with original text
|
||||
return HTTPError(
|
||||
f"Original Error: {str(e)}. GNS3 response text: {response.text}",
|
||||
f"Original Error: {e!s}. GNS3 response text: {response.text}",
|
||||
response=response,
|
||||
)
|
||||
return e
|
||||
@@ -143,7 +143,7 @@ class GNS3ProjectInfoTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error retrieving GNS3 project info: %s", str(e))
|
||||
return {"error": f"Failed to retrieve project info: {str(e)}"}
|
||||
return {"error": f"Failed to retrieve project info: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -145,7 +145,7 @@ class GNS3TopologyTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error retrieving GNS3 topology: %s", str(e))
|
||||
return {"error": f"Failed to retrieve topology: {str(e)}"}
|
||||
return {"error": f"Failed to retrieve topology: {e!s}"}
|
||||
|
||||
def _clean_nodes_ports(self, data: dict) -> dict:
|
||||
"""
|
||||
|
||||
@@ -239,7 +239,7 @@ def reload_injection_skills() -> dict[str, Any]:
|
||||
logger.error(f"Error during skills reload: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"Error: {str(e)}",
|
||||
"message": f"Error: {e!s}",
|
||||
"skill_count": len(INJECTION_SKILLS_REGISTRY),
|
||||
"version": "",
|
||||
}
|
||||
@@ -278,7 +278,7 @@ def reload_prompts() -> dict[str, Any]:
|
||||
manager = get_skills_manager()
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"Error: {str(e)}",
|
||||
"message": f"Error: {e!s}",
|
||||
"prompt_count": manager.get_prompt_count() if manager else 0,
|
||||
"version": "",
|
||||
}
|
||||
@@ -314,7 +314,7 @@ def reload_forbidden_commands() -> dict[str, Any]:
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error during forbidden commands reload: {e}")
|
||||
return {"success": False, "message": f"Error: {str(e)}", "command_count": 0, "version": ""}
|
||||
return {"success": False, "message": f"Error: {e!s}", "command_count": 0, "version": ""}
|
||||
|
||||
|
||||
def get_prompt(prompt_name: str) -> str:
|
||||
|
||||
@@ -255,7 +255,7 @@ class ExecuteMultipleDeviceConfigCommands(BaseTool):
|
||||
except Exception as e:
|
||||
# Overall execution failed
|
||||
logger.error("Error executing configurations on all devices: %s", e)
|
||||
return [{"status": "failed", "error": f"Execution error: {str(e)}"}]
|
||||
return [{"status": "failed", "error": f"Execution error: {e!s}"}]
|
||||
|
||||
logger.info(
|
||||
"Multiple device configuration execution completed. Results: %s",
|
||||
@@ -285,7 +285,7 @@ class ExecuteMultipleDeviceConfigCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"Configuration failed (ReadTimeout): {str(e)}",
|
||||
result=f"Configuration failed (ReadTimeout): {e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
@@ -305,7 +305,7 @@ class ExecuteMultipleDeviceConfigCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"Configuration failed (Unhandled Exception): {str(e)}",
|
||||
result=f"Configuration failed (Unhandled Exception): {e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -253,7 +253,7 @@ class ExecuteMultipleDeviceCommands(BaseTool):
|
||||
except Exception as e:
|
||||
# Overall execution failed
|
||||
logger.error("Error executing display on all devices: %s", e)
|
||||
return [{"status": "failed", "error": f"Execution error: {str(e)}"}]
|
||||
return [{"status": "failed", "error": f"Execution error: {e!s}"}]
|
||||
|
||||
logger.debug(
|
||||
"Multiple device display execution completed. Results: %s",
|
||||
@@ -288,7 +288,7 @@ class ExecuteMultipleDeviceCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"diagnostic command failed (ReadTimeout): {str(e)}",
|
||||
result=f"diagnostic command failed (ReadTimeout): {e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
@@ -313,7 +313,7 @@ class ExecuteMultipleDeviceCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"diagnostic command failed (Unhandled): {str(e)}",
|
||||
result=f"diagnostic command failed (Unhandled): {e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ class GNS3LinkTool(BaseTool):
|
||||
created_links.append(link_info)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Failed to create link {i}: {str(e)}"
|
||||
error_msg = f"Failed to create link {i}: {e!s}"
|
||||
logger.error(error_msg)
|
||||
created_links.append({"error": error_msg})
|
||||
|
||||
@@ -257,7 +257,7 @@ class GNS3LinkTool(BaseTool):
|
||||
return [{"error": f"Invalid JSON input: {e}"}]
|
||||
except Exception as e:
|
||||
logger.error("Failed to process link creation: %s", e)
|
||||
return [{"error": f"Failed to process link creation: {str(e)}"}]
|
||||
return [{"error": f"Failed to process link creation: {e!s}"}]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -244,7 +244,7 @@ class GNS3CreateNodeTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
error_info = {
|
||||
"error": f"Node {i + 1} creation failed: {str(e)}",
|
||||
"error": f"Node {i + 1} creation failed: {e!s}",
|
||||
"status": "failed",
|
||||
}
|
||||
results.append(error_info)
|
||||
@@ -280,7 +280,7 @@ class GNS3CreateNodeTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to process node creation request: %s", e)
|
||||
return {"error": f"Failed to process node creation request: {str(e)}"}
|
||||
return {"error": f"Failed to process node creation request: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -168,7 +168,7 @@ class GNS3TemplateTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to connect to GNS3 server or retrieve templates: %s", e)
|
||||
return {"error": f"Failed to retrieve templates: {str(e)}"}
|
||||
return {"error": f"Failed to retrieve templates: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -239,7 +239,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to process packet filter request: %s", e)
|
||||
return {"error": f"Failed to process packet filter request: {str(e)}"}
|
||||
return {"error": f"Failed to process packet filter request: {e!s}"}
|
||||
|
||||
def _get_available_filters(self, gns3_ctx: dict, project_id: str, link_id: str) -> dict[str, Any]:
|
||||
"""Get available filter types for the link."""
|
||||
@@ -260,7 +260,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
return {
|
||||
"action": "get_available",
|
||||
"link_id": link_id,
|
||||
"error": f"Failed to get available filters: {str(e)}",
|
||||
"error": f"Failed to get available filters: {e!s}",
|
||||
"status": "failed",
|
||||
}
|
||||
|
||||
@@ -318,7 +318,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error during BPF validation: %s", e)
|
||||
return {"valid": False, "error": f"BPF validation error: {str(e)}"}
|
||||
return {"valid": False, "error": f"BPF validation error: {e!s}"}
|
||||
|
||||
def _set_filters(
|
||||
self,
|
||||
@@ -384,7 +384,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
return {
|
||||
"action": "set",
|
||||
"link_id": link_id,
|
||||
"error": f"Failed to set filters: {str(e)}",
|
||||
"error": f"Failed to set filters: {e!s}",
|
||||
"status": "failed",
|
||||
}
|
||||
|
||||
@@ -407,7 +407,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
return {
|
||||
"action": "get",
|
||||
"link_id": link_id,
|
||||
"error": f"Failed to get filters: {str(e)}",
|
||||
"error": f"Failed to get filters: {e!s}",
|
||||
"status": "failed",
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ class GNS3PacketFilterTool(BaseTool):
|
||||
return {
|
||||
"action": "clear",
|
||||
"link_id": link_id,
|
||||
"error": f"Failed to clear filters: {str(e)}",
|
||||
"error": f"Failed to clear filters: {e!s}",
|
||||
"status": "failed",
|
||||
}
|
||||
|
||||
|
||||
@@ -220,7 +220,7 @@ class GNS3StartNodeTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to start nodes: %s", e)
|
||||
return {"error": f"Failed to start nodes: {str(e)}"}
|
||||
return {"error": f"Failed to start nodes: {e!s}"}
|
||||
|
||||
|
||||
# Backward-compat alias: the waiting variant was removed; both names now
|
||||
|
||||
@@ -198,7 +198,7 @@ class GNS3StopNodeTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to stop nodes: %s", e)
|
||||
return {"error": f"Failed to stop nodes: {str(e)}"}
|
||||
return {"error": f"Failed to stop nodes: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -209,7 +209,7 @@ class GNS3SuspendNodeTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to suspend nodes: %s", e)
|
||||
return {"error": f"Failed to suspend nodes: {str(e)}"}
|
||||
return {"error": f"Failed to suspend nodes: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -241,7 +241,7 @@ class GNS3UpdateNodeNameTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Failed to update node names: %s", e)
|
||||
return {"error": f"Failed to update node names: {str(e)}"}
|
||||
return {"error": f"Failed to update node names: {e!s}"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -104,4 +104,4 @@ class GNS3WaitTool(BaseTool):
|
||||
return {"error": f"Invalid JSON input: {e}"}
|
||||
except Exception as e:
|
||||
logger.error("Wait tool failed: %s", e)
|
||||
return {"error": f"Wait tool failed: {str(e)}"}
|
||||
return {"error": f"Wait tool failed: {e!s}"}
|
||||
@@ -325,7 +325,7 @@ class PacketAnalysisTool(BaseTool):
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"PacketAnalysisTool error: {e}", exc_info=True)
|
||||
return f'{{"error": "Analysis failed: {str(e)}"}}'
|
||||
return f'{{"error": "Analysis failed: {e!s}"}}'
|
||||
|
||||
finally:
|
||||
# Clean up temp file
|
||||
@@ -497,7 +497,7 @@ class PacketAnalysisTool(BaseTool):
|
||||
return '{"error": "tshark not installed. Please install tshark: apt install tshark"}'
|
||||
except Exception as e:
|
||||
logger.error(f"tshark execution error: {e}", exc_info=True)
|
||||
return f'{{"error": "tshark failed: {str(e)}"}}'
|
||||
return f'{{"error": "tshark failed: {e!s}"}}'
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -227,7 +227,7 @@ class VPCSCommands(BaseTool):
|
||||
except Exception as e:
|
||||
# Overall execution failed
|
||||
logger.error("Error executing commands on all VPCS devices: %s", e)
|
||||
return [{"status": "failed", "error": f"Execution error: {str(e)}"}]
|
||||
return [{"status": "failed", "error": f"Execution error: {e!s}"}]
|
||||
|
||||
logger.debug(
|
||||
"VPCS command execution completed. Results: %s",
|
||||
@@ -262,7 +262,7 @@ class VPCSCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"Command failed (ReadTimeout): {str(e)}",
|
||||
result=f"Command failed (ReadTimeout): {e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
@@ -289,7 +289,7 @@ class VPCSCommands(BaseTool):
|
||||
)
|
||||
return Result(
|
||||
host=task.host,
|
||||
result=f"Command failed: {str(retry_e)}",
|
||||
result=f"Command failed: {retry_e!s}",
|
||||
failed=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -496,7 +496,7 @@ def _test_parse_tool_content() -> None:
|
||||
for i, (input_data, expected) in enumerate(test_cases):
|
||||
result = parse_tool_content(input_data)
|
||||
status = "✓" if result == expected else "✗"
|
||||
print(f"Test {i + 1}: {status} Input: {repr(input_data)} -> {result}")
|
||||
print(f"Test {i + 1}: {status} Input: {input_data!r} -> {result}")
|
||||
|
||||
print("\nTesting format_tool_response function:")
|
||||
format_tests = [
|
||||
@@ -518,7 +518,7 @@ def _test_parse_tool_content() -> None:
|
||||
valid = "✓"
|
||||
except Exception:
|
||||
valid = "✗"
|
||||
print(f"Format Test {i + 1}: {valid} Input: {repr(input_data)} -> {result}")
|
||||
print(f"Format Test {i + 1}: {valid} Input: {input_data!r} -> {result}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user