From 8f992004180d709bcbf783b9c71117dad3d05395 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Wed, 14 Aug 2024 15:25:32 +0400 Subject: [PATCH] Fix removal of empty strings --- click_extra/config.py | 2 +- click_extra/platforms.py | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/click_extra/config.py b/click_extra/config.py index d8d97701d..88e2b2ce1 100644 --- a/click_extra/config.py +++ b/click_extra/config.py @@ -362,7 +362,7 @@ def merge_default_map(self, ctx: Context, user_conf: dict) -> None: # Clean-up the conf by removing all blank values left-over by the template # structure. - clean_conf = remove_blanks(filtered_conf) + clean_conf = remove_blanks(filtered_conf, remove_str=False) # Update the default_map. if ctx.default_map is None: diff --git a/click_extra/platforms.py b/click_extra/platforms.py index 676cd2272..587ab6c89 100644 --- a/click_extra/platforms.py +++ b/click_extra/platforms.py @@ -180,17 +180,32 @@ def recursive_update( return a -def remove_blanks(tree: dict) -> dict: - """Returns a copy of a dict without items whose values are `None` or empty `dict`. - - Works recusively. +def remove_blanks( + tree: dict, + remove_none: bool = True, + remove_dicts: bool = True, + remove_str: bool = True, +) -> dict: + """Returns a copy of a dict without items whose values blanks. + + Are considered blanks: + - `None` values + - empty strings + - empty `dict` + + The removal of each of these class can be skipped by setting ``remove_*`` + parameters. + + Dictionarries are inspected recusively and their own blank values are removed. """ def visit(path, key, value) -> bool: - """Skip `None` values and empty `dict`.""" - if value is None: + """Ignore some class of blank values depending on configuration.""" + if remove_none and value is None: + return False + if remove_dicts and isinstance(value, dict) and not len(value): return False - if isinstance(value, dict) and not len(value): + if remove_str and isinstance(value, str) and not len(value): return False return True @@ -232,7 +247,7 @@ def info(self) -> dict[str, str | None | dict[str, str | None]]: "like": None, "codename": None, } - # Get that extra info from distro. + # Get extra info from distro. if distro.id() == self.id: cleaned_info = remove_blanks(distro.info()) info = recursive_update(info, cleaned_info)