From 20bf632b2dcff6be0eae26a14f999790981854fd Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 1 Mar 2025 22:22:02 +0100 Subject: [PATCH] [ENTRIES] Implements `hide_undetected` option to mask missing values --- CHANGELOG.md | 4 ++++ README.md | 3 +++ archey/__main__.py | 2 +- archey/entry.py | 3 +++ archey/output.py | 17 +++++++++-------- archey/test/test_archey_entry.py | 4 +++- config.json | 1 + 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f73e03e..e4ac0200 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project (partially) adheres to [Semantic Versioning](https://semver.org ### Added - Python 3.13 & 3.14 official support - `entries_color` config option validation +- `hide_undetected` config option to hide undetected entries + +### Changed +- `Entry` behavior in boolean contexts ("truthy" when `value` is populated) ## [v4.15.0.0] - 2024-09-30 ### Added diff --git a/README.md b/README.md index 2cc1c46d..dd85fe68 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,9 @@ Below stand further descriptions for each available (default) option : // Note that the `--logo-style` argument overrides this setting. "logo_style": "", // + // Use this option to hide undetected entries, to prevent "Not detected" messages from being displayed. + "hide_undetected": false, + // // Enable icons for entries. // A terminal "nerd font" is required to display the icons. Otherwise, these are simply missing and a placeholder will be seen. // You can also refer to : . diff --git a/archey/__main__.py b/archey/__main__.py index fed4bf25..c764f1f3 100644 --- a/archey/__main__.py +++ b/archey/__main__.py @@ -184,7 +184,7 @@ def _entry_instantiator(entry: dict) -> Optional[Entry]: mapper = executor.map for entry_instance in mapper(_entry_instantiator, available_entries): - if entry_instance: + if entry_instance is not None: output.add_entry(entry_instance) output.output() diff --git a/archey/entry.py b/archey/entry.py index 539bbd6b..37b87cef 100644 --- a/archey/entry.py +++ b/archey/entry.py @@ -44,6 +44,9 @@ def __init__(self, name: Optional[str] = None, value=None, options: Optional[dic # Provision a logger for each entry. self._logger = logging.getLogger(self.__module__) + def __bool__(self) -> bool: + return bool(self.value) + def output(self, output) -> None: """Output the results to output. Can be overridden by subclasses.""" if self.value: diff --git a/archey/output.py b/archey/output.py index 934ed8ad..430bffe3 100644 --- a/archey/output.py +++ b/archey/output.py @@ -18,7 +18,7 @@ from archey.logos import get_logo_width, lazy_load_logo_module -class Output: +class Output: # pylint: disable=too-many-instance-attributes """ This is the object handling output entries populating. It also handles the logo choice based on some system detections. @@ -27,12 +27,12 @@ class Output: __logo_right_padding = " " def __init__(self, **kwargs): - configuration = Configuration() + self.configuration = Configuration() # Fetches passed arguments. self._format_to_json = kwargs.get("format_to_json") preferred_logo_style = ( - kwargs.get("preferred_logo_style") or configuration.get("logo_style") or "" + kwargs.get("preferred_logo_style") or self.configuration.get("logo_style") or "" ).upper() # If logo shouldn't be displayed, don't load any module and reset right padding @@ -59,11 +59,11 @@ def __init__(self, **kwargs): # If `os-release`'s `ANSI_COLOR` option is set, honor it. ansi_color = Distributions.get_ansi_color() - if ansi_color and configuration.get("honor_ansi_color"): + if ansi_color and self.configuration.get("honor_ansi_color"): # Replace each Archey integrated colors by `ANSI_COLOR`. self._colors = len(self._colors) * [Style.escape_code_from_attrs(ansi_color)] - entries_color = configuration.get("entries_color") + entries_color = self.configuration.get("entries_color") if entries_color: self._entries_color = Style.escape_code_from_attrs(entries_color) elif self._colors: @@ -76,9 +76,9 @@ def __init__(self, **kwargs): # Each class output will be added in the list below afterwards self._results = [] - def add_entry(self, module: Entry) -> None: + def add_entry(self, entry: Entry) -> None: """Append an entry to the list of entries to output""" - self._entries.append(module) + self._entries.append(entry) def append(self, key: str, value) -> None: """Append a pre-formatted entry to the final output content""" @@ -95,7 +95,8 @@ def output(self) -> None: else: # Iterate through the entries and run their output method to add their content. for entry in self._entries: - entry.output(self) + if not self.configuration.get("hide_undetected") or entry: + entry.output(self) self._output_text() def _output_json(self) -> None: diff --git a/archey/test/test_archey_entry.py b/archey/test/test_archey_entry.py index a0da5204..63039309 100644 --- a/archey/test/test_archey_entry.py +++ b/archey/test/test_archey_entry.py @@ -30,7 +30,7 @@ def test_entry_itself(self): def test_entry_disabling(self): """Test `Entry` _disabling_""" simple_entry = _SimpleEntry() - self.assertTrue(simple_entry) + self.assertIsNotNone(simple_entry) simple_entry = _SimpleEntry(options={"disabled": True}) self.assertIsNone(simple_entry) @@ -44,6 +44,7 @@ def test_entry_usage(self): simple_entry = _SimpleEntry() self.assertEqual(simple_entry.name, "Simple Entry") self.assertIsNone(simple_entry.value) + self.assertFalse(simple_entry) # No `_PRETTY_NAME` is defined : proper fall-back on entry internal name. delattr(_SimpleEntry, "_PRETTY_NAME") @@ -53,6 +54,7 @@ def test_entry_usage(self): simple_entry = _SimpleEntry("T", "est") self.assertEqual(simple_entry.name, "T") self.assertEqual(simple_entry.value, "est") + self.assertTrue(simple_entry) def test_entry_output_overriding(self): """Check `Entry.output` public method overriding""" diff --git a/config.json b/config.json index ae236996..cf9c3fcc 100644 --- a/config.json +++ b/config.json @@ -5,6 +5,7 @@ "entries_color": "", "honor_ansi_color": true, "logo_style": "", + "hide_undetected": false, "entries_icon": false, "entries": [ { "type": "User" },