Merge pull request #203 from jbjardine/cojbdev/kicad-mcp-lib-table-spaces

fix: parse quoted KiCad library table URIs with spaces
This commit is contained in:
mixelpixx
2026-05-30 09:33:09 -04:00
committed by GitHub
3 changed files with 90 additions and 23 deletions

View File

@@ -91,14 +91,19 @@ class LibraryManager:
with open(table_path, "r") as f:
content = f.read()
# Simple regex-based parser for lib entries
# Pattern: (lib (name "NAME")(type TYPE)(uri "URI")...)
lib_pattern = r'\(lib\s+\(name\s+"?([^")\s]+)"?\)\s*\(type\s+"?([^")\s]+)"?\)\s*\(uri\s+"?([^")\s]+)"?'
# Simple regex-based parser for lib entries. Name, type, and URI
# may be quoted or bare; quoted URIs can contain spaces.
lib_pattern = (
r"\(lib\s+"
r'\(name\s+(?:"([^"]+)"|([^"\)\s]+))\)\s*'
r'\(type\s+(?:"([^"]+)"|([^"\)\s]+))\)\s*'
r'\(uri\s+(?:"([^"]+)"|([^"\)\s]+))'
)
for match in re.finditer(lib_pattern, content, re.IGNORECASE):
nickname = match.group(1)
lib_type = match.group(2)
uri = match.group(3)
nickname = match.group(1) or match.group(2)
lib_type = match.group(3) or match.group(4)
uri = match.group(5) or match.group(6)
if lib_type.lower() == "table":
table_uri = uri

View File

@@ -143,14 +143,19 @@ class SymbolLibraryManager:
with open(table_path, "r", encoding="utf-8") as f:
content = f.read()
# Simple regex-based parser for lib entries
# Pattern: (lib (name "NAME")(type TYPE)(uri "URI")...)
lib_pattern = r'\(lib\s+\(name\s+"?([^")\s]+)"?\)\s*\(type\s+"?([^")\s]+)"?\)\s*\(uri\s+"?([^")\s]+)"?'
# Simple regex-based parser for lib entries. Name, type, and URI
# may be quoted or bare; quoted URIs can contain spaces.
lib_pattern = (
r"\(lib\s+"
r'\(name\s+(?:"([^"]+)"|([^"\)\s]+))\)\s*'
r'\(type\s+(?:"([^"]+)"|([^"\)\s]+))\)\s*'
r'\(uri\s+(?:"([^"]+)"|([^"\)\s]+))'
)
for match in re.finditer(lib_pattern, content, re.IGNORECASE):
nickname = match.group(1)
lib_type = match.group(2)
uri = match.group(3)
nickname = match.group(1) or match.group(2)
lib_type = match.group(3) or match.group(4)
uri = match.group(5) or match.group(6)
if lib_type.lower() == "table":
table_uri = uri
@@ -587,9 +592,7 @@ class SymbolLibraryCommands:
if value:
start = Path(value).expanduser().parent
for ancestor in [start, *start.parents]:
if (ancestor / "sym-lib-table").exists() or list(
ancestor.glob("*.kicad_pro")
):
if (ancestor / "sym-lib-table").exists() or list(ancestor.glob("*.kicad_pro")):
return ancestor
return start