File indexing completed on 2024-04-21 05:44:53

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Remove selected manual comments in fuzzy messages.
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.report import report
0016 
0017 
0018 def setup_sieve (p):
0019 
0020     p.set_desc(_("@info sieve discription",
0021     "Remove selected manual comments from fuzzy messages."
0022     ))
0023 
0024     p.add_param("all", bool, defval=False,
0025                 desc=_("@info sieve parameter discription",
0026     "Remove all manual comments."
0027     ))
0028     p.add_param("nopipe", bool, defval=False,
0029                 desc=_("@info sieve parameter discription",
0030     "Remove embedded lists of no-pipe flags (# |, foo, ...)."
0031     ))
0032     p.add_param("pattern", str,
0033                 metavar=_("@info sieve parameter value placeholder", "REGEX"),
0034                 desc=_("@info sieve parameter discription",
0035     "Remove comments matching the regular expression."
0036     ))
0037     p.add_param("exclude", str,
0038                 metavar=_("@info sieve parameter value placeholder", "REGEX"),
0039                 desc=_("@info sieve parameter discription",
0040     "Remove comments not matching the regular expression."
0041     ))
0042     p.add_param("case", bool, defval=False,
0043                 desc=_("@info sieve parameter discription",
0044     "Case-sensitive pattern matching."
0045     ))
0046 
0047 
0048 
0049 class Sieve (object):
0050 
0051     def __init__ (self, params):
0052 
0053         self.sel_all = params.all
0054         self.sel_nopipe = params.nopipe
0055 
0056         self.rxflags = re.U
0057         if not params.case:
0058             self.rxflags |= re.I
0059 
0060         self.pattern = None
0061         if params.pattern:
0062             self.pattern = params.pattern
0063             self.pattern_rx = re.compile(self.pattern, self.rxflags)
0064 
0065         self.exclude = None
0066         if params.exclude:
0067             self.exclude = params.exclude
0068             self.exclude_rx = re.compile(self.exclude, self.rxflags)
0069 
0070         # Regex for matching no-pipe flag lists.
0071         self.nopipe_rx = re.compile(r"^\s*\|,")
0072 
0073         # Number of modified messages.
0074         self.nmod = 0
0075 
0076 
0077     def process (self, msg, cat):
0078 
0079         # Process comments only for fuzzy messages.
0080         if not msg.fuzzy:
0081             return
0082 
0083         modcount = msg.modcount
0084 
0085         # Go through manual comments.
0086         i = 0
0087         while i < len(msg.manual_comment):
0088             selected = False
0089             cmnt = msg.manual_comment[i]
0090 
0091             # Specific selections.
0092             if not selected and self.sel_all:
0093                 selected = True
0094 
0095             if not selected and self.sel_nopipe:
0096                 selected = self.nopipe_rx.search(cmnt) is not None
0097 
0098             # Inclusion pattern.
0099             if not selected and self.pattern is not None:
0100                 selected = self.pattern_rx.search(cmnt) is not None
0101 
0102             # Exclusion pattern.
0103             if selected and self.exclude is not None:
0104                 selected = self.exclude_rx.search(cmnt) is None
0105 
0106             # Apply selection.
0107             if selected:
0108                 msg.manual_comment.pop(i)
0109             else:
0110                 i += 1
0111 
0112         if msg.modcount > modcount:
0113             self.nmod += 1
0114 
0115 
0116     def finalize (self):
0117 
0118         if self.nmod > 0:
0119             msg = n_("@info:progress",
0120                      "Removed some comments from %(num)d fuzzy message.",
0121                      "Removed some comments from %(num)d fuzzy messages.",
0122                      num=self.nmod)
0123             report("===== " + msg)
0124