File indexing completed on 2024-04-14 05:37:56

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Unfuzzy those messages fuzzied only due to a context change.
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.msgreport import report_msg_content
0014 from pology.msgreport import report_msg_to_lokalize
0015 from pology.report import report
0016 from pology.sieve import add_param_poeditors
0017 
0018 
0019 def setup_sieve (p):
0020 
0021     p.set_desc(_("@info sieve discription",
0022     "Unfuzzy messages which got fuzzy only due to changed context."
0023     "\n\n"
0024     "Possible only if catalogs were merged with --previous option."
0025     "\n\n"
0026     "By default, unfuzzied messages will get a translator comment with "
0027     "the string '%(str)s', so that they can be reviewed later.",
0028     str="unreviewed-context"
0029     ))
0030 
0031     p.add_param("noreview", bool, defval=False,
0032                 desc=_("@info sieve parameter discription",
0033     "Do not add translator comment indicating unreviewed context."
0034     ))
0035     p.add_param("eqmsgid", bool, defval=False,
0036                 desc=_("@info sieve parameter discription",
0037     "Do not unfuzzy messages which have same msgid as another message, "
0038     "and report them together with all other messages with the same msgid."
0039     ))
0040     add_param_poeditors(p)
0041 
0042 
0043 class Sieve (object):
0044 
0045     def __init__ (self, params):
0046 
0047         self.p = params
0048 
0049         self.nunfuzz = 0
0050         self.nrep = 0
0051 
0052 
0053     def process_header (self, hdr, cat):
0054 
0055         self.msgs_by_msgid = {}
0056         self.msgs_to_unfuzzy_by_msgid = {}
0057 
0058 
0059     def process (self, msg, cat):
0060 
0061         if msg.obsolete:
0062             return
0063 
0064         if msg.msgid not in self.msgs_by_msgid:
0065             self.msgs_by_msgid[msg.msgid] = []
0066         self.msgs_by_msgid[msg.msgid].append(msg)
0067 
0068         if (    msg.fuzzy
0069             and msg.msgid == msg.msgid_previous
0070             and msg.msgid_plural == msg.msgid_plural_previous
0071         ):
0072             if msg.msgid not in self.msgs_to_unfuzzy_by_msgid:
0073                 self.msgs_to_unfuzzy_by_msgid[msg.msgid] = []
0074             self.msgs_to_unfuzzy_by_msgid[msg.msgid].append(msg)
0075 
0076 
0077     def process_header_last (self, hdr, cat):
0078 
0079         msgs_to_report = []
0080         keys_of_msgs_to_report = set()
0081         if self.p.eqmsgid:
0082             for msg in cat:
0083                 if msg.obsolete:
0084                     continue
0085                 msgs = self.msgs_by_msgid.get(msg.msgid)
0086                 msgs_to_unfuzzy = self.msgs_to_unfuzzy_by_msgid.get(msg.msgid)
0087                 if msgs and msgs_to_unfuzzy and len(msgs) > 1:
0088                     msgs_to_report.append(msg)
0089                     keys_of_msgs_to_report.add(msg.key)
0090 
0091         for msgs in list(self.msgs_to_unfuzzy_by_msgid.values()):
0092             for msg in msgs:
0093                 if msg.key not in keys_of_msgs_to_report:
0094                     msg.unfuzzy()
0095                     self.nunfuzz += 1
0096 
0097         for msg in msgs_to_report:
0098             if self.p.lokalize:
0099                 report_msg_to_lokalize(msg, cat)
0100             else:
0101                 report_msg_content(msg, cat, delim="-" * 20)
0102             self.nrep += 1
0103 
0104 
0105     def finalize (self):
0106 
0107         if self.nunfuzz > 0:
0108             msg = n_("@info:progress",
0109                      "Unfuzzied %(num)d message fuzzy due to "
0110                      "difference in context only.",
0111                      "Unfuzzied %(num)d messages fuzzy due to "
0112                      "difference in context only.",
0113                      num=self.nunfuzz)
0114             report("===== " + msg)
0115         if self.nrep > 0:
0116             msg = n_("@info:progress",
0117                      "Reported %(num)d message due to equality "
0118                      "of '%(field)s' field.",
0119                      "Reported %(num)d messages due to equality "
0120                      "of '%(field)s' field.",
0121                      num=self.nrep, field="msgid")
0122             report("===== " + msg)
0123