Skip to content
Snippets Groups Projects

F copy entity

Merged Alexander Schlemmer requested to merge f-copy-entity into dev
1 unresolved thread
Files
3
+ 100
4
@@ -565,8 +565,8 @@ def getCommitIn(folder):
return t.readline().strip()
COMPARED = ["name", "role", "datatype", "description", "importance",
"id", "path", "checksum", "size"]
SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description",
"id", "path", "checksum", "size"]
def compare_entities(old_entity: Entity, new_entity: Entity):
@@ -592,7 +592,7 @@ def compare_entities(old_entity: Entity, new_entity: Entity):
if old_entity is new_entity:
return (olddiff, newdiff)
for attr in COMPARED:
for attr in SPECIAL_ATTRIBUTES:
try:
oldattr = old_entity.__getattribute__(attr)
old_entity_attr_exists = True
@@ -681,10 +681,106 @@ def compare_entities(old_entity: Entity, new_entity: Entity):
return (olddiff, newdiff)
def copy_entity(entity: Entity):
"""
Return a copy of entity.
If deep == True return a deep copy, recursively copying all sub entities.
Standard properties are copied using add_property.
Special attributes, as defined by the global variable SPECIAL_ATTRIBUTES and additionaly
the "value" are copied using setattr.
"""
print(entity)
if entity.role == "File":
new = File()
elif entity.role == "Property":
new = Property()
elif entity.role == "RecordType":
new = RecordType()
elif entity.role == "Record":
new = Record()
elif entity.role == "Entity":
new = Entity()
else:
raise RuntimeError("Unkonwn role.")
# Copy special attributes:
# TODO: this might rise an exception when copying
# special file attributes like checksum and size.
for attribute in SPECIAL_ATTRIBUTES + ["value"]:
val = getattr(entity, attribute)
if val is not None:
setattr(new, attribute, val)
# Copy parents:
for p in entity.parents:
new.add_parent(p)
# Copy properties:
for p in entity.properties:
new.add_property(p, importance=entity.get_importance(p))
return new
def merge_entities(entity_a: Entity, entity_b: Entity):
"""
Merge entity_b into entity_a such that they have the same parents and properties.
"""
# Compare both entities:
diff_r1, diff_r2 = compare_entities(entity_a, entity_b)
# Go through the comparison and try to apply changes to entity_a:
for key in diff_r2["parents"]:
entity_a.add_parent(entity_b.get_parent(key))
for key in diff_r2["properties"]:
if key in diff_r1["properties"]:
if ("importance" in diff_r1["properties"][key] and
"importance" in diff_r2["properties"][key]):
if (diff_r1["properties"][key]["importance"] !=
diff_r2["properties"][key]["importance"]):
raise NotImplementedError()
elif ("importance" in diff_r1["properties"][key] or
"importance" in diff_r2["properties"][key]):
raise NotImplementedError()
for attribute in ("datatype", "unit", "value"):
if diff_r1["properties"][key][attribute] is None:
setattr(entity_a.get_property(key), attribute,
diff_r2["properties"][key][attribute])
else:
raise RuntimeError("Merge conflict.")
else:
# TODO: This is a temporary FIX for
# https://gitlab.indiscale.com/caosdb/src/caosdb-pylib/-/issues/105
entity_a.add_property(id=entity_b.get_property(key).id,
name=entity_b.get_property(key).name,
datatype=entity_b.get_property(key).datatype,
value=entity_b.get_property(key).value,
unit=entity_b.get_property(key).unit,
importance=entity_b.get_importance(key))
# entity_a.add_property(
# entity_b.get_property(key),
# importance=entity_b.get_importance(key))
for special_attribute in ("name", "description"):
sa_a = getattr(entity_a, special_attribute)
sa_b = getattr(entity_b, special_attribute)
if sa_a != sa_b:
if sa_a is None:
setattr(entity_a, special_attribute, sa_b)
else:
raise RuntimeError("Merge conflict.")
return entity_a
def describe_diff(olddiff, newdiff, name=None, as_update=True):
description = ""
for attr in list(set(list(olddiff.keys())+list(newdiff.keys()))):
for attr in list(set(list(olddiff.keys()) + list(newdiff.keys()))):
if attr == "parents" or attr == "properties":
continue
description += "{} differs:\n".format(attr)
Loading