Normalize a graphify node across common graph.json schema variants.
(raw: dict, index: int)
| 136 | |
| 137 | |
| 138 | def normalize_node(raw: dict, index: int) -> dict: |
| 139 | """Normalize a graphify node across common graph.json schema variants.""" |
| 140 | node = dict(raw) |
| 141 | node_id = first_present( |
| 142 | node, |
| 143 | "id", |
| 144 | "node_id", |
| 145 | "key", |
| 146 | "uid", |
| 147 | "name", |
| 148 | "qualified_name", |
| 149 | "fqname", |
| 150 | "symbol", |
| 151 | default=f"node_{index + 1}", |
| 152 | ) |
| 153 | source_file = first_present( |
| 154 | node, |
| 155 | "source_file", |
| 156 | "file", |
| 157 | "file_path", |
| 158 | "filepath", |
| 159 | "path", |
| 160 | "module_path", |
| 161 | "defined_in", |
| 162 | default="", |
| 163 | ) |
| 164 | label = first_present( |
| 165 | node, |
| 166 | "label", |
| 167 | "display_name", |
| 168 | "title", |
| 169 | "name", |
| 170 | "qualified_name", |
| 171 | "fqname", |
| 172 | "symbol", |
| 173 | default=node_id, |
| 174 | ) |
| 175 | community = first_present( |
| 176 | node, |
| 177 | "community", |
| 178 | "community_id", |
| 179 | "cluster", |
| 180 | "cluster_id", |
| 181 | "group", |
| 182 | "group_id", |
| 183 | "modularity_class", |
| 184 | default="unknown", |
| 185 | ) |
| 186 | node_type = first_present(node, "node_type", "kind", "type", "category", default="") |
| 187 | file_type = first_present(node, "file_type", "content_type", "artifact_type", default="") |
| 188 | if not file_type: |
| 189 | suffix = Path(str(source_file)).suffix.lower() |
| 190 | file_type = "document" if suffix in {".md", ".mdx", ".rst", ".txt"} else "code" |
| 191 | |
| 192 | node["id"] = str(node_id) |
| 193 | node["label"] = str(label) |
| 194 | node["community"] = community |
| 195 | node["source_file"] = str(source_file or "") |
no test coverage detected