indent fix
This commit is contained in:
+79
-79
@@ -106,87 +106,87 @@ class LocalGraph:
|
|||||||
f.write(toon_text)
|
f.write(toon_text)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def _parse_node_id(self, node_id: str):
|
def _parse_node_id(self, node_id: str):
|
||||||
"""
|
"""
|
||||||
Parse node_id into components and return a dict of canonical attributes.
|
Parse node_id into components and return a dict of canonical attributes.
|
||||||
|
|
||||||
Supported formats:
|
Supported formats:
|
||||||
1) lang::node_type::file_path::name (full chunk/file tied)
|
1) lang::node_type::file_path::name (full chunk/file tied)
|
||||||
2) lang::node_type::name (file-less canonical form - Option B)
|
2) lang::node_type::name (file-less canonical form - Option B)
|
||||||
3) file::<file_path> (file nodes)
|
3) file::<file_path> (file nodes)
|
||||||
otherwise -> fallback to Symbol
|
otherwise -> fallback to Symbol
|
||||||
"""
|
"""
|
||||||
attrs = {
|
attrs = {
|
||||||
"type": "Symbol",
|
"type": "Symbol",
|
||||||
"name": node_id,
|
"name": node_id,
|
||||||
"lang": "",
|
"lang": "",
|
||||||
"file": ""
|
"file": ""
|
||||||
}
|
}
|
||||||
if not isinstance(node_id, str):
|
if not isinstance(node_id, str):
|
||||||
return attrs
|
return attrs
|
||||||
parts = node_id.split("::")
|
parts = node_id.split("::")
|
||||||
if len(parts) == 4:
|
if len(parts) == 4:
|
||||||
lang, node_type, file_path, name = parts
|
lang, node_type, file_path, name = parts
|
||||||
attrs["type"] = node_type
|
attrs["type"] = node_type
|
||||||
attrs["name"] = name
|
attrs["name"] = name
|
||||||
attrs["lang"] = lang
|
attrs["lang"] = lang
|
||||||
attrs["file"] = file_path
|
attrs["file"] = file_path
|
||||||
elif len(parts) == 3:
|
elif len(parts) == 3:
|
||||||
# Option B canonical file-less representation: lang::kind::entity_name
|
# Option B canonical file-less representation: lang::kind::entity_name
|
||||||
lang, node_type, name = parts
|
lang, node_type, name = parts
|
||||||
attrs["type"] = node_type
|
attrs["type"] = node_type
|
||||||
attrs["name"] = name
|
attrs["name"] = name
|
||||||
attrs["lang"] = lang
|
attrs["lang"] = lang
|
||||||
attrs["file"] = ""
|
attrs["file"] = ""
|
||||||
elif len(parts) == 2 and parts[0] == "file":
|
elif len(parts) == 2 and parts[0] == "file":
|
||||||
# file::<file_path>
|
# file::<file_path>
|
||||||
attrs["type"] = "File"
|
attrs["type"] = "File"
|
||||||
attrs["name"] = parts[1]
|
attrs["name"] = parts[1]
|
||||||
attrs["file"] = parts[1]
|
attrs["file"] = parts[1]
|
||||||
attrs["lang"] = ""
|
attrs["lang"] = ""
|
||||||
else:
|
else:
|
||||||
# fallback: try to be helpful by guessing the name
|
# fallback: try to be helpful by guessing the name
|
||||||
if len(parts) >= 1:
|
if len(parts) >= 1:
|
||||||
attrs["name"] = parts[-1]
|
attrs["name"] = parts[-1]
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def _ensure_node_exists(self, node_id: str):
|
def _ensure_node_exists(self, node_id: str):
|
||||||
"""
|
"""
|
||||||
Ensure a node with node_id exists in the graph.
|
Ensure a node with node_id exists in the graph.
|
||||||
If it doesn't, create it using sensible attributes derived from the node_id format.
|
If it doesn't, create it using sensible attributes derived from the node_id format.
|
||||||
"""
|
"""
|
||||||
if node_id in self.graph.nodes:
|
if node_id in self.graph.nodes:
|
||||||
return
|
return
|
||||||
parsed = self._parse_node_id(node_id)
|
parsed = self._parse_node_id(node_id)
|
||||||
# Use add_node (keeps behaviour consistent)
|
# Use add_node (keeps behaviour consistent)
|
||||||
self.add_node(node_id, **{
|
self.add_node(node_id, **{
|
||||||
"type": parsed.get("type", "Symbol"),
|
"type": parsed.get("type", "Symbol"),
|
||||||
"name": parsed.get("name", node_id),
|
"name": parsed.get("name", node_id),
|
||||||
"lang": parsed.get("lang", ""),
|
"lang": parsed.get("lang", ""),
|
||||||
"file": parsed.get("file", "")
|
"file": parsed.get("file", "")
|
||||||
})
|
})
|
||||||
|
|
||||||
def add_edge(self, src: str, dst: str, edge_type: str, **attrs):
|
def add_edge(self, src: str, dst: str, edge_type: str, **attrs):
|
||||||
"""
|
"""
|
||||||
Add an edge but first ensure both source and destination nodes exist and have basic attributes.
|
Add an edge but first ensure both source and destination nodes exist and have basic attributes.
|
||||||
This prevents the creation of attribute-less nodes and makes semantic edges meaningful.
|
This prevents the creation of attribute-less nodes and makes semantic edges meaningful.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Ensure source node exists (create minimal entry if missing)
|
# Ensure source node exists (create minimal entry if missing)
|
||||||
if src not in self.graph.nodes:
|
if src not in self.graph.nodes:
|
||||||
self._ensure_node_exists(src)
|
self._ensure_node_exists(src)
|
||||||
|
|
||||||
# Ensure destination node exists (create minimal entry if missing)
|
# Ensure destination node exists (create minimal entry if missing)
|
||||||
if dst not in self.graph.nodes:
|
if dst not in self.graph.nodes:
|
||||||
self._ensure_node_exists(dst)
|
self._ensure_node_exists(dst)
|
||||||
|
|
||||||
# Finally add the edge with type and any extra attrs
|
# Finally add the edge with type and any extra attrs
|
||||||
self.graph.add_edge(src, dst, type=edge_type, **attrs)
|
self.graph.add_edge(src, dst, type=edge_type, **attrs)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Keep behaviour non-fatal for indexing runs - log if you have a logger available
|
# Keep behaviour non-fatal for indexing runs - log if you have a logger available
|
||||||
# fallback: still attempt to add the edge
|
# fallback: still attempt to add the edge
|
||||||
try:
|
try:
|
||||||
self.graph.add_edge(src, dst, type=edge_type, **attrs)
|
self.graph.add_edge(src, dst, type=edge_type, **attrs)
|
||||||
except Exception:
|
except Exception:
|
||||||
# swallow; graph should remain usable
|
# swallow; graph should remain usable
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user