Skip to content
Snippets Groups Projects
Commit ef3ecc9b authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

feat(apiutils): added the possibility to use custom labels instead of 'old'...

feat(apiutils): added the possibility to use custom labels instead of 'old' and 'new' in describe_diff function
parent b4f20073
No related branches found
No related tags found
2 merge requests!159Release 0.16.o,!154Added the possibility to use custom labels instead of 'old' and 'new'
Pipeline #56802 passed with warnings
...@@ -514,9 +514,9 @@ def merge_entities(entity_a: Entity, ...@@ -514,9 +514,9 @@ def merge_entities(entity_a: Entity,
def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any],
name: str = None, name: str = None,
as_update=True, as_update: bool = True,
label_old="old version", label_old: str = "old version",
label_new="new version"): label_new: str = "new version") -> str:
""" """
Generate a textual description of the differences between two entities. Generate a textual description of the differences between two entities.
These can be generated using :func:`compare_entities` and used within this function like this: These can be generated using :func:`compare_entities` and used within this function like this:
...@@ -541,6 +541,16 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], ...@@ -541,6 +541,16 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any],
as_update: bool as_update: bool
Default True. Not used anymore. Default True. Not used anymore.
label_old: str
Can be used to set a custom label for the diff that is associated with the old entity.
label_new: str
Can be used to set a custom label for the diff that is associated with the new entity.
Returns:
--------
A text description of the differences.
""" """
description = "" description = ""
...@@ -548,17 +558,17 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], ...@@ -548,17 +558,17 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any],
if attr == "parents" or attr == "properties": if attr == "parents" or attr == "properties":
continue continue
description += "{} differs:\n".format(attr) description += "{} differs:\n".format(attr)
description += "old version: {}\n".format( description += label_old + ": {}\n".format(
olddiff[attr] if attr in olddiff else "not set") olddiff[attr] if attr in olddiff else "not set")
description += "new version: {}\n\n".format( description += label_new + ": {}\n\n".format(
newdiff[attr] if attr in newdiff else "not set") newdiff[attr] if attr in newdiff else "not set")
if len(olddiff["parents"]) > 0: if len(olddiff["parents"]) > 0:
description += ("Parents that are only in the old version:\n" description += ("Parents that are only in the " + label_old + ":\n"
+ ", ".join(olddiff["parents"]) + "\n") + ", ".join(olddiff["parents"]) + "\n")
if len(newdiff["parents"]) > 0: if len(newdiff["parents"]) > 0:
description += ("Parents that are only in the new version:\n" description += ("Parents that are only in the " + label_new + ":\n"
+ ", ".join(olddiff["parents"]) + "\n") + ", ".join(olddiff["parents"]) + "\n")
for prop in list(set(list(olddiff["properties"].keys()) for prop in list(set(list(olddiff["properties"].keys())
...@@ -566,18 +576,21 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], ...@@ -566,18 +576,21 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any],
description += "property {} differs:\n".format(prop) description += "property {} differs:\n".format(prop)
if prop not in olddiff["properties"]: if prop not in olddiff["properties"]:
description += "it does not exist in the old version: \n" description += "it does not exist in the " + label_old + ": \n"
elif prop not in newdiff["properties"]: elif prop not in newdiff["properties"]:
description += "it does not exist in the new version: \n" description += "it does not exist in the " + label_new + ": \n"
else: else:
description += "old version: {}\n".format( description += label_old + ": {}\n".format(
olddiff["properties"][prop]) olddiff["properties"][prop])
description += "new version: {}\n\n".format( description += label_new + ": {}\n\n".format(
newdiff["properties"][prop]) newdiff["properties"][prop])
if description != "": if description != "":
description = ("## Difference between the old and the new " description = ("## Difference between the " +
"version of {}\n\n".format(name))+description label_old +
" and the " +
label_new +
" of {}\n\n".format(name))+description
return description return description
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment