Skip to content
Snippets Groups Projects
Commit 513b600c authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Check for conflicts when merging metadata

parent f86be23d
No related branches found
No related tags found
1 merge request!781Support for saving metadata fragments & merging into calibration_metadata.yml
......@@ -813,13 +813,23 @@ def module_index_to_qm(index: int, total_modules: int = 16):
def recursive_update(target: dict, source: dict):
"""Recursively merge source into target, checking for conflicts
Conflicting entries will not be copied to target. Returns True if any
conflicts were found.
"""
conflict = False
for k, v2 in source.items():
v1 = target.get(k, None)
if isinstance(v1, dict) and isinstance(v2, dict):
recursive_update(v1, v2)
conflict = recursive_update(v1, v2) or conflict
elif (v1 is not None) and (v1 != v2):
conflict = True
else:
target[k] = v2
return conflict
class CalibrationMetadata(dict):
"""Convenience class: dictionary stored in metadata YAML file
......@@ -868,14 +878,19 @@ class CalibrationMetadata(dict):
def gather_fragments(self):
"""Merge in fragments saved by add_fragment(), then delete them"""
frag_files = list(self._yaml_fn.parent.glob('metadata_frag_*.yml'))
to_delete = []
for fn in frag_files:
with fn.open("r") as fd:
data = yaml.safe_load(fd)
recursive_update(self, data)
if recursive_update(self, data):
print(f"{fn} contained conflicting metadata. "
f"This file will be left for debugging")
else:
to_delete.append(fn)
self.save()
for fn in frag_files:
for fn in to_delete:
fn.unlink()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment