MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _attach_files

Function _attach_files

graphify/tree_html.py:74–168  ·  view source on GitHub ↗

Build one directory tree from ``file_nodes`` (all carry ``source_file``). Returns ``(root_node, matched_files, by_file)``. ``matched_files`` counts the files that sit under ``root``; files outside it attach flat to the root. Each call owns its own ``dir_index``, so two calls never share

(
    file_nodes: List[Dict[str, Any]],
    *,
    root: str,
    max_children: int,
    label: Optional[str],
    directory_ids: set[int],
)

Source from the content-addressed store, hash-verified

72
73
74def _attach_files(
75 file_nodes: List[Dict[str, Any]],
76 *,
77 root: str,
78 max_children: int,
79 label: Optional[str],
80 directory_ids: set[int],
81) -> "tuple[Dict[str, Any], int, Dict[str, List[Dict[str, Any]]]]":
82 """Build one directory tree from ``file_nodes`` (all carry ``source_file``).
83
84 Returns ``(root_node, matched_files, by_file)``. ``matched_files`` counts
85 the files that sit under ``root``; files outside it attach flat to the
86 root. Each call owns its own ``dir_index``, so two calls never share
87 directory nodes even when their relative paths are identical.
88 """
89 root_path = Path(root)
90
91 by_file: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
92 for n in file_nodes:
93 if not isinstance(n, dict):
94 continue
95 src_val = n.get("source_file")
96 if isinstance(src_val, str) and src_val.strip():
97 by_file[src_val.strip()].append(n)
98
99 # Build dir tree.
100 dir_index: Dict[str, Dict[str, Any]] = {}
101 label_root = label or root_path.name or root or "/"
102 root_node: Dict[str, Any] = {
103 "name": label_root, "total_count": 0, "children": [],
104 }
105 dir_index[str(root_path)] = root_node
106 directory_ids.add(id(root_node))
107
108 def _ensure_dir(abs_path: Path) -> Dict[str, Any]:
109 key = str(abs_path)
110 if key in dir_index:
111 return dir_index[key]
112 if abs_path == abs_path.parent:
113 return root_node
114 parent = (_ensure_dir(abs_path.parent)
115 if abs_path.parent != abs_path else root_node)
116 node = {"name": abs_path.name, "total_count": 0, "children": []}
117 dir_index[key] = node
118 directory_ids.add(id(node))
119 parent["children"].append(node)
120 return node
121
122 matched_files = 0
123 for src_file, syms in sorted(by_file.items()):
124 src_path = Path(src_file)
125 try:
126 rel = src_path.relative_to(root_path)
127 parent_path = (root_path / rel).parent
128 matched_files += 1
129 except ValueError:
130 parent_path = root_path
131 parent_dir = _ensure_dir(parent_path)

Callers 1

build_treeFunction · 0.85

Calls 5

_is_file_node_labelFunction · 0.90
_ensure_dirFunction · 0.85
_make_truncation_leafFunction · 0.85
getMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected