Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/incatools/odk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def update_config_dict(obj: Dict[str, Any]) -> None:

:param obj: The dictionary to update.
"""
# First take care of stubs, if needed
# First remove pointless null values (typically the result of an
# auto-generated config file, when seeding without a file)
Comment thread
gouttegd marked this conversation as resolved.
purge_null_values(obj)

# Then take care of stubs, if needed
update_stubs(obj)

# Then all the other simple changes
Expand Down Expand Up @@ -266,6 +270,22 @@ def update_config_dict(obj: Dict[str, Any]) -> None:
spec["compressions"] = ["none", "gz"]


def purge_null_values(obj: Any) -> None:
"""Removes all null values from a dictionary, recursively."""
if isinstance(obj, dict):
nulls = []
for k, v in obj.items():
if v is None:
nulls.append(k)
else:
purge_null_values(v)
for null in nulls:
obj.pop(null)
elif isinstance(obj, list):
for i in obj:
purge_null_values(i)


def pop_key(obj: Dict[str, Any], path: str) -> Optional[str]:
"""Gets the value of a key in a nested dictionary structure.

Expand Down Expand Up @@ -341,4 +361,6 @@ def save_config(project: OntologyProject, output: TextIO) -> None:
:param project: The project to save.
:param output: The file-like object where to save the project.
"""
output.write(yaml.dump(project.to_dict(), default_flow_style=False))
project_as_dict = project.to_dict()
purge_null_values(project_as_dict)
output.write(yaml.dump(project_as_dict, default_flow_style=False))
Loading