diff --git a/src/newcrawler/identifiable_adapters.py b/src/newcrawler/identifiable_adapters.py index 8f3da864d16ba4489c4bd0978ec83138a764c4de..c1125ee1bdaba71ed4fa339fa74b379604293c98 100644 --- a/src/newcrawler/identifiable_adapters.py +++ b/src/newcrawler/identifiable_adapters.py @@ -52,7 +52,6 @@ def convert_value(value): return str(value) - class IdentifiableAdapter(metaclass=ABCMeta): """ Base class for identifiable adapters. @@ -90,7 +89,8 @@ class IdentifiableAdapter(metaclass=ABCMeta): """ if len(ident.parents) != 1: - raise RuntimeError("Multiple parents for identifiables not supported.") + raise RuntimeError( + "Multiple parents for identifiables not supported.") query_string = "FIND Record " + ident.get_parents()[0].name query_string += " WITH " @@ -100,7 +100,9 @@ class IdentifiableAdapter(metaclass=ABCMeta): "The identifiable must have features to identify it.") if ident.name is not None: - query_string += "name='{}' AND ".format(ident.name) + query_string += "name='{}'".format(ident.name) + if len(ident.get_properties()) > 0: + query_string += " AND " query_string += IdentifiableAdapter.create_property_query(ident) return query_string @@ -131,8 +133,6 @@ class IdentifiableAdapter(metaclass=ABCMeta): convert_value(p.value) + "' AND ") # remove the last AND return query_string[:-4] - - @abstractmethod def get_registered_identifiable(self, record: db.Record): @@ -153,7 +153,6 @@ class IdentifiableAdapter(metaclass=ABCMeta): """ pass - def get_identifiable_for_file(self, record: db.File): """ Retrieve an identifiable for a file. @@ -166,8 +165,6 @@ class IdentifiableAdapter(metaclass=ABCMeta): identifiable.path = record.path return identifiable - - def get_identifiable(self, record: db.Record): """ retrieve the registred identifiable and fill the property values to create an @@ -192,6 +189,9 @@ class IdentifiableAdapter(metaclass=ABCMeta): # fill the values: for prop in registered_identifiable.properties: + if prop.name == "name": + # The name can be an identifiable, but it isn't a property + continue # problem: what happens with multi properties? # case A: in the registered identifiable # case B: in the identifiable @@ -294,7 +294,8 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): def store_state(self, filename): with open(filename, "w") as f: - f.write(db.common.utils.xml2str(db.Container().extend(self._records).to_xml())) + f.write(db.common.utils.xml2str( + db.Container().extend(self._records).to_xml())) def restore_state(self, filename): with open(filename, "r") as f: @@ -312,7 +313,8 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): Return True in that case and False otherwise. """ if len(registered_identifiable.parents) != 1: - raise RuntimeError("Multiple parents for identifiables not supported.") + raise RuntimeError( + "Multiple parents for identifiables not supported.") if not has_parent(record, registered_identifiable.parents[0].name): return False @@ -328,7 +330,8 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): if self.is_identifiable_for_record(definition, record): identifiable_candidates.append(definition) if len(identifiable_candidates) > 1: - raise RuntimeError("Multiple candidates for an identifiable found.") + raise RuntimeError( + "Multiple candidates for an identifiable found.") if len(identifiable_candidates) == 0: return None return identifiable_candidates[0] @@ -344,7 +347,8 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): identifiable is the record that was created during the crawler run. """ if len(identifiable.parents) != 1: - raise RuntimeError("Multiple parents for identifiables not supported.") + raise RuntimeError( + "Multiple parents for identifiables not supported.") if not has_parent(record, identifiable.parents[0].name): return False for prop in identifiable.properties: diff --git a/unittests/test_identifiable_adapters.py b/unittests/test_identifiable_adapters.py index 9730461020c6c582188db58df6524246c0a1042c..4a3ae786438e99ded8925d4405d9b051cd86bf66 100644 --- a/unittests/test_identifiable_adapters.py +++ b/unittests/test_identifiable_adapters.py @@ -52,3 +52,8 @@ def test_create_query_for_identifiable(): assert (query.lower() == "find record b with name='a' and 'c'='c' and 'd'='5' and 'e'='5.5'" " and 'f'='2020-10-10t00:00:00' and 'g'='true' and 'h'='1111' and 'i'='1112' and " "'j'='2222' and 'j'='3333' ") + + # The name can be the only identifiable + query = IdentifiableAdapter.create_query_for_identifiable( + db.Record(name="TestRecord").add_parent("TestType")) + assert query.lower() == "find record testtype with name='testrecord'"