Skip to content

Commit a1bbf79

Browse files
authored
Add option to skip none values when printing a configuration (#24)
1 parent d8bd284 commit a1bbf79

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/configaroo/configuration.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ def to_flat_dict(self, _prefix: str = "") -> dict[str, Any]:
221221

222222

223223
def print_configuration(
224-
config: Configuration | BaseModel, section: str | None = None, indent: int = 4
224+
config: Configuration | BaseModel,
225+
section: str | None = None,
226+
*,
227+
skip_none: bool = False,
228+
indent: int = 4,
225229
) -> None:
226230
"""Pretty print a configuration.
227231
@@ -232,18 +236,22 @@ def print_configuration(
232236
)
233237
if section is None:
234238
_print, _escape = _get_rich_print()
235-
return _print_dict_as_tree(cfg, indent=indent, _print=_print, _escape=_escape)
239+
return _print_dict_as_tree(
240+
cfg, skip_none=skip_none, indent=indent, _print=_print, _escape=_escape
241+
)
236242

237243
cfg_section = cfg.get(section)
238244
if cfg_section is None:
239245
message = f"'{type(cfg).__name__}' has no section '{section}'"
240246
raise KeyError(message) from None
241247

242248
if isinstance(cfg_section, Configuration):
243-
return print_configuration(cfg_section, indent=indent)
249+
return print_configuration(cfg_section, skip_none=skip_none, indent=indent)
244250

245251
*_, key = section.split(".")
246-
return print_configuration(Configuration({key: cfg_section}), indent=indent)
252+
return print_configuration(
253+
Configuration({key: cfg_section}), skip_none=skip_none, indent=indent
254+
)
247255

248256

249257
def _get_rich_print() -> tuple[
@@ -264,13 +272,17 @@ def _get_rich_print() -> tuple[
264272

265273
def _print_dict_as_tree(
266274
data: dict[str, Any] | UserDict[str, Any] | Configuration,
275+
*,
276+
skip_none: bool = False,
267277
indent: int = 4,
268278
current_indent: int = 0,
269279
_print: Callable[[str], None] = print,
270280
_escape: Callable[[str], str] = str,
271281
) -> None:
272282
"""Print a nested dictionary as a tree."""
273283
for key, value in data.items():
284+
if skip_none and value is None:
285+
continue
274286
if isinstance(value, dict | UserDict | Configuration):
275287
_print(" " * current_indent + f"- {key}")
276288
_print_dict_as_tree(

tests/test_print.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,27 @@ def test_printing_of_rich_markup() -> None:
100100
"""Test that a config value containing malformed Rich markup can be printed."""
101101
config = Configuration({"markup": "[/]"})
102102
print_configuration(config)
103+
104+
105+
def test_print_keeping_none(
106+
capsys: pytest.CaptureFixture[str], config: Configuration
107+
) -> None:
108+
"""Test that None-values are kept in printout by default."""
109+
print_configuration(config | {"none": None})
110+
stdout = capsys.readouterr().out
111+
lines = stdout.splitlines()
112+
113+
assert "- none: None" in lines
114+
assert "- number: 42" in lines
115+
116+
117+
def test_print_skipping_none(
118+
capsys: pytest.CaptureFixture[str], config: Configuration
119+
) -> None:
120+
"""Test that None-values are skipped in printout if asked for."""
121+
print_configuration(config | {"none": None}, skip_none=True)
122+
stdout = capsys.readouterr().out
123+
lines = stdout.splitlines()
124+
125+
assert "- none: None" not in lines
126+
assert "- number: 42" in lines

0 commit comments

Comments
 (0)