From 65e6145bb6c8890604d09608dfb3f9cc1967ed77 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 15 Apr 2024 12:01:38 +0200 Subject: [PATCH] Finishing console output of bin/get-intern-used-from-addresses. --- lib/pp_admintools/app/get_from_addr.py | 99 +++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/lib/pp_admintools/app/get_from_addr.py b/lib/pp_admintools/app/get_from_addr.py index e47bb79..8b92b0b 100644 --- a/lib/pp_admintools/app/get_from_addr.py +++ b/lib/pp_admintools/app/get_from_addr.py @@ -23,7 +23,7 @@ from . import BaseDPXApplication from ..handler.pflogparse import PostfixLogfileParser from ..xlate import XLATOR -__version__ = '0.6.0' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -317,12 +317,107 @@ class GetFromAddressesApp(BaseDPXApplication): self.pflogparser.evaluate_logfiles(self.logfiles) self._collect_results() - if self.verbose > 0: + if self.verbose > 2: self.empty_line() LOG.debug(_('Results per domain:') + '\n' + pp(self.per_domain)) self.empty_line() LOG.debug(_('Results per address:') + '\n' + pp(self.per_address)) + if self.show_address_results: + self._output_results_per_address() + self._output_results_per_domain() + + # ------------------------------------------------------------------------- + def _output_results_per_address(self): + + max_len_loghost = 1 + max_len_address = 1 + max_len_count = 1 + + title = _('Results per address:') + self.empty_line() + print(self.colored(title, 'CYAN')) + self.line(width=len(title), linechar='-', color='CYAN') + + for loghost in self.per_address.keys(): + + if len(loghost) > max_len_loghost: + max_len_loghost = len(loghost) + + for address in self.per_address[loghost].keys(): + count = self.per_address[loghost][address] + if len(address) > max_len_address: + max_len_address = len(address) + if len(str(count)) > max_len_count: + max_len_count = len(str(count)) + + template = '{{h:<{lh}}} {{a:<{la}}} - {{c:>{lc}}}'.format( + lh=max_len_loghost, la=max_len_address, lc=max_len_count) + + for loghost in sorted(self.per_address.keys(), key=str.lower): + + first = True + + self.empty_line() + for address in sorted( + self.per_address[loghost].keys(), + key=lambda x: self.per_address[loghost][x], + reverse=True): + lh_out = loghost + if not first: + lh_out = '' + + count = self.per_address[loghost][address] + print(template.format(h=lh_out, a=address, c=count)) + + first = False + + # ------------------------------------------------------------------------- + def _output_results_per_domain(self): + + max_len_loghost = 1 + max_len_domain = 1 + max_len_count = 1 + + title = _('Results per domain:') + self.empty_line() + print(self.colored(title, 'CYAN')) + self.line(width=len(title), linechar='-', color='CYAN') + + for loghost in self.per_domain.keys(): + + if len(loghost) > max_len_loghost: + max_len_loghost = len(loghost) + + for domain in self.per_domain[loghost].keys(): + count = self.per_domain[loghost][domain] + if len(domain) > max_len_domain: + max_len_domain = len(domain) + if len(str(count)) > max_len_count: + max_len_count = len(str(count)) + + template = '{{h:<{lh}}} {{d:<{ld}}} - {{c:>{lc}}}'.format( + lh=max_len_loghost, ld=max_len_domain, lc=max_len_count) + + for loghost in sorted(self.per_domain.keys(), key=str.lower): + + first = True + + self.empty_line() + + for domain in sorted( + self.per_domain[loghost].keys(), + key=lambda x: self.per_domain[loghost][x], + reverse=True): + lh_out = loghost + if not first: + lh_out = '' + + count = self.per_domain[loghost][domain] + print(template.format(h=lh_out, d=domain, c=count)) + + first = False + # ============================================================================= if __name__ == '__main__': -- 2.39.5