diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index f6e3b89f0f996263b7f980ecbcc292100ec6eb65..3058e58f0557e0f55030e3d5b214bb7345749415 100644 --- a/src/caosadvancedtools/cfood.py +++ b/src/caosadvancedtools/cfood.py @@ -91,6 +91,25 @@ class AbstractCFood(object): """ raise NotImplementedError() + @classmethod + def cook(cls, crawled_file): + """ possibly checks for existing CFoods whether the match should be + added or whether a new CFood instance needs to be returned + + This function should typically be used to create CFoods in order to + prevent the creation of unnecessary instances. + + This standard implementation does not do a check but may be overwritten + by subclasses. + + Retruns + ------------- + CFood: if a new instance was created + None: otherwise + """ + + return cls() + @classmethod def match(cls, string): """ Matches the regular expression of this class against file names @@ -337,3 +356,31 @@ def get_ids_for_entities_with_names(entities): for parent in ent.get_parents(): insert_id_based_on_name(parent) insert_id_based_on_name(ent) + + +class CMeal(object): + existing_instances = [] + matching_groups = [] + + def __init__(self, *args, **kwargs): + self.existing_instances.append(self) + self.crawled_files = [] + + def add(self, crawled_file): + self.crawled_files.append(crawled_file) + + @classmethod + def get_suitable_cfood(cls, match): + for cfood in cls.existing_instances: + suitable = True + + for group in cls.matching_groups: + if (group not in match or + group not in cfood.match or + match.group(group) == cfood.match.group(group)): + suitable = False + + if suitable: + return cfood + + return None