diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index 31f7bbf62f1993e11ce9e9d58e76553b39e3d6ad..2afe4b30053fc4e9b037ea6e076c7bed30de4e7b 100644 --- a/src/caosadvancedtools/cfood.py +++ b/src/caosadvancedtools/cfood.py @@ -262,6 +262,45 @@ def assure_has_parent(entity, parent, to_be_updated=None, verbose=True): to_be_updated.append(entity) +def assure_has_property(entity, name, value, to_be_updated=None, verbose=True): + """ + Checks whether `entity` has a property `name` with the value `value`. + + If this is the case this function ends. Otherwise the entity is assigned + a new parent. The list to_be_updated is supplied, the entity is added to + the list in order to indicate, that the entity `entity` should be updated. + Otherwise it is directly updated + """ + properties = entity.get_properties() + possible_properties = [prop for prop in properties if prop.name == name] + contained = False + + for el in possible_properties: + if el.value == value: + contained = True + + break + + if contained: + if verbose: + print("entity {} has property {} with value {}".format( + entity.id, + name, value)) + + return + + if verbose: + print("Adding property {} with value {} to entity {}".format( + name, + value, entity.id)) + entity.add_property(name=name, value=value) + + if to_be_updated is None: + entity.update(unique=False) + else: + to_be_updated.append(entity) + + def insert_id_based_on_name(entity): if entity.name is not None and (entity.id is None or entity.id < 0): entity.id = get_entity(entity.name).id diff --git a/unittests/test_cfood.py b/unittests/test_cfood.py index 2f00c1c1065d39cba3d2e6a3cc92ecc4079f7648..a9e71557f87622bb9f13ad729898d067415ce003 100644 --- a/unittests/test_cfood.py +++ b/unittests/test_cfood.py @@ -24,7 +24,6 @@ import unittest import caosdb as db - from caosadvancedtools.cfood import (AbstractCFood, assure_has_parent, assure_object_is_in_list) @@ -43,7 +42,8 @@ class CFoodReTest(unittest.TestCase): self.assertEquals(TestCFood.get_re(), PATTERN) self.assertEqual(TestCFood._pattern, None) self.assertIsNotNone(TestCFood.match("hallo")) - self.assertIsNotNone(TestCFood._pattern) + # TODO the caching is of compiled re is disabled currently + # self.assertIsNotNone(TestCFood._pattern) self.assertIsNotNone(TestCFood.match("hallo")) self.assertIsNone(TestCFood.match("allo"))