Handle going from location=None to unset and back in prototype, as per #2005

This commit is contained in:
Griatch 2020-03-28 20:34:56 +01:00
parent bea61b289e
commit 8c44766c0a
5 changed files with 59 additions and 34 deletions

View file

@ -2294,7 +2294,8 @@ def node_apply_diff(caller, **kwargs):
if not custom_location:
diff.pop("location", None)
txt, options = _format_diff_text_and_options(diff, objects=update_objects, base_obj=base_obj)
txt, options = _format_diff_text_and_options(diff, objects=update_objects,
base_obj=base_obj, prototype=prototype)
if options:
text = [

View file

@ -272,10 +272,10 @@ def save_prototype(prototype):
stored_prototype = create_script(
DbPrototype,
key=prototype_key,
desc=prototype["prototype_desc"],
desc=in_prototype["prototype_desc"],
persistent=True,
locks=prototype_locks,
tags=prototype["prototype_tags"],
tags=in_prototype["prototype_tags"],
attributes=[("prototype", in_prototype)],
)
return stored_prototype.prototype
@ -724,15 +724,15 @@ def prototype_to_str(prototype):
prototype_desc=prototype.get("prototype_desc", "|wNone|n"),
prototype_parent=prototype.get("prototype_parent", "|wNone|n"),
)
key = prototype.get("key", "")
if key:
key = aliases = attrs = tags = locks = permissions = location = home = destination = ""
if "key" in prototype:
key = prototype["key"]
key = "|ckey:|n {key}".format(key=key)
aliases = prototype.get("aliases", "")
if aliases:
if "aliases" in prototype:
aliases = prototype["aliases"]
aliases = "|caliases:|n {aliases}".format(aliases=", ".join(aliases))
attrs = prototype.get("attrs", "")
if attrs:
if "attrs" in prototype:
attrs = prototype["attrs"]
out = []
for (attrkey, value, category, locks) in attrs:
locks = ", ".join(lock for lock in locks if lock)
@ -751,8 +751,8 @@ def prototype_to_str(prototype):
)
)
attrs = "|cattrs:|n\n {attrs}".format(attrs="\n ".join(out))
tags = prototype.get("tags", "")
if tags:
if "tags" in prototype:
tags = prototype['tags']
out = []
for (tagkey, category, data) in tags:
out.append(
@ -761,20 +761,20 @@ def prototype_to_str(prototype):
)
)
tags = "|ctags:|n\n {tags}".format(tags=", ".join(out))
locks = prototype.get("locks", "")
if locks:
if "locks" in prototype:
locks = prototype["locks"]
locks = "|clocks:|n\n {locks}".format(locks=locks)
permissions = prototype.get("permissions", "")
if permissions:
if "permissions" in prototype:
permissions = prototype["permissions"]
permissions = "|cpermissions:|n {perms}".format(perms=", ".join(permissions))
location = prototype.get("location", "")
if location:
if "location" in prototype:
location = prototype["location"]
location = "|clocation:|n {location}".format(location=location)
home = prototype.get("home", "")
if home:
if "home" in prototype:
home = prototype["home"]
home = "|chome:|n {home}".format(home=home)
destination = prototype.get("destination", "")
if destination:
if "destination" in prototype:
destination = prototype["destination"]
destination = "|cdestination:|n {destination}".format(destination=destination)
body = "\n".join(

View file

@ -354,6 +354,9 @@ def prototype_diff(prototype1, prototype2, maxdepth=2, homogenize=False, implici
class Unset:
def __bool__(self):
return False
def __str__(self):
return "<Unset>"
_unset = Unset()
def _recursive_diff(old, new, depth=0):
@ -361,10 +364,10 @@ def prototype_diff(prototype1, prototype2, maxdepth=2, homogenize=False, implici
old_type = type(old)
new_type = type(new)
if old_type == new_type and not (old and new):
if old_type == new_type and not (old or new):
# both old and new are unset, like [] or None
return (old, new, "KEEP")
elif old_type != new_type:
return (None, None, "KEEP")
if old_type != new_type:
if old and not new:
if depth < maxdepth and old_type == dict:
return {key: (part, None, "REMOVE") for key, part in old.items()}
@ -388,7 +391,7 @@ def prototype_diff(prototype1, prototype2, maxdepth=2, homogenize=False, implici
elif depth < maxdepth and new_type == dict:
all_keys = set(list(old.keys()) + list(new.keys()))
return {
key: _recursive_diff(old.get(key), new.get(key, _unset), depth=depth + 1)
key: _recursive_diff(old.get(key, _unset), new.get(key, _unset), depth=depth + 1)
for key in all_keys
}
elif depth < maxdepth and is_iter(new):
@ -396,7 +399,7 @@ def prototype_diff(prototype1, prototype2, maxdepth=2, homogenize=False, implici
new_map = {part[0] if is_iter(part) else part: part for part in new}
all_keys = set(list(old_map.keys()) + list(new_map.keys()))
return {
key: _recursive_diff(old_map.get(key), new_map.get(key, _unset), depth=depth + 1)
key: _recursive_diff(old_map.get(key, _unset), new_map.get(key, _unset), depth=depth + 1)
for key in all_keys
}
elif old != new: