File indexing completed on 2024-11-03 05:12:58

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Apply hooks to translation.
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.msgreport import report_msg_content
0015 from pology.report import report, warning, error
0016 from pology.sieve import add_param_filter
0017 from pology.sieve import SieveError
0018 
0019 
0020 def setup_sieve (p):
0021 
0022     p.set_desc(_("@info sieve discription",
0023     "Apply hooks to translation."
0024     "\n\n"
0025     "Message msgstr fields are passed through one or more of "
0026     "F1A, F3A/C, V1A, V3A/C, S1A, S3A/C hooks."
0027     ))
0028 
0029     add_param_filter(p, _("@info sieve parameter discription",
0030     "Specification of the hook through which msgstr fields are passed."
0031     ))
0032     p.add_param("showmsg", bool, defval=False,
0033                 desc=_("@info sieve parameter discription",
0034     "Report message to standard output if it got modified."
0035     ))
0036 
0037 
0038 class Sieve (object):
0039 
0040     def __init__ (self, params):
0041 
0042         self.p = params
0043 
0044         self.tfilters = [[get_hook_ireq(x, abort=True), x]
0045                          for x in (params.filter or [])]
0046 
0047         # Number of modified messages.
0048         self.nmod = 0
0049 
0050 
0051     def process (self, msg, cat):
0052 
0053         mcount = msg.modcount
0054 
0055         for i in range(len(msg.msgstr)):
0056             for tfilter, tfname in self.tfilters:
0057                 try: # try as type *1A hook
0058                     res = tfilter(msg.msgstr[i])
0059                 except TypeError:
0060                     try: # try as type *3* hook
0061                         res = tfilter(msg.msgstr[i], msg, cat)
0062                     except TypeError:
0063                         raise SieveError(
0064                             _("@info",
0065                               "Cannot execute filter '%(filt)s'.",
0066                               filt=tfname))
0067 
0068                 # Process result based on hook type.
0069                 if isinstance(res, str):
0070                     # Modification hook.
0071                     msg.msgstr[i] = res
0072                 elif isinstance(res, list):
0073                     # Validation hook.
0074                     if res:
0075                         report_msg_content(msg, cat,
0076                                            highlight=[("msgstr", i, res)],
0077                                            delim=("-" * 20))
0078                 else:
0079                     # Side-effect hook, nothing to do.
0080                     # TODO: Perhaps report returned number?
0081                     pass
0082 
0083         if mcount < msg.modcount:
0084             self.nmod += 1
0085             if self.p.showmsg:
0086                 report_msg_content(msg, cat, delim=("-" * 20))
0087 
0088 
0089     def finalize (self):
0090 
0091         if self.nmod:
0092             msg = n_("@info:progress",
0093                      "Modified %(num)d message by filtering.",
0094                      "Modified %(num)d messages by filtering.",
0095                      num=self.nmod)
0096             report("===== " + msg)
0097