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

- 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:
Ryan Malloy 2025-08-11 17:54:14 -06:00
parent bc0f3db97c
commit 50f17eff35

View File

@ -2,6 +2,7 @@
Analysis and validation tools for KiCad projects. Analysis and validation tools for KiCad projects.
""" """
import json
import os import os
from typing import Any from typing import Any
@ -19,12 +20,49 @@ def register_analysis_tools(mcp: FastMCP) -> None:
@mcp.tool() @mcp.tool()
def validate_project(project_path: str) -> dict[str, Any]: 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): 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 = [] 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 # Check for essential files
if "pcb" not in files: if "pcb" not in files:
@ -33,14 +71,12 @@ def register_analysis_tools(mcp: FastMCP) -> None:
if "schematic" not in files: if "schematic" not in files:
issues.append("Missing schematic file") issues.append("Missing schematic file")
# Validate project file # Validate project file JSON format
try: try:
with open(project_path) as f: with open(project_path) as f:
import json
json.load(f) json.load(f)
except json.JSONDecodeError: except json.JSONDecodeError as e:
issues.append("Invalid project file format (JSON parsing error)") issues.append(f"Invalid project file format (JSON parsing error): {str(e)}")
except Exception as e: except Exception as e:
issues.append(f"Error reading project file: {str(e)}") issues.append(f"Error reading project file: {str(e)}")