]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Generating totals yaml file.
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 15:27:44 +0000 (17:27 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 15:27:44 +0000 (17:27 +0200)
.gitignore
lib/pp_admintools/app/get_from_addr.py

index bf86b61a1cbbeb9013a3425406c5f955b732e16d..d00cfa6ae0861c91956501a7e76e6cc1822244c2 100644 (file)
@@ -22,3 +22,4 @@ dist/
 tmp/*
 venv/*
 etc/*.ini
+get-from-addr-totals.yaml
index 42f64b64c4bfe443bafe4567848f54a6e9f76d48..eab96be1c358285167c5820ac129da35ddb1b752 100644 (file)
@@ -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: