Skip to content
Snippets Groups Projects

F fix merge entity conflicts

Merged Florian Spreckelsen requested to merge f-fix-merge-entity-conflicts into dev
All threads resolved!
1 file
+ 41
5
Compare changes
  • Side-by-side
  • Inline
+ 41
5
@@ -269,7+269,7 @@
@@ -269,7+269,7 @@
new_entity.get_importance(prop.name)):
new_entity.get_importance(prop.name)):
olddiff["properties"][prop.name]["importance"] = \
olddiff["properties"][prop.name]["importance"] = \
old_entity.get_importance(prop.name)
old_entity.get_importance(prop.name)
newdiff["properties"][prop.name]["importance"] = \
newdiff["properties"][prop.name]["importance"] = \
new_entity.get_importance(prop.name)
new_entity.get_importance(prop.name)
if (prop.datatype != matching[0].datatype):
if (prop.datatype != matching[0].datatype):
@@ -283,7+283,7 @@
@@ -283,7+283,7 @@
matching[0].unit
matching[0].unit
if (prop.value != matching[0].value):
if (prop.value != matching[0].value):
olddiff["properties"][prop.name]["value"] = prop.value
# basic comparison of value objects says they are different
newdiff["properties"][prop.name]["value"] = \
same_value = False
matching[0].value
if compare_referenced_records:
 
# scalar reference
 
if isinstance(prop.value, Entity) and isinstance(matching[0].value, Entity):
 
# exlicitely not recursive to prevent infinite recursion
 
same_value = empty_diff(
 
prop.value, matching[0].value, compare_referenced_records=False)
 
# list of references
 
elif isinstance(prop.value, list) and isinstance(matching[0].value, list):
 
# all elements in both lists actually are entity objects
 
if all([isinstance(x, Entity) for x in prop.value]) and all([isinstance(x, Entity) for x in matching[0].value]):
 
# can't be the same if the lengths are different
 
if len(prop.value) == len(matching[0].value):
 
# do a one-by-one comparison; the values are the same, if all diffs are empty
 
same_value = all(
 
[empty_diff(x, y, False) for x, y in zip(prop.value, matching[0].value)])
 
 
if not same_value:
 
olddiff["properties"][prop.name]["value"] = prop.value
 
newdiff["properties"][prop.name]["value"] = \
 
matching[0].value
if (len(newdiff["properties"][prop.name]) == 0
if (len(newdiff["properties"][prop.name]) == 0
and len(olddiff["properties"][prop.name]) == 0):
and len(olddiff["properties"][prop.name]) == 0):
@@ -338,7 +357,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
@@ -338,7 +357,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
return True
return True
def merge_entities(entity_a: Entity, entity_b: Entity):
def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_empty_diffs=True):
"""
"""
Merge entity_b into entity_a such that they have the same parents and properties.
Merge entity_b into entity_a such that they have the same parents and properties.
@@ -352,13 +371,30 @@ def merge_entities(entity_a: Entity, entity_b: Entity):
@@ -352,13 +371,30 @@ def merge_entities(entity_a: Entity, entity_b: Entity):
Returns entity_a.
Returns entity_a.
WARNING: This function is currently experimental and insufficiently tested. Use with care.
WARNING: This function is currently experimental and insufficiently tested. Use with care.
 
 
Parameters
 
----------
 
entity_a, entity_b : Entity
 
The entities to be merged. entity_b will be merged into entity_a in place
 
merge_references_with_empty_diffs : bool, optional
 
Whether the merge is performed if entity_a and entity_b both reference
 
record(s) that may be different Python objects but have empty diffs. If
 
set to `False` a merge conflict will be raised in this case
 
instead. Default is True.
 
 
Returns
 
-------
 
entity_a : Entity
 
The initial entity_a after the in-place merge
 
"""
"""
logging.warning(
logging.warning(
"This function is currently experimental and insufficiently tested. Use with care.")
"This function is currently experimental and insufficiently tested. Use with care.")
# Compare both entities:
# Compare both entities:
diff_r1, diff_r2 = compare_entities(entity_a, entity_b)
diff_r1, diff_r2 = compare_entities(
 
entity_a, entity_b, compare_referenced_records=merge_references_with_empty_diffs)
# Go through the comparison and try to apply changes to entity_a:
# Go through the comparison and try to apply changes to entity_a:
for key in diff_r2["parents"]:
for key in diff_r2["parents"]:
Loading