File indexing completed on 2024-04-21 05:44:53

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Unfuzzy messages fuzzied only due to a change in UI context marker.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0009 @license: GPLv3
0010 """
0011 
0012 import re
0013 
0014 from pology import _, n_
0015 from pology.report import report
0016 
0017 
0018 def setup_sieve (p):
0019 
0020     p.set_desc(_("@info sieve discription",
0021     "Unfuzzy messages which got fuzzy only due to changed context marker."
0022     "\n\n"
0023     "Possible only if catalogs were merged with --previous option."
0024     "\n\n"
0025     "By default, unfuzzied messages will get a translator comment with "
0026     "the string '%(str)s', so that they can be reviewed later.",
0027     str="unreviewed-context"
0028     ))
0029 
0030     p.add_param("noreview", bool, defval=False,
0031                 desc=_("@info sieve parameter discription",
0032     "Do not add translator comment indicating unreviewed context."
0033     ))
0034 
0035 
0036 _strip_rx = re.compile(r"^\s*@[^\s]+(.*)", re.U)
0037 _norm_rx = re.compile(r"[^\w]", re.U)
0038 
0039 # Strip the KUIT context marker, and normalize rest of the string.
0040 def _stripped (ctxt):
0041     m = _strip_rx.search(ctxt)
0042     if m: stripped = m.group(1)
0043     else: stripped = ctxt
0044     return _norm_rx.sub("", stripped.lower())
0045 
0046 
0047 class Sieve (object):
0048 
0049     def __init__ (self, params):
0050 
0051         self.flag_review = not params.noreview
0052 
0053         self.nmatch = 0
0054 
0055 
0056     def process (self, msg, cat):
0057 
0058         if (    msg.fuzzy
0059             and msg.msgid == msg.msgid_previous
0060             and msg.msgid_plural == msg.msgid_plural_previous
0061             and (   _stripped(msg.msgctxt or "")
0062                  == _stripped(msg.msgctxt_previous or ""))
0063         ):
0064             msg.unfuzzy()
0065             if self.flag_review:
0066                 # Add as manual comment, as any other type will vanish
0067                 # when catalog is merged with template.
0068                 msg.manual_comment.append("unreviewed-context")
0069             self.nmatch += 1
0070 
0071 
0072     def finalize (self):
0073 
0074         if self.nmatch > 0:
0075             msg = n_("@info:progress",
0076                      "Unfuzzied %(num)d message fuzzy due to "
0077                      "difference in context marker only.",
0078                      "Unfuzzied %(num)d messages fuzzy due to "
0079                      "difference in context marker only.",
0080                      num=self.nmatch)
0081             report("===== " + msg)
0082