INNER CODE UNIT · Python
by_body
lissy93/portainer-templates · lib/changelog.py:69
def by_body(template):
"""Every field but the title, so a template under a new name still matches"""
return json.dumps({k: v for k, v in template.items() if k != 'title'}, sort_keys=True)
def match_renames(old, new, added, removed):
"""Renames, matched on title then body and dropped from `added` and `removed`.
A pair only counts when exactly one candidate matches, so splits stay honest."""
renamed = []
for key in (by_name, by_body):
candidates = {}
for title in added:
candidates.setdefault(key(new[title]), []).append(title)
for title in list(removed):
identity = key(old[title])
if len(candidates.get(identity, [])) == 1:
new_title = candidates.pop(identity)[0]
renamed.append({'from': title, 'to': new_title})
added.remove(new_title)