diff options
author | Laurie Clark-Michalek <bluepeppers@archlinux.us> | 2010-11-19 13:49:19 +0000 |
---|---|---|
committer | Laurie Clark-Michalek <bluepeppers@archlinux.us> | 2010-11-19 13:49:19 +0000 |
commit | a629b153f8d889133e2fb5984321b0626e94443e (patch) | |
tree | b962514a4aa4c817f963dd3a363fb82264b97892 | |
parent | 3e92ed801be456782f0b327d79c7db117a1c40f8 (diff) |
Evaluate display classes at the last minute, and via an accessor function.
This allows display classes to implement functions to return their key value pairs, instead of forcing them to do it in the __init__ function.
Output.append has been removed, as it is implemented by the inherited list.
-rw-r--r-- | archey.new | 46 |
1 files changed, 42 insertions, 4 deletions
@@ -187,13 +187,39 @@ class Output(list): new_results[start:start + length - 1] = results return new_results + + def _get_results(self): + """ + Returns a dict of the keys and values of the currently registered display classes. + NOTE: Will not include any key value pairs where either evaluates to False. - def append(self, display): - super().append('%s%s: %s%s' % (self._color(1), display.key, self._color('Clear'), display.value)) + >>>out = Output() + >>>class TestDisplay(): + ... key = 'Foo' + ... value = 'Bar' + ... key_value_pair = lambda self: {self.key: self.value} + ... + >>>out.append(TestDisplay()) + >>>out._get_results() + {'Foo': 'Bar'} + """ + return {key:value for key, value in [disp.key_value_pair() for disp in self] if key and value} def output(self): - results = self._center_results(self) - print(self._get_logo().format(color=self._color(self.distro), results=results)) + """ + Outputs the archey logo and information. Reads Display classes from the internal list, + and formats them, adding color. The final pretty list is then centered (though other alignments + may be added) and printed. + """ + + results = self._get_results() + + unformatted_stn = '{color}{key}: {clear}{value}{clear}' + pretty_results = [unformatted_stn.format(color=self._color(1), key=key, value=value, clear=self._color('Clear')) + for key, value in results.items()] + + centered_results = self._center_results(pretty_results) + print(self._get_logo().format(color=self._color(self.distro), results=centered_results)) class BaseDisplay(): """ @@ -216,6 +242,18 @@ class BaseDisplay(): def __repr__(self): return '<{0}: key={1}, value={2}>'.format(self.__class__.__name__, self.key, self.value) + + def key_value_pair(self): + """ + Returns a tuple of the key and value of the current display. + + >>>disp = BaseDisplay() + >>>disp.key = 'Foo' + >>>disp.value = 'Bar' + >>>disp.key_value_pair() + ('Foo', 'Bar') + """ + return (self.key, self.value) #class Hostname: # def __init__(self): |