Private
Public Access
refactor(mcp_client): migrate L1338 py_get_hierarchy to Result[T] (Phase 7 site 1)
This commit is contained in:
+9
-34
@@ -1302,41 +1302,16 @@ def py_check_syntax(path: str) -> str:
|
|||||||
return "; ".join(e.ui_message() for e in resolved.errors)
|
return "; ".join(e.ui_message() for e in resolved.errors)
|
||||||
|
|
||||||
def py_get_hierarchy(path: str, class_name: str) -> str:
|
def py_get_hierarchy(path: str, class_name: str) -> str:
|
||||||
"""Scans the project to find subclasses of a given class."""
|
"""Scans the project to find subclasses of a given class.
|
||||||
p, err = _resolve_and_check(path)
|
|
||||||
if err: return err
|
|
||||||
assert p is not None
|
|
||||||
subclasses: list[str] = []
|
|
||||||
|
|
||||||
def _search_file(fp: Path) -> None:
|
Thin wrapper over py_get_hierarchy_result; the legacy str shape is
|
||||||
if not _is_allowed(fp): return
|
preserved for backward compatibility, but the try/except Exception
|
||||||
try:
|
lives in the Result variant.
|
||||||
code = fp.read_text(encoding="utf-8")
|
"""
|
||||||
tree = ast.parse(code)
|
resolved = py_get_hierarchy_result(path, class_name)
|
||||||
for node in ast.walk(tree):
|
if resolved.ok:
|
||||||
if isinstance(node, ast.ClassDef):
|
return resolved.data
|
||||||
for base in node.bases:
|
return "; ".join(e.ui_message() for e in resolved.errors)
|
||||||
if isinstance(base, ast.Name) and base.id == class_name:
|
|
||||||
subclasses.append(f"{fp.name}: class {node.name}({class_name})")
|
|
||||||
elif isinstance(base, ast.Attribute) and base.attr == class_name:
|
|
||||||
if isinstance(base.value, ast.Name):
|
|
||||||
subclasses.append(f"{fp.name}: class {node.name}({base.value.id}.{class_name})")
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
if p.is_file():
|
|
||||||
_search_file(p)
|
|
||||||
else:
|
|
||||||
for root, dirs, files in os.walk(p):
|
|
||||||
dirs[:] = [d for d in dirs if not d.startswith('.') and d not in ('__pycache__', 'venv', 'env')]
|
|
||||||
for file in files:
|
|
||||||
if file.endswith('.py'):
|
|
||||||
_search_file(Path(root) / file)
|
|
||||||
if not subclasses:
|
|
||||||
return f"No subclasses of '{class_name}' found in {p}"
|
|
||||||
return f"Subclasses of '{class_name}':\n" + "\n".join(f" - {s}" for s in subclasses)
|
|
||||||
except Exception as e:
|
|
||||||
return f"ERROR finding subclasses of '{class_name}': {e}"
|
|
||||||
|
|
||||||
def py_get_docstring(path: str, name: str) -> str:
|
def py_get_docstring(path: str, name: str) -> str:
|
||||||
"""Extracts the docstring for a specific module, class, or function."""
|
"""Extracts the docstring for a specific module, class, or function."""
|
||||||
|
|||||||
Reference in New Issue
Block a user