Fix validate_project tool to handle directory paths and json import error
Some checks failed
CI / Lint and Format (push) Has been cancelled
CI / Test Python 3.11 on macos-latest (push) Has been cancelled
CI / Test Python 3.12 on macos-latest (push) Has been cancelled
CI / Test Python 3.13 on macos-latest (push) Has been cancelled
CI / Test Python 3.10 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / Build Package (push) Has been cancelled
Some checks failed
CI / Lint and Format (push) Has been cancelled
CI / Test Python 3.11 on macos-latest (push) Has been cancelled
CI / Test Python 3.12 on macos-latest (push) Has been cancelled
CI / Test Python 3.13 on macos-latest (push) Has been cancelled
CI / Test Python 3.10 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / Build Package (push) Has been cancelled
- Move json import to module level to fix NameError - Add directory path handling to automatically find .kicad_pro files - Improve error messages and validation logic - Add proper file type validation Fixes IsADirectoryError and 'json' variable access errors.
This commit is contained in:
parent
bc0f3db97c
commit
50f17eff35
@ -2,6 +2,7 @@
|
||||
Analysis and validation tools for KiCad projects.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
@ -19,12 +20,49 @@ def register_analysis_tools(mcp: FastMCP) -> None:
|
||||
|
||||
@mcp.tool()
|
||||
def validate_project(project_path: str) -> dict[str, Any]:
|
||||
"""Basic validation of a KiCad project."""
|
||||
"""Basic validation of a KiCad project.
|
||||
|
||||
Args:
|
||||
project_path: Path to the KiCad project file (.kicad_pro) or directory containing it
|
||||
|
||||
Returns:
|
||||
Dictionary with validation results
|
||||
"""
|
||||
# Handle directory paths by looking for .kicad_pro file
|
||||
if os.path.isdir(project_path):
|
||||
# Look for .kicad_pro files in the directory
|
||||
kicad_pro_files = [f for f in os.listdir(project_path) if f.endswith('.kicad_pro')]
|
||||
if not kicad_pro_files:
|
||||
return {
|
||||
"valid": False,
|
||||
"error": f"No .kicad_pro file found in directory: {project_path}"
|
||||
}
|
||||
elif len(kicad_pro_files) > 1:
|
||||
return {
|
||||
"valid": False,
|
||||
"error": f"Multiple .kicad_pro files found in directory: {project_path}. Please specify the exact file."
|
||||
}
|
||||
else:
|
||||
project_path = os.path.join(project_path, kicad_pro_files[0])
|
||||
|
||||
if not os.path.exists(project_path):
|
||||
return {"valid": False, "error": f"Project not found: {project_path}"}
|
||||
return {"valid": False, "error": f"Project file not found: {project_path}"}
|
||||
|
||||
if not project_path.endswith('.kicad_pro'):
|
||||
return {
|
||||
"valid": False,
|
||||
"error": f"Invalid file type. Expected .kicad_pro file, got: {project_path}"
|
||||
}
|
||||
|
||||
issues = []
|
||||
files = get_project_files(project_path)
|
||||
|
||||
try:
|
||||
files = get_project_files(project_path)
|
||||
except Exception as e:
|
||||
return {
|
||||
"valid": False,
|
||||
"error": f"Error analyzing project files: {str(e)}"
|
||||
}
|
||||
|
||||
# Check for essential files
|
||||
if "pcb" not in files:
|
||||
@ -33,14 +71,12 @@ def register_analysis_tools(mcp: FastMCP) -> None:
|
||||
if "schematic" not in files:
|
||||
issues.append("Missing schematic file")
|
||||
|
||||
# Validate project file
|
||||
# Validate project file JSON format
|
||||
try:
|
||||
with open(project_path) as f:
|
||||
import json
|
||||
|
||||
json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
issues.append("Invalid project file format (JSON parsing error)")
|
||||
except json.JSONDecodeError as e:
|
||||
issues.append(f"Invalid project file format (JSON parsing error): {str(e)}")
|
||||
except Exception as e:
|
||||
issues.append(f"Error reading project file: {str(e)}")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user