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

ENH: added some functions from the old crawler framework

parent f4b751e2
No related branches found
No related tags found
1 merge request!53Release 0.1
......@@ -25,6 +25,7 @@
import caosdb as db
from abc import abstractmethod
from utils import get_value
class IdentifiableAdapter(object):
"""
......@@ -43,6 +44,40 @@ class IdentifiableAdapter(object):
from the database.
"""
@staticmethod
def create_query_for_identifiable(ident: db.Record):
"""
This function is taken from the old crawler:
caosdb-advanced-user-tools/src/caosadvancedtools/crawler.py
uses the properties of ident to create a query that can determine
whether the required record already exists.
"""
# TODO multiple parents are ignored! Sufficient?
if len(ident.get_parents()) == 0:
raise ValueError("The identifiable must have at least one parent.")
query_string = "FIND Record " + ident.get_parents()[0].name
query_string += " WITH "
if ident.name is None and len(ident.get_properties()) == 0:
raise ValueError(
"The identifiable must have features to identify it.")
if ident.name is not None:
query_string += "name='{}' AND".format(ident.name)
for p in ident.get_properties():
if p.datatype is not None and p.datatype.startswith("LIST<"):
for v in p.value:
query_string += ("references "
+ str(v.id if isinstance(v, db.Entity)
else v)
+ " AND ")
else:
query_string += ("'" + p.name + "'='" + str(get_value(p))
+ "' AND ")
# remove the last AND
return query_string[:-4]
@abstractmethod
def get_registered_identifiable(self, record: db.Record):
"""
......
......@@ -24,6 +24,7 @@
#
import caosdb as db
from datetime import datetime
# Some utility functions, e.g. for extending pylib.
......@@ -38,3 +39,26 @@ def has_parent(entity: db.Entity, name: str):
if parent.name == name:
return True
return False
def get_value(prop):
""" Returns the value of a Property
This function is taken from the old crawler:
caosdb-advanced-user-tools/src/caosadvancedtools/crawler.py
Parameters
----------
prop : The property of which the value shall be returned.
Returns
-------
out : The value of the property; if the value is an entity, its ID.
"""
if isinstance(prop.value, db.Entity):
return prop.value.id
elif isinstance(prop.value, datetime):
return prop.value.isoformat()
else:
return prop.value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment