diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index a6e442941a4d5c91367f866f56ec5d0ffa166ae6..31f7bbf62f1993e11ce9e9d58e76553b39e3d6ad 100644 --- a/src/caosadvancedtools/cfood.py +++ b/src/caosadvancedtools/cfood.py @@ -78,6 +78,7 @@ class AbstractCFood(object): self.to_be_updated = db.Container() self.identifiables = db.Container() self.verbose = verbose + self.attached_ones = [] @staticmethod def get_re(): @@ -129,6 +130,16 @@ class AbstractCFood(object): get_ids_for_entities_with_names(self.to_be_updated) self.to_be_updated.update() + def attach(self, crawled_file): + self.attached_ones.append(crawled_file) + + def looking_for(self, crawled_file): + """ + should set the instance variable Container with the identifiables + """ + + return False + @staticmethod # move to api? def set_parents(entity, names): @@ -173,32 +184,48 @@ def assure_object_is_in_list(obj, containing_object, property_name, `containing_object` should be updated. If the property is missing, it is added first and then the entity is added. + + If obj is a list, every element is added """ if containing_object.get_property(property_name) is None: containing_object.add_property(property_name, value=[], datatype=db.LIST(property_name)) current_list = containing_object.get_property(property_name).value - contained = False - for el in current_list: - if el == obj: - contained = True + if not isinstance(current_list, list): + current_list = [current_list] - break + if not isinstance(obj, list): + objects = [obj] + else: + objects = obj - if contained: - if verbose: - print("{} is in {} of entity {}".format(obj, property_name, - containing_object.id)) + update = False - return + for o in objects: + contained = False - if verbose: - print("Appending {} to {} of entity {}".format(obj, property_name, - containing_object.id)) - current_list.append(obj) - to_be_updated.append(containing_object) + for el in current_list: + if el == o: + contained = True + + break + + if verbose: + if contained: + print("{} is in {} of entity {}".format( + o, property_name, containing_object.id)) + else: + print("Appending {} to {} of entity {}".format( + o, property_name, containing_object.id)) + + if not contained: + current_list.append(o) + update = True + + if update: + to_be_updated.append(containing_object) def assure_has_parent(entity, parent, to_be_updated=None, verbose=True): diff --git a/src/caosadvancedtools/crawler.py b/src/caosadvancedtools/crawler.py index 74c13732a0e266e29a7e6b1b3747833af7844f59..2cc85ec7b724a92e50af12758f08c954c0998388 100644 --- a/src/caosadvancedtools/crawler.py +++ b/src/caosadvancedtools/crawler.py @@ -106,6 +106,11 @@ class Crawler(object): if self.verbose and matches > 1: print("Attention: More than one matching cfood!") + for crawled_file in sorted(files, key=lambda x: x.path): + for cfood in cfoods: + if cfood.looking_for(crawled_file): + cfood.attach(crawled_file) + for cfood in cfoods: try: cfood.create_identifiables()