File indexing completed on 2024-09-08 05:10:00
0001 # -*- coding: UTF-8 -*- 0002 0003 """ 0004 Resolve alternative directives in 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 0013 import os 0014 import re 0015 import sys 0016 0017 from pology import _, n_ 0018 from pology.msgreport import warning_on_msg 0019 from pology.report import report 0020 from pology.resolve import resolve_alternatives 0021 from pology.sieve import SieveError 0022 0023 0024 def setup_sieve (p): 0025 0026 p.set_desc(_("@info sieve discription", 0027 "Resolve alternative directives in translation." 0028 )) 0029 0030 p.add_param("alt", str, mandatory=True, 0031 metavar=_("@info sieve parameter value placeholder", "N,Mt"), 0032 desc=_("@info sieve parameter discription", 0033 "N is index (1-based) of the alternative to take from each directive, " 0034 "and M the number of alternatives per directive. Example:\n" 0035 "\n" 0036 "alt:1,2t" 0037 )) 0038 0039 0040 class Sieve (object): 0041 0042 def __init__ (self, params): 0043 0044 self.total = None 0045 self.select = None 0046 try: 0047 for spec in params.alt.split(","): 0048 if spec.endswith("t"): 0049 self.total = int(spec[:-1]) 0050 else: 0051 self.select = int(spec) 0052 except: 0053 raise SieveError( 0054 _("@info", 0055 "Malformed specification for " 0056 "resolution of alternatives '%(spec)s'.", 0057 spec=params.alt)) 0058 if self.total is None: 0059 raise SieveError( 0060 _("@info", 0061 "Number of alternatives per directive not given.")) 0062 if self.select is None: 0063 raise SieveError( 0064 _("@info", 0065 "Index of selected alternative not given.")) 0066 if self.total < 1: 0067 raise SieveError( 0068 _("@info", 0069 "Number of alternatives specified as %(num)d, " 0070 "but must be greater than 1.", 0071 num=self.total)) 0072 if self.select < 1 or self.select > self.total: 0073 raise SieveError( 0074 _("@info", 0075 "Selected alternative no. %(ord)d is out of range.", 0076 ord=self.select)) 0077 0078 self.nresolved = 0 0079 0080 0081 def process (self, msg, cat): 0082 0083 for i in range(len(msg.msgstr)): 0084 msg.msgstr[i], nresolved, valid = \ 0085 resolve_alternatives(msg.msgstr[i], self.select, self.total, 0086 srcname=cat.filename) 0087 if valid: 0088 self.nresolved += nresolved 0089 else: 0090 warning_on_msg(_("@info", 0091 "Invalid alternatives directive " 0092 "in translation."), msg, cat) 0093 0094 0095 def finalize (self): 0096 0097 if self.nresolved > 0: 0098 msg = n_("@info:progress", 0099 "Resolved %(num)d alternative in translation.", 0100 "Resolved %(num)d alternatives in translation.", 0101 num=self.nresolved) 0102 report("===== " + msg) 0103