File indexing completed on 2024-03-24 17:21:45

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Apply hooks to headers.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0009 @license: GPLv3
0010 """
0011 
0012 from pology import _, n_
0013 from pology.getfunc import get_hook_ireq
0014 from pology.report import report, warning
0015 from pology.sieve import add_param_filter
0016 from pology.sieve import SieveError
0017 
0018 
0019 def setup_sieve (p):
0020 
0021     p.set_desc(_("@info sieve discription",
0022     "Apply hooks to header."
0023     "\n\n"
0024     "Catalog header is passed through one or more of "
0025     "F4B, V4B, S4B hooks. "
0026     ))
0027 
0028     add_param_filter(p, _("@info sieve parameter discription",
0029     "Specification of the hook through which headers are passed."
0030     ))
0031 
0032 
0033 class Sieve (object):
0034 
0035     def __init__ (self, params):
0036 
0037         self.tfilters = [[get_hook_ireq(x, abort=True), x]
0038                           for x in (params.filter or [])]
0039 
0040         # Number of modified headers.
0041         self.nmod = 0
0042 
0043 
0044     def process_header (self, hdr, cat):
0045 
0046         mcount = hdr.modcount
0047 
0048         for tfilter, tfname in self.tfilters:
0049             try:
0050                 res = tfilter(hdr, cat)
0051             except TypeError:
0052                 raise SieveError(
0053                     _("@info",
0054                       "Cannot execute filter '%(filt)s'.",
0055                       filt=tfname))
0056 
0057             # Process result based on hook type.
0058             if isinstance(res, list):
0059                 # Validation hook.
0060                 # TODO: Better span reporting on headers.
0061                 for part in res:
0062                     for span in part[2]:
0063                         if len(span) >= 3:
0064                             errmsg = span[2]
0065                             report("%s:header: %s", (cat.filename, errmsg))
0066             else:
0067                 # Side-effect hook, nothing to do.
0068                 # TODO: Perhaps report returned number?
0069                 pass
0070 
0071         if mcount < hdr.modcount:
0072             self.nmod += 1
0073 
0074 
0075     def finalize (self):
0076 
0077         if self.nmod:
0078             msg = n_("@info:progress",
0079                      "Modified %(num)d header by filtering.",
0080                      "Modified %(num)d headers by filtering.",
0081                      num=self.nmod)
0082             report("===== " + msg)
0083