diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index a4c4c1f9d67235b71fae65139d612d5bcbac7f3f..30cad59276505e81049109558e524d696ce5c998 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -532,9 +532,8 @@ COMPARED = ["name", "role", "datatype", "description", "importance"] def compare_entities(old_entity, new_entity): - olddiff = {} - newdiff = {} - description = "" + olddiff = {"properties": {}, "parents": {}} + newdiff = {"properties": {}, "parents": {}} if old_entity is new_entity: return (olddiff, newdiff) @@ -545,73 +544,62 @@ def compare_entities(old_entity, new_entity): old_entity_attr_exists = True except BaseException: old_entity_attr_exists = False - oldattr = None try: - newattr =new_entity.__getattribute__(attr) + newattr = new_entity.__getattribute__(attr) new_entity_attr_exists = True except BaseException: new_entity_attr_exists = False - newattr = None if not old_entity_attr_exists and not new_entity_attr_exists: continue - if old_entity_attr_exists ^ new_entity_attr_exists: - olddiff[attr] = old_entity.__getattribute__(attr) - newdiff[attr] = old_entity.__getattribute__(attr) + if ((old_entity_attr_exists ^ new_entity_attr_exists) + or (oldattr != newattr)): - if old_entity.__getattribute__(attr) != new_entity.__getattribute__(attr): - description += attr + " differs:\n" - description += str(old_entity.__getattribute__(attr)) + "\n" - description += str(new_entity.__getattribute__(attr)) + "\n" + if old_entity_attr_exists: + olddiff[attr] = oldattr + + if new_entity_attr_exists: + newdiff[attr] = newattr # properties - if (len(old_entity.properties) > 0) ^ (len(new_entity.properties) > 0): - description += "only one has properties\n" - else: - for prop in old_entity.properties: - matching = [p for p in new_entity.properties if p.name == prop.name] - - if len(matching) == 0: - description += "new_entity is missing the property '" + prop.name + "'\n" - elif len(matching) == 1: - if (old_entity.get_importance(prop.name) != - new_entity.get_importance(prop.name)): - description += "importance of '" + prop.name + "' differs\n" - - if ((prop.datatype is not None and - matching[0].datatype is not None) and - (prop.datatype != matching[0].datatype)): - description += "datatype of '" + prop.name + "' differs\n" - else: - raise NotImplementedError() + for prop in old_entity.properties: + matching = [p for p in new_entity.properties if p.name == prop.name] + + if len(matching) == 0: + olddiff["properties"][prop.name] = {} + elif len(matching) == 1: + newdiff["properties"][prop.name] = {} + + if (old_entity.get_importance(prop.name) != + new_entity.get_importance(prop.name)): + olddiff["properties"][prop.name]["importance"] = \ + old_entity.get_importance(prop.name) + newdiff["properties"][prop.name]["importance"] = \ + new_entity.get_importance(prop.name) + + if ((prop.datatype is not None and + matching[0].datatype is not None) and + (prop.datatype != matching[0].datatype)): + olddiff["properties"][prop.name]["datatype"] = prop.datatype + newdiff["properties"][prop.name]["datatype"] = \ + matching[0].datatype + else: + raise NotImplementedError() - for prop in new_entity.properties: - if len([0 for p in old_entity.properties if p.name == prop.name]) == 0: - description += "old_entity is missing the property '" + prop.name + "'\n" + for prop in new_entity.properties: + if len([0 for p in old_entity.properties if p.name == prop.name]) == 0: + newdiff["properties"][prop.name] = {} # parents - if ((len(old_entity.parents) > 0) ^ (len(new_entity.parents) > 0)): - description += "only one has parents\n" - else: - for par in old_entity.parents: - matching = [p for p in new_entity.parents if p.name == par.name] - - if len(matching) == 0: - description += "new_entity is missing the parent '" + par.name + "'\n" - elif len(matching) == 1: - description += compare_entities(par, matching[0]) - else: - raise NotImplementedError() + for parent in old_entity.parents: + if len([0 for p in new_entity.parents if p.name == parent.name]) == 0: + olddiff["parents"][parent.name] = {} - for par in new_entity.parents: - if len([0 for p in old_entity.parents if p.name == par.name]) == 0: - description += "old_entity is missing the parent '" + par.name + "'\n" + for parent in new_entity.parents: + if len([0 for p in old_entity.parents if p.name == parent.name]) == 0: + newdiff["parents"][parent.name] = {} - if description != "": - description = """####################### -{} -#######################\n""".format(old_entity.name) + description - return description + return (olddiff, newdiff)