archey4/archey/entry.py
Michael Bromilow 5889969164 [FEATURE] [BREAKING] Change configuration layout (#80)
`config.json` has been reshuffled so that entries are now represented as a dict with `type` as long as their respective options as keys.

+ Entries order can be rearranged from configuration (closes #57)
+ Entries now have an extra `options` attribute, which contains their own settings
+ Entries can be specified multiple times in the configuration with each one having independent options from the others
+ Implements temporary entry hiding (`disabled` option field)
+ Defaults entry `name` attribute to instantiated class name
+ Allows entry renaming (`name` option field)
+ Adapts documentation of new configuration layout in README

* Various miscellaneous changes to support the new `entry_options` attribute.
* `LanIp` and `WanIp` internally renamed to `LanIP` and `WanIP` respectively
* Sets `Disk.get_df_output_dict` as private method
* Fixes `Raspberry` spelling :)

- Stops providing a global `Configuration` reference to entries (only one to `default_strings` for i18n)
- Sets `use_unicode` option as `Terminal`-specific
- Sets `honor_ansi_color` option as project-global

Co-authored-by: Samuel FORESTIER <dev@samuel.domains>
2020-11-22 16:30:28 +01:00

39 lines
1.5 KiB
Python

"""Entry base class"""
from abc import ABC as AbstractBaseClass, abstractmethod
from archey.configuration import Configuration
class Entry(AbstractBaseClass):
"""Module base class"""
@abstractmethod
def __init__(self, name=None, value=None, options=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.__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 __bool__(self):
"""Makes an `Entry` evaluates to _falsy_ if `disabled` config field is _truthy_"""
return not bool(self.options.get('disabled'))
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')
)