diff options
Diffstat (limited to 'hostinfo/prefix.py')
-rw-r--r-- | hostinfo/prefix.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/hostinfo/prefix.py b/hostinfo/prefix.py new file mode 100644 index 0000000..e9f0bf2 --- /dev/null +++ b/hostinfo/prefix.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +from __future__ import print_function + +class Printer: + flags = list() + + def __init__(self, full_key='', printer=None): + if printer is None: + self.output = print + else: + self.output = printer.pprint + + self.parent = printer + self.empty = 'oneline' in self.__class__.flags + self.full_key = full_key + self.has_output = False + self.label = '' + + def set_label(self, label='', maxlength=0): + self.label = self._get_label(label, maxlength) + + def _get_label(self, label, maxlength): + if label == '': + return label + + label = "%s: " % label + if self.parent is not None and self.parent.full_key == '': + if 'nospaces' in self.__class__.flags: + return label.rjust(maxlength+4) + else: + return (label + " ").rjust(maxlength+6) + else: + return label.ljust(maxlength+2) + + def pprint(self, data): + self.output("%s%s" % (self.label, data)) + self.has_output = True + if not self.empty: + self.label = ' ' * len(self.label) + self.empty = True + + def space(self, force=False): + if 'nospaces' not in self.__class__.flags: + if self.has_output or force: + self.output('') + |