File indexing completed on 2024-11-03 11:24:06
0001 # -*- coding: UTF-8 -*- 0002 0003 """ 0004 Embed differences in original text in fuzzy messages into previous fields. 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.comments import parse_summit_branches 0016 from pology.diff import word_ediff, word_ediff_to_old 0017 from pology.report import report 0018 0019 0020 def setup_sieve (p): 0021 0022 p.set_desc(_("@info sieve discription", 0023 "Diff previous to current fields in fuzzy messages." 0024 )) 0025 0026 p.add_param("strip", bool, 0027 desc=_("@info sieve parameter discription", 0028 "Remove embedded differences from previous fields." 0029 )) 0030 0031 p.add_param("branch", str, seplist=True, 0032 metavar=_("@info sieve parameter value placeholder", "BRANCH"), 0033 desc=_("@info sieve parameter discription", 0034 "In summit catalogs, process only messages belonging to given branch. " 0035 "Several branches can be given as comma-separated list." 0036 )) 0037 0038 0039 class Sieve (object): 0040 0041 def __init__ (self, params): 0042 0043 self.nmod = 0 0044 0045 self.strip = params.strip 0046 self.branches = set(params.branch or []) 0047 0048 0049 def _diff (self, msgold, msgnew, format): 0050 0051 # Remove any previous diff. 0052 msgold_clean = word_ediff_to_old(msgold) 0053 0054 # Create the diff or only put back the clean text. 0055 if not self.strip: 0056 return word_ediff(msgold_clean, msgnew, 0057 markup=True, format=format) 0058 else: 0059 return msgold_clean 0060 0061 0062 def process (self, msg, cat): 0063 0064 # Summit: if branches were given, skip the message if it does not 0065 # belong to any of the given branches. 0066 if self.branches: 0067 msg_branches = parse_summit_branches(msg) 0068 if not set.intersection(self.branches, msg_branches): 0069 return 0070 0071 # Skip if message is not fuzzy or does not have previous fields. 0072 if not msg.fuzzy or msg.msgid_previous is None: 0073 # Remove any stray previous fields. 0074 msg.msgctxt_previous = None 0075 msg.msgid_previous = None 0076 msg.msgid_plural_previous = None 0077 return 0078 0079 # Skip message if obsolete fuzzy. 0080 if msg.obsolete: 0081 return 0082 0083 oldcount = msg.modcount 0084 0085 msg.msgctxt_previous = self._diff(msg.msgctxt_previous, msg.msgctxt, 0086 msg.format) 0087 msg.msgid_previous = self._diff(msg.msgid_previous, msg.msgid, 0088 msg.format) 0089 msg.msgid_plural_previous = self._diff(msg.msgid_plural_previous, 0090 msg.msgid_plural, msg.format) 0091 0092 if msg.modcount > oldcount: 0093 self.nmod += 1 0094 0095 0096 def finalize (self): 0097 0098 if self.nmod > 0: 0099 if not self.strip: 0100 msg = n_("@info:progress", 0101 "Added differences to %(num)d fuzzy message.", 0102 "Added differences to %(num)d fuzzy messages.", 0103 num=self.nmod) 0104 else: 0105 msg = n_("@info:progress", 0106 "Stripped differences from %(num)d fuzzy message.", 0107 "Stripped differences from %(num)d fuzzy messages.", 0108 num=self.nmod) 0109 report("===== " + msg) 0110