archey4/archey/entry.py
Samuel FORESTIER 1369a69ed0 [CORE][BREAKING] Implements "pretty naming" for multiple-words entries
> Based on @GentleHoneyLover's comment (<https://github.com/HorlogeSkynet/archey4/issues/98#issuecomment-827838361>) :
>   * `DesktopEnvironment` -> `Desktop Environment`
>   * `WindowManager` -> `Window Manager`
>   * `LAN_IP` -> `LAN IP`
>   * `WAN_IP` -> `WAN IP`

Unless your configuration file specifies `name` option for entries, this will change text and JSON output entries names/keys.
2021-05-08 11:31:18 +02:00

45 lines
1.7 KiB
Python

"""Entry base class"""
from abc import ABC as AbstractBaseClass, abstractmethod
from typing import Optional
from archey.configuration import Configuration
class Entry(AbstractBaseClass):
"""Module base class"""
_PRETTY_NAME: Optional[str] = None
def __new__(cls, *_, **kwargs):
"""Hook object instantiation to handle our particular `disabled` config field"""
if kwargs.get('options', {}).get('disabled'):
return None
return super().__new__(cls)
@abstractmethod
def __init__(self, name: str = None, value=None, options: dict = None):
# Each entry will have always have the following attributes...
# `name`: key (defaults to the instantiated entry class name);
# `value`: value of entry as an appropriate object;
# `options`: configuration options *specific* to an entry instance;
self.name = name or self._PRETTY_NAME or self.__class__.__name__
self.value = value
self.options = options or {}
# Propagates a reference to default strings specified in `Configuration`.
self._default_strings = Configuration().get('default_strings')
def output(self, output):
"""Output the results to output. Can be overridden by subclasses."""
if self.value:
# Let's assume we can just use `__str__` on the object in value,
# and create a single-line output with it.
output.append(self.name, str(self.value))
else:
# If the value is "falsy" leave a generic "Not detected" message for this entry.
output.append(
self.name,
self._default_strings.get('not_detected')
)