From 85bc5783328dd1a8c2013b4b8d4d2493ab0cb506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org> Date: Thu, 15 Aug 2019 08:10:17 +0200 Subject: [PATCH] DEV: introduced CMeal, an addition for CFood that allows to collect similar files in one CFood --- src/caosadvancedtools/cfood.py | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index f6e3b89f..3058e58f 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 -- GitLab