Skip to content

Commit 95fe87e

Browse files
committed
applying remove_empty instead of remove_empty_string
1 parent 0bb9ec9 commit 95fe87e

3 files changed

Lines changed: 58 additions & 22 deletions

File tree

‎src/osw/core.py‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
get_uuid,
3232
is_empty,
3333
namespace_from_full_title,
34-
remove_empty_strings,
34+
remove_empty,
3535
title_from_full_title,
3636
)
3737
from osw.wiki_tools import SearchParam
@@ -605,8 +605,9 @@ class LoadEntityParam(BaseModel):
605605
autofetch_schema: Optional[bool] = True
606606
"""If true, load the corresponding schemas /
607607
categories ad-hoc if not already present"""
608-
remove_empty_strings: Optional[bool] = True
609-
"""If true, remove key with an empty string value from the jsondata."""
608+
remove_empty: Optional[bool] = True
609+
"""If true, remove key with an empty string, list, dict or set as value
610+
from the jsondata."""
610611
disable_cache: bool = False
611612
"""If true, disable the cache for the loading process"""
612613

@@ -662,8 +663,8 @@ def load_entity(
662663
schemas = []
663664
schemas_fetched = True
664665
jsondata = page.get_slot_content("jsondata")
665-
if param.remove_empty_strings:
666-
remove_empty_strings(jsondata)
666+
if param.remove_empty:
667+
remove_empty(jsondata)
667668
if jsondata:
668669
for category in jsondata["type"]:
669670
schema = (
@@ -780,7 +781,7 @@ class _ApplyOverwriteParam(OswBaseModel):
780781
namespace: Optional[str]
781782
meta_category_title: Optional[str]
782783
meta_category_template_str: Optional[str]
783-
remove_empty_strings: Optional[bool] = True
784+
remove_empty: Optional[bool] = True
784785
inplace: Optional[bool] = False
785786
debug: Optional[bool] = False
786787

@@ -861,8 +862,8 @@ def set_content(content_to_set: dict) -> None:
861862
):
862863
# Use pydantic serialization, skip none values:
863864
new_content["jsondata"] = json.loads(param.entity.json(exclude_none=True))
864-
if param.remove_empty_strings:
865-
remove_empty_strings(new_content["jsondata"])
865+
if param.remove_empty:
866+
remove_empty(new_content["jsondata"])
866867
set_content(new_content)
867868
page.changed = True
868869
return page # Guard clause --> exit function
@@ -892,8 +893,8 @@ def set_content(content_to_set: dict) -> None:
892893
for slot in ["jsondata", "header", "footer"]: # SLOTS:
893894
remote_content[slot] = page.get_slot_content(slot)
894895
# Todo: remote content does not contain properties that are not set
895-
if param.remove_empty_strings:
896-
remove_empty_strings(remote_content["jsondata"])
896+
if param.remove_empty:
897+
remove_empty(remote_content["jsondata"])
897898
if remote_content["header"]: # not None or {} or ""
898899
new_content["header"] = remote_content["header"]
899900
if remote_content["footer"]:
@@ -904,8 +905,8 @@ def set_content(content_to_set: dict) -> None:
904905
# Properties that are not set in the local content will be set to None
905906
# We want those not to be listed as keys
906907
local_content["jsondata"] = json.loads(param.entity.json(exclude_none=True))
907-
if param.remove_empty_strings:
908-
remove_empty_strings(local_content["jsondata"])
908+
if param.remove_empty:
909+
remove_empty(local_content["jsondata"])
909910
if param.debug:
910911
print(f"'local_content': {str(remote_content)}")
911912
# Apply the overwrite logic
@@ -981,7 +982,7 @@ class StoreEntityParam(OswBaseModel):
981982
"""A list of OverwriteClassParam objects. If a class specific overwrite setting
982983
is set, this setting is used.
983984
"""
984-
remove_empty_strings: Optional[bool] = True
985+
remove_empty: Optional[bool] = True
985986
"""If true, remove key with an empty string value from the jsondata."""
986987
change_id: Optional[str] = None
987988
"""ID to document the change. Entities within the same store_entity() call will
@@ -1101,15 +1102,15 @@ def store_entity_(
11011102
namespace=namespace_,
11021103
policy=overwrite_class_param,
11031104
meta_category_template_str=meta_category_template_str,
1104-
remove_empty_strings=param.remove_empty_strings,
1105+
remove_empty=param.remove_empty,
11051106
debug=param.debug,
11061107
)
11071108
)
11081109
if meta_category_template:
11091110
try:
11101111
jsondata = page.get_slot_content("jsondata")
1111-
if param.remove_empty_strings:
1112-
remove_empty_strings(jsondata)
1112+
if param.remove_empty:
1113+
remove_empty(jsondata)
11131114
schema_str = eval_compiled_handlebars_template(
11141115
meta_category_template,
11151116
jsondata,

‎src/osw/utils/wiki.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,35 @@ def remove_empty_strings(d: dict, inplace: bool = True) -> dict:
106106
if value == "":
107107
del d[key]
108108
return d
109+
110+
111+
def remove_empty(d: dict, inplace: bool = True) -> dict:
112+
"""Iterates through the dictionary structure and removes key-value pairs
113+
where the value is an empty string, list, set or dictionary
114+
115+
Parameters
116+
----------
117+
d:
118+
The dictionary to perform the operation on
119+
inplace:
120+
Whether to perform the operation in place or return a new dictionary
121+
122+
Returns
123+
-------
124+
result:
125+
The modified dictionary
126+
"""
127+
if not inplace:
128+
d = deepcopy(d)
129+
keys = list(d.keys())
130+
for key in keys:
131+
value = d[key]
132+
if isinstance(value, dict):
133+
remove_empty(value)
134+
elif isinstance(value, list):
135+
for item in value:
136+
if isinstance(item, dict):
137+
remove_empty(item)
138+
if value in ["", [], {}, set()]:
139+
del d[key]
140+
return d

‎tests/test_osl.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import osw.model.entity as model
66
from osw.core import OSW, AddOverwriteClassOptions, OverwriteOptions
7-
from osw.utils.wiki import remove_empty_strings
7+
from osw.utils.wiki import remove_empty
88
from osw.wtsite import WtPage
99

1010

@@ -58,8 +58,8 @@ def check_false(original: model.Entity, altered: model.Entity, stored: model.Ent
5858
assert stored.name == original.name
5959
# empty string property is removed on store and load:
6060
assert stored.iri == altered.iri
61-
if len(original.description) == 0:
62-
assert stored.description == original.description
61+
if original.description is None or len(original.description) == 0:
62+
assert stored.description == altered.description
6363
else:
6464
assert stored.description[0].text == original.description[0].text
6565
assert stored.query_label == altered.query_label # value == None -->
@@ -108,7 +108,10 @@ def check_keep_existing(
108108
# assert stored.iri == original.iri
109109
# empty string property is removed on store and load:
110110
assert getattr(stored, "iri", None) is None
111-
assert stored.description == original.description # empty list
111+
if original.description is None or len(original.description) == 0: # empty list
112+
assert stored.description is None
113+
else:
114+
assert stored.description[0].text == original.description[0].text
112115
assert stored.query_label == original.query_label
113116
assert stored.image == original.image
114117
assert getattr(stored, "attachments", None) is None
@@ -154,9 +157,9 @@ def test_apply_overwrite_policy():
154157
+ OSW.get_osw_id(original_item_local.uuid)
155158
)
156159
jsondata = json.loads(original_item_local.json(exclude_none=True))
157-
# Emulate the default setting for StoreEntityParam.remove_empty_strings,
160+
# Emulate the default setting for StoreEntityParam.remove_empty,
158161
# which is True and applied on store_entity
159-
remove_empty_strings(jsondata)
162+
remove_empty(jsondata)
160163

161164
original_page.set_slot_content("jsondata", jsondata)
162165
# To reproduce what happens in the OSL instance, we need to get the content

0 commit comments

Comments
 (0)