Download scripts/coverage-anchors/folder-tree-new-module.py.snapshot from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/scripts/coverage-anchors/folder-tree-new-module.py.snapshot
- Command line
-
hf download hf://spaces/VoiceOfML/Search/scripts/coverage-anchors/folder-tree-new-module.py.snapshot
-
curl -L -o folder-tree-new-module.py.snapshot https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/scripts/coverage-anchors/folder-tree-new-module.py.snapshot
2.61 kB
| """Build search filter trees from a caller-owned index snapshot.""" | |
| from __future__ import annotations | |
| from collections.abc import Mapping, Sequence | |
| from typing import NotRequired, TypedDict, cast | |
| class FolderTreeNode(TypedDict): | |
| name: str | |
| path: str | |
| children: list[FolderTreeNode] | |
| count: int | |
| hasDirectFiles: bool | |
| hasChildren: bool | |
| showSelfToggle: bool | |
| isRoot: NotRequired[bool] | |
| def build_folder_tree(repo: str, folder_index: Mapping[str, Mapping[str, Sequence[int]]], | |
| repo_records_map: Mapping[str, Sequence[int]], | |
| records: Sequence[dict[str, object]]) -> list[FolderTreeNode]: | |
| """Preserve corpus counts, stable child order and direct-file toggles.""" | |
| if repo not in folder_index: | |
| return [] | |
| repo_name_short = repo.split("/")[-1] | |
| tree_root: FolderTreeNode = { | |
| "name": repo_name_short, | |
| "path": "", | |
| "children": [], | |
| "count": 0, | |
| "hasDirectFiles": False, | |
| "hasChildren": False, | |
| "showSelfToggle": False, | |
| "isRoot": True, | |
| } | |
| node_map: dict[str, FolderTreeNode] = {"": tree_root} | |
| for path, idx_list in folder_index[repo].items(): | |
| if path == "": | |
| tree_root["count"] = len(idx_list) | |
| continue | |
| parts = path.split("/") | |
| for depth in range(1, len(parts) + 1): | |
| key = "/".join(parts[:depth]) | |
| if key not in node_map: | |
| parent_key = "/".join(parts[:depth - 1]) if depth > 1 else "" | |
| parent = node_map[parent_key] | |
| node: FolderTreeNode = { | |
| "name": parts[depth - 1], | |
| "path": key, | |
| "children": [], | |
| "count": 0, | |
| "hasDirectFiles": False, | |
| "hasChildren": False, | |
| "showSelfToggle": False, | |
| } | |
| node_map[key] = node | |
| parent["children"].append(node) | |
| node_map[path]["count"] = len(idx_list) | |
| direct_file_dirs: set[str] = set() | |
| for idx in repo_records_map.get(repo, []): | |
| rec = records[idx] | |
| folders = cast(list[str], rec.get("Folder", []) or []) | |
| direct_file_dirs.add("/".join(folders)) | |
| for path_key, node in node_map.items(): | |
| node["hasDirectFiles"] = path_key in direct_file_dirs | |
| node["children"].sort(key=lambda child: child["name"]) | |
| node["hasChildren"] = len(node["children"]) > 0 | |
| node["showSelfToggle"] = bool(path_key and node["hasDirectFiles"] and node["hasChildren"]) | |
| return [tree_root] | |