INNER CODE UNIT · Python
by_name
lissy93/portainer-templates · lib/changelog.py:65
def by_name(template):
"""Bare letters and digits of a title, so 'Node Red' and 'Node-Red' match"""
return re.sub(r'[^a-z0-9]', '', template['title'].lower())
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])