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

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Convert separator-embedded context to Gettext context.
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.escape import unescape_c as unescape
0014 from pology.msgreport import warning_on_msg
0015 from pology.report import report
0016 from pology.sieve import SieveError
0017 
0018 
0019 def setup_sieve (p):
0020 
0021     p.set_desc(_("@info sieve discription",
0022     "Convert separator-embedded context to Gettext context."
0023     ))
0024 
0025     p.add_param("sep", str, mandatory=True,
0026                 metavar=_("@info sieve parameter value placeholder", "STRING"),
0027                 desc=_("@info sieve parameter discription",
0028     "Separator between the context and the text in msgid field."
0029     ))
0030 
0031 
0032 class Sieve (object):
0033 
0034     def __init__ (self, params):
0035 
0036         self.nconv = 0
0037 
0038         self.csep = unescape(params.sep)
0039         if not self.csep:
0040             raise SieveError(
0041                 _("@info", "Context separator cannot be empty string."))
0042 
0043 
0044     def process (self, msg, cat):
0045 
0046         # Skip messages already having Gettext context.
0047         if msg.msgctxt or msg.msgctxt_previous:
0048             return
0049 
0050         pos = msg.msgid.find(self.csep)
0051         if pos >= 0:
0052             if msg.msgid.find(self.csep, pos + len(self.csep)) >= 0:
0053                 # If more than one delimiter, probably not context.
0054                 return
0055             ctxt = msg.msgid[:pos]
0056             text = msg.msgid[pos + len(self.csep):]
0057             if not ctxt or not text:
0058                 # Something is strange, skip.
0059                 return
0060             exmsgs = cat.select_by_key(ctxt, text, wobs=True)
0061             if exmsgs:
0062                 exmsg = exmsgs[0]
0063                 if not msg.obsolete and exmsg.obsolete:
0064                     cat.remove_on_sync(exmsg)
0065                 elif msg.obsolete and not exmsg.obsolete:
0066                     cat.remove_on_sync(msg)
0067                     return
0068                 else:
0069                     return
0070             msg.msgctxt = ctxt
0071             msg.msgid = text
0072             self.nconv += 1
0073 
0074 
0075     def finalize (self):
0076 
0077         if self.nconv > 0:
0078             msg = n_("@info:progress",
0079                      "Converted %(num)d separator-embedded context.",
0080                      "Converted %(num)d separator-embedded contexts.",
0081                      num=self.nconv)
0082             report("===== " + msg)
0083