# Standard modules
import ipaddress
import logging
+import re
from pathlib import Path
# Third party modules
from ..handler.pflogparse import PostfixLogfileParser
from ..xlate import XLATOR
-__version__ = '0.2.1'
+__version__ = '0.2.2'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
self.show_address_results = True
if self.args.logfiles:
- for lfile in self.args.logfiles:
- logfile = Path(lfile)
- if logfile.exists():
- if logfile.is_file():
- if logfile not in self.logfiles:
- self.logfiles.append(logfile)
- else:
- LOG.warn(_('File {!r} is not a regular file.').format(lfile))
- else:
- log_dir = logfile.parent
- if not log_dir.exists():
- LOG.warn(_('Directory {!r} does not exists.').format(str(log_dir)))
- elif not log_dir.is_dir():
- LOG.warn(_('Path {!r} is not a directory.').format(str(log_dir)))
- else:
- found_files = log_dir.glob(logfile.name)
- for f in found_files:
- if f.is_file():
- if f not in self.logfiles:
- self.logfiles.append(f)
- else:
- LOG.warn(_('File {!r} is not a regular file.').format(str(f)))
-
+ self._eval_given_logfiles()
else:
- found_files = self.logdir.glob(self.logfile_pattern)
- for f in found_files:
- if f.is_file():
- if f not in self.logfiles:
- self.logfiles.append(f)
+ self._eval_default_logfiles()
+ self._sort_sogfiles()
+
+ # -------------------------------------------------------------------------
+ def _eval_given_logfiles(self):
+
+ for lfile in self.args.logfiles:
+ logfile = Path(lfile)
+ if logfile.exists():
+ if logfile.is_file():
+ if logfile not in self.logfiles:
+ self.logfiles.append(logfile)
else:
- LOG.warn(_('File {!r} is not a regular file.').format(str(f)))
+ LOG.warn(_('File {!r} is not a regular file.').format(lfile))
+ else:
+ log_dir = logfile.parent
+ if not log_dir.exists():
+ LOG.warn(_('Directory {!r} does not exists.').format(str(log_dir)))
+ elif not log_dir.is_dir():
+ LOG.warn(_('Path {!r} is not a directory.').format(str(log_dir)))
+ else:
+ found_files = log_dir.glob(logfile.name)
+ for f in found_files:
+ if f.is_file():
+ if f not in self.logfiles:
+ self.logfiles.append(f)
+ else:
+ LOG.warn(_('File {!r} is not a regular file.').format(str(f)))
+
+ # -------------------------------------------------------------------------
+ def _eval_default_logfiles(self):
+
+ found_files = self.logdir.glob(self.logfile_pattern)
+ for f in found_files:
+ if f.is_file():
+ if f not in self.logfiles:
+ self.logfiles.append(f)
+ else:
+ LOG.warn(_('File {!r} is not a regular file.').format(str(f)))
+
+ # -------------------------------------------------------------------------
+ def _sort_sogfiles(self):
+
+ if self.verbose > 1:
+ LOG.debug(_('Sorting logfiles ...'))
+ sorted_logfiles = []
+ logfiles_left = []
+
+ re_dateext = re.compile(r'log-20', re.IGNORECASE)
+
+ for logfile in self.logfiles:
+ if re_dateext.search(logfile.name):
+ sorted_logfiles.append(str(logfile))
+ else:
+ logfiles_left.append(logfile)
+
+ sorted_logfiles.sort(key=str.lower)
+ self.logfiles = []
+ for logfile in sorted_logfiles:
+ self.logfiles.append(Path(logfile))
+
+ for logfile in logfiles_left:
+ self.logfiles.append(logfile)
# -------------------------------------------------------------------------
def _run(self):