File indexing completed on 2024-11-03 11:24:06

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Merge translation corrections from (partial) PO files tree into main tree.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0009 @author: Goran Rakic (Горан Ракић) <grakic@devbase.net>
0010 @license: GPLv3
0011 """
0012 
0013 import os
0014 
0015 from pology import _, n_
0016 from pology.catalog import Catalog
0017 from pology.header import Header
0018 from pology.report import report
0019 from pology.sieve import SieveError
0020 
0021 
0022 def setup_sieve (p):
0023 
0024     p.set_desc(_("@info sieve discription",
0025     "Merge translation corrections from partial PO files tree into main tree."
0026     "\n\n"
0027     "Give main PO files tree as input and provide the path difference to "
0028     "where the partial correction tree is available."
0029     ))
0030 
0031     p.add_param("pathdelta", str, mandatory=True,
0032                 metavar=_("@info sieve parameter value placeholder",
0033                           "FIND[:REPLACE]"),
0034                 desc=_("@info sieve parameter discription",
0035     "Specify that partial tree is available at path obtained when "
0036     "first FIND in the input path is replaced with REPLACE. "
0037     "If REPLACE is not given, FIND is just removed. "
0038     "Example:"
0039     "\n\n"
0040     "pathdelta:ui:ui-check"
0041     ))
0042 
0043 
0044 class Sieve (object):
0045 
0046     def __init__ (self, params):
0047 
0048         self.ncorr = 0
0049 
0050         pathdelta = params.pathdelta
0051         if ":" not in pathdelta:
0052             self.pd_srch = pathdelta
0053             self.pd_repl = ""
0054         else:
0055             self.pd_srch, self.pd_repl = pathdelta.split(":", 1)
0056 
0057 
0058     def process_header (self, hdr, cat):
0059 
0060         # Cancel prior correction catalog.
0061         self.corr_cat = None
0062 
0063         # Construct expected path to correction catalog.
0064         corr_path = cat.filename.replace(self.pd_srch, self.pd_repl, 1)
0065 
0066         # Open the catalog if it exists.
0067         if os.path.isfile(corr_path):
0068             self.corr_cat = Catalog(corr_path)
0069 
0070 
0071     def process (self, msg, cat):
0072 
0073         if not self.corr_cat: # No correction catalog for this one, skip
0074             return
0075 
0076         if msg in self.corr_cat:
0077 
0078             corr_msg = self.corr_cat[msg]
0079 
0080             oldcount = msg.modcount
0081 
0082             # Need to take over manual comments too (the translator may have
0083             # made some upon correction), but without those added by pofilter.
0084             corr_manual_comment = []
0085             for cmnt in corr_msg.manual_comment:
0086                 if "(pofilter)" not in cmnt:
0087                     corr_manual_comment.append(cmnt)
0088 
0089             # Take over all extraction-invariant parts
0090             # and set cleaned up comments.
0091             msg.set_inv(corr_msg)
0092             msg.manual_comment[:] = corr_manual_comment
0093 
0094             if msg.modcount > oldcount:
0095                 self.ncorr += 1
0096 
0097 
0098     def finalize (self):
0099 
0100         if self.ncorr > 0:
0101             msg = n_("@info:progress",
0102                      "Merged %(num)d corrected message.",
0103                      "Merged %(num)d corrected messages.",
0104                      num=self.ncorr)
0105             report("===== " + msg)
0106