Skip to content
Snippets Groups Projects
Commit 33905db0 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-minor' into 'dev'

MAINT: minimal refactoring; better error handling and format

See merge request !145
parents 25c44ed6 64f77892
No related branches found
No related tags found
2 merge requests!160STY: styling,!145MAINT: minimal refactoring; better error handling and format
Pipeline #46742 passed
......@@ -55,6 +55,7 @@ from caosdb.apiutils import (EntityMergeConflictError, compare_entities,
from caosdb.cached import cache_clear, cached_get_entity_by
from caosdb.exceptions import EmptyUniqueQueryError
from linkahead.common.datatype import get_list_datatype, is_reference
from linkahead.utils.escape import escape_quoted_text
from .config import get_config_setting
from .converters import Converter, ConverterValidationError
......@@ -658,6 +659,10 @@ class Crawler(object):
try_to_merge_later.append(record)
if newrecord.id is not None:
record.id = newrecord.id
except NotImplementedError:
print(newrecord)
print(record)
raise
Crawler.bend_references_to_new_object(
old=record, new=newrecord,
entities=flat + to_be_updated + to_be_inserted + try_to_merge_later
......@@ -892,6 +897,12 @@ class Crawler(object):
"mode. This might lead to a failure of inserts that follow.")
logger.info(parent_updates)
@staticmethod
def _get_property_id_for_datatype(rtname: str, name: str):
return cached_get_entity_by(
query=f"FIND Entity '{escape_quoted_text(rtname)}' "
f"with name='{escape_quoted_text(name)}'").id
@staticmethod
def replace_name_with_referenced_entity_id(prop: db.Property):
"""changes the given property in place if it is a reference property that has a name as
......@@ -909,8 +920,8 @@ class Crawler(object):
prop.datatype != db.FILE and prop.datatype != db.REFERENCE): # datatype is a non-generic reference and value is a string
try:
# the get_entity function will raise an error if not unique
prop.value = cached_get_entity_by(
query=f"FIND Entity {prop.datatype} with name='{prop.value}'").id
prop.value = Crawler._get_property_id_for_datatype(
rtname=prop.datatype, name=prop.value)
except (db.EmptyUniqueQueryError, db.QueryNotUniqueError):
logger.error("The Property {prop.name} with datatype={prop.datatype} has the "
"value {prop.value} and there is no appropriate Entity with such "
......@@ -925,8 +936,8 @@ class Crawler(object):
if isinstance(el, str):
try:
# the get_entity function will raise an error if not unique
propval.append(cached_get_entity_by(
query=f"FIND Entity {prop.datatype} with name='{prop.value}'").id)
propval.append(Crawler._get_property_id_for_datatype(rtname=prop.datatype,
name=el))
except (db.EmptyUniqueQueryError, db.QueryNotUniqueError):
logger.error(
"The Property {prop.name} with datatype={prop.datatype} has the "
......@@ -943,6 +954,8 @@ class Crawler(object):
unique_names=True):
for record in to_be_inserted:
for prop in record.properties:
if prop.name == "name":
raise Exception('Cannot search for the property with name "name"')
entity = cached_get_entity_by(name=prop.name)
_resolve_datatype(prop, entity)
Crawler.replace_name_with_referenced_entity_id(prop)
......
......@@ -241,7 +241,7 @@ identifiabel, identifiable and identified record) for a Record.
# raise an exception?
# TODO: is this the appropriate error?
raise NotImplementedError(
f"The following record is missing an identifying property:"
f"The following record is missing an identifying property:\n"
f"RECORD\n{record}\nIdentifying PROPERTY\n{prop.name}"
)
identifiable_props[record_prop.name] = record_prop.value
......
......@@ -316,7 +316,7 @@ def scanner(items: list[StructureElement],
converters_path = []
for element in items:
element_path = os.path.join(*(structure_elements_path + [element.get_name()]))
element_path = os.path.join(*(structure_elements_path + [str(element.get_name())]))
logger.debug(f"Dealing with {element_path}")
for converter in converters:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment