Skip to content
Snippets Groups Projects
Commit 666d6d41 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

FIX: problems with cyclic references in high level api

parent 968d234f
Branches
Tags
1 merge request!109Detection of cyclic references for high level API
Pipeline #38488 passed with warnings
...@@ -561,8 +561,8 @@ class CaosDBPythonEntity(object): ...@@ -561,8 +561,8 @@ class CaosDBPythonEntity(object):
return propval return propval
def resolve_references(self, deep: bool, references: db.Container, def resolve_references(self, deep: bool, references: db.Container,
visited: Dict[Union[str, int], visited: Optional[Dict[Union[str, int],
"CaosDBPythonEntity"] = None): "CaosDBPythonEntity"]] = None):
""" """
Resolve this entity's references. This affects unresolved properties as well Resolve this entity's references. This affects unresolved properties as well
as unresolved parents. as unresolved parents.
...@@ -807,7 +807,9 @@ BASE_ATTRIBUTES = ( ...@@ -807,7 +807,9 @@ BASE_ATTRIBUTES = (
def _single_convert_to_python_object(robj: CaosDBPythonEntity, def _single_convert_to_python_object(robj: CaosDBPythonEntity,
entity: db.Entity, entity: db.Entity,
references: Optional[db.Container] = None): references: Optional[db.Container] = None,
visited: Optional[Dict[int,
"CaosDBPythonEntity"]] = None):
""" """
Convert a db.Entity from the standard API to a (previously created) Convert a db.Entity from the standard API to a (previously created)
CaosDBPythonEntity from the high level API. CaosDBPythonEntity from the high level API.
...@@ -822,6 +824,17 @@ def _single_convert_to_python_object(robj: CaosDBPythonEntity, ...@@ -822,6 +824,17 @@ def _single_convert_to_python_object(robj: CaosDBPythonEntity,
Returns the input object robj. Returns the input object robj.
""" """
# This parameter is used in the recursion to keep track of already visited
# entites (in order to detect cycles).
if visited is None:
visited = dict()
if id(entity) in visited:
return visited[id(entity)]
else:
visited[id(entity)] = robj
for base_attribute in BASE_ATTRIBUTES: for base_attribute in BASE_ATTRIBUTES:
val = entity.__getattribute__(base_attribute) val = entity.__getattribute__(base_attribute)
if val is not None: if val is not None:
...@@ -924,7 +937,9 @@ def convert_to_entity(python_object): ...@@ -924,7 +937,9 @@ def convert_to_entity(python_object):
def convert_to_python_object(entity: Union[db.Container, db.Entity], def convert_to_python_object(entity: Union[db.Container, db.Entity],
references: Optional[db.Container] = None): references: Optional[db.Container] = None,
visited: Optional[Dict[int,
"CaosDBPythonEntity"]] = None):
""" """
Convert either a container of CaosDB entities or a single CaosDB entity Convert either a container of CaosDB entities or a single CaosDB entity
into the high level representation. into the high level representation.
...@@ -936,15 +951,16 @@ def convert_to_python_object(entity: Union[db.Container, db.Entity], ...@@ -936,15 +951,16 @@ def convert_to_python_object(entity: Union[db.Container, db.Entity],
""" """
if isinstance(entity, db.Container): if isinstance(entity, db.Container):
# Create a list of objects: # Create a list of objects:
return [convert_to_python_object(i, references) for i in entity] return [convert_to_python_object(i, references, visited) for i in entity]
# TODO: recursion problems?
return _single_convert_to_python_object( return _single_convert_to_python_object(
high_level_type_for_standard_type(entity)(), entity, references) high_level_type_for_standard_type(entity)(), entity, references, visited)
def new_high_level_entity(entity: db.RecordType, def new_high_level_entity(entity: db.RecordType,
importance_level: str, importance_level: str,
name: str = None): name: Optional[str] = None):
""" """
Create an new record in high level format based on a record type in standard format. Create an new record in high level format based on a record type in standard format.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment