diff --git a/adbnx_adapter/abc.py b/adbnx_adapter/abc.py index 5fe4e4a..9151dd5 100644 --- a/adbnx_adapter/abc.py +++ b/adbnx_adapter/abc.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- from abc import ABC -from typing import Any, Dict, List, Optional, Set +from typing import Any, Dict, List, Optional, Set, Union from arango.graph import Graph as ADBGraph from networkx.classes.graph import Graph as NXGraph @@ -92,7 +92,7 @@ def _keyify_networkx_edge( to_node_id: NxId, nx_map: Dict[NxId, str], col: str, - ) -> str: + ) -> Union[str, None]: raise NotImplementedError # pragma: no cover def _prepare_networkx_node( diff --git a/adbnx_adapter/adapter.py b/adbnx_adapter/adapter.py index 8bf5714..f19bf56 100644 --- a/adbnx_adapter/adapter.py +++ b/adbnx_adapter/adapter.py @@ -639,7 +639,10 @@ def __process_nx_node( key = self.__cntrl._keyify_networkx_node(i, nx_id, nx_node, col) nx_node["_key"] = key - nx_map[nx_id] = f"{col}/{key}" + + _id = f"{col}/{key}" + if _id != nx_id: + nx_map[nx_id] = _id self.__cntrl._prepare_networkx_node(nx_node, col) adb_docs[col].append(nx_node) @@ -702,9 +705,10 @@ def __process_nx_edge( col, ) - nx_edge["_key"] = key - nx_edge["_from"] = nx_map[from_node_id] - nx_edge["_to"] = nx_map[to_node_id] + nx_edge["_from"] = nx_map.get(from_node_id, from_node_id) + nx_edge["_to"] = nx_map.get(to_node_id, to_node_id) + if key: + nx_edge["_key"] = key self.__cntrl._prepare_networkx_edge(nx_edge, col) adb_docs[col].append(nx_edge) diff --git a/adbnx_adapter/controller.py b/adbnx_adapter/controller.py index 12c13a1..dd5a015 100644 --- a/adbnx_adapter/controller.py +++ b/adbnx_adapter/controller.py @@ -4,7 +4,7 @@ from typing import Any, Dict, List, Tuple from .abc import Abstract_ADBNX_Controller -from .typings import Json, NxData, NxId +from .typings import Json, NxData, NxId, Union class ADBNX_Controller(Abstract_ADBNX_Controller): @@ -140,9 +140,9 @@ def _keyify_networkx_edge( to_node_id: NxId, nx_map: Dict[NxId, str], col: str, - ) -> str: + ) -> Union[str, None]: """Given a NetworkX edge, its collection, and its pair of nodes, derive - its ArangoDB key. + its ArangoDB key. If None is returned, an auto-generated key will be used. NOTE #1: You must override this function if you want to create custom ArangoDB _key values for your NetworkX edges. @@ -169,8 +169,8 @@ def _keyify_networkx_edge( i.e, nx_map[from_node_id] will give you the ArangoDB _from value, and nx_map[to_node_id] will give you the ArangoDB _to value. :type nx_map: Dict[NxId, str] - :return: A valid ArangoDB _key value. - :rtype: str + :return: A valid ArangoDB _key value, or None if you want an auto-generated key. + :rtype: str | None """ return str(i)