glam/restore_slot_fixes.py

82 lines
2.9 KiB
Python

import yaml
import os
def load_yaml(path):
with open(path, 'r') as f:
return yaml.safe_load(f) or {}
def save_yaml(data, path):
with open(path, 'w') as f:
yaml.dump(data, f, sort_keys=False, allow_unicode=True)
def merge_slot_fixes():
full_path = 'slot_fixes_backup_full.yaml'
current_path = 'slot_fixes_current.yaml'
target_path = 'schemas/20251121/linkml/modules/slots/slot_fixes.yaml'
full_data = load_yaml(full_path)
current_data = load_yaml(current_path)
full_list = full_data.get('fixes', []) if isinstance(full_data, dict) else full_data
current_list = current_data.get('fixes', []) if isinstance(current_data, dict) else current_data
# Normalize structure if it's a list at root vs dict with 'fixes' key
# The standard seems to be a list, but let's check
if not isinstance(full_list, list):
# Maybe it's a list at root
full_list = full_data if isinstance(full_data, list) else []
if not isinstance(current_list, list):
current_list = current_data if isinstance(current_data, list) else []
print(f"Loaded {len(full_list)} entries from backup.")
print(f"Loaded {len(current_list)} entries from current file.")
# Map current entries by original_slot_id for easy lookup
current_map = {}
for entry in current_list:
if 'original_slot_id' in entry:
current_map[entry['original_slot_id']] = entry
elif 'orignal_slot_id' in entry: # Handle typo found in previous edits
current_map[entry['orignal_slot_id']] = entry
# Merge strategy:
# 1. Start with full backup list
# 2. Update matching entries with current version
# 3. Track which current entries were used
merged_list = []
processed_ids = set()
for entry in full_list:
slot_id = entry.get('original_slot_id') or entry.get('orignal_slot_id')
if slot_id and slot_id in current_map:
# Use the CURRENT version (contains recent updates)
merged_list.append(current_map[slot_id])
processed_ids.add(slot_id)
else:
# Use the BACKUP version (restoring deleted entry)
merged_list.append(entry)
# 4. Add any NEW entries from current that weren't in backup
for entry in current_list:
slot_id = entry.get('original_slot_id') or entry.get('orignal_slot_id')
if slot_id and slot_id not in processed_ids:
print(f"Adding new entry not in backup: {slot_id}")
merged_list.append(entry)
print(f"Total entries in merged file: {len(merged_list)}")
# Save result
# Check if the original file format was a list or dict
if isinstance(full_data, dict) and 'fixes' in full_data:
output_data = {'fixes': merged_list}
else:
output_data = merged_list
save_yaml(output_data, target_path)
print("Successfully restored slot_fixes.yaml")
if __name__ == "__main__":
merge_slot_fixes()