From: Frank Brehm Date: Mon, 15 Apr 2024 15:27:44 +0000 (+0200) Subject: Generating totals yaml file. X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=bad0384bbc251dbd276885ea69440c1134a07bc1;p=pixelpark%2Fpp-admin-tools.git Generating totals yaml file. --- diff --git a/.gitignore b/.gitignore index bf86b61..d00cfa6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ dist/ tmp/* venv/* etc/*.ini +get-from-addr-totals.yaml diff --git a/lib/pp_admintools/app/get_from_addr.py b/lib/pp_admintools/app/get_from_addr.py index 42f64b6..eab96be 100644 --- a/lib/pp_admintools/app/get_from_addr.py +++ b/lib/pp_admintools/app/get_from_addr.py @@ -18,14 +18,17 @@ from pathlib import Path from fb_tools.common import pp from fb_tools.mailaddress import MailAddress +import yaml + # Own modules from . import BaseDPXApplication from ..argparse_actions import NonNegativeIntegerOptionAction from ..argparse_actions import OutputFileOptionAction +from ..errors import DpxFileError from ..handler.pflogparse import PostfixLogfileParser from ..xlate import XLATOR -__version__ = '0.7.0' +__version__ = '0.8.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -51,6 +54,11 @@ class GetFromAddressesApp(BaseDPXApplication): re_mail_domain = re.compile(r'.*@') + open_args = { + 'encoding': 'utf-8', + 'errors': 'surrogateescape', + } + local_networks = [ ipaddress.ip_network('10.0.0.0/8'), ipaddress.ip_network('172.16.0.0/12'), @@ -394,6 +402,8 @@ class GetFromAddressesApp(BaseDPXApplication): self.empty_line() LOG.debug(_('Results per address:') + '\n' + pp(self.per_address)) + self._generate_totals() + if self.oldest_entry or self.newest_entry: self.empty_line() if self.oldest_entry: @@ -409,6 +419,59 @@ class GetFromAddressesApp(BaseDPXApplication): self._output_results_per_address() self._output_results_per_domain() + # ------------------------------------------------------------------------- + def _generate_totals(self): + + totals_file_abs = self.totals_yaml_file.resolve() + if self.verbose > 0: + LOG.debug(_('Using totals YAML file {!r}.').format(str(totals_file_abs))) + + totals = self._read_totals(totals_file_abs) + + for loghost in self.per_address.keys(): + if loghost not in totals: + totals[loghost] = {} + + totals[loghost]['per_address'] = {} + for address in self.per_address[loghost].keys(): + count = self.per_address[loghost][address] + totals[loghost]['per_address'][address] = count + + for loghost in self.per_address.keys(): + if loghost not in totals: + totals[loghost] = {} + + totals[loghost]['per_domain'] = {} + for domain in self.per_domain[loghost].keys(): + count = self.per_domain[loghost][domain] + totals[loghost]['per_domain'][domain] = count + + if self.verbose > 0: + self.empty_line() + LOG.debug(_('Results total:') + '\n' + pp(totals)) + + LOG.debug(_('Writing file {!r} ...').format(str(totals_file_abs))) + with totals_file_abs.open('wt', **self.open_args) as fh: + yaml.safe_dump(totals, fh, explicit_start=True) + + # ------------------------------------------------------------------------- + def _read_totals(self, totals_file_abs): + + totals = {} + try: + self.check_output_file(totals_file_abs) + except DpxFileError as e: + LOG.error(str(e)) + self.exit(3) + + if totals_file_abs.exists(): + if self.verbose > 0: + LOG.debug(_('Reading file {!r} ...').format(str(totals_file_abs))) + with totals_file_abs.open('rt', **self.open_args) as fh: + totals = yaml.safe_load(fh) + + return totals + # ------------------------------------------------------------------------- def _output_results_per_address(self): @@ -421,6 +484,10 @@ class GetFromAddressesApp(BaseDPXApplication): print(self.colored(title, 'CYAN')) self.line(width=len(title), linechar='-', color='CYAN') + if not self.per_address: + print('None.') + return + for loghost in self.per_address.keys(): if len(loghost) > max_len_loghost: @@ -469,6 +536,10 @@ class GetFromAddressesApp(BaseDPXApplication): print(self.colored(title, 'CYAN')) self.line(width=len(title), linechar='-', color='CYAN') + if not self.per_domain: + print('None.') + return + for loghost in self.per_domain.keys(): if len(loghost) > max_len_loghost: