コード例 #1
0
ファイル: fancy_quote.py プロジェクト: KDE/pology
    def process(self, msg, cat):

        # Skip the message when told so.
        if _flag_no_fancy_quote in manc_parse_flag_list(msg, "|"):
            return

        # Skip the message if special by context (one of meta-messages).
        if _spec_msgctxt_rx.search(msg.msgctxt or ""):
            return

        # Skip the message if auto comments identify it as literal user input.
        for cmnt in msg.auto_comment:
            cmnt = cmnt.lower()
            # - extracted by KDE's xml2pot
            if "tag:" in cmnt:
                tag = cmnt[cmnt.find(":") + 1:].strip()
                if tag in _xml_literal_tags:
                    return

        # Modify quotes in all translations.
        for i in range(len(msg.msgstr)):
            text = msg.msgstr[i]
            if self.singles:
                text, nrepl = equip_fancy_quotes(text, "'", self.singles)
                self.nrepl_single += nrepl
            if self.doubles:
                text, nrepl = equip_fancy_quotes(text, '"', self.doubles)
                self.nrepl_double += nrepl
            msg.msgstr[i] = text
コード例 #2
0
ファイル: bpatterns.py プロジェクト: KDE/pology
    def hook(text, msg, cat):
        if _flag_no_bad_patterns in manc_parse_flag_list(msg, "|"):
            return 0

        indspans = _match_patterns(text, patterns_cmp)
        for pind, span in indspans:
            pstr = patterns_str[pind]
            report_on_msg(
                _("@info", "Bad pattern '%(pattern)s' detected.",
                  pattern=pstr), msg, cat)
        return len(indspans)
コード例 #3
0
ファイル: sieve.py プロジェクト: KDE/pology
def parse_sieve_flags (msg):
    """
    Extract sieve flags embedded in manual comments.

    Sieve flags are put into manual comments with the following syntax::

        # |, flag1, flag2, ...

    Some sieves will define certain sieve flags by which their behavior
    can be altered on a particular message.

    @param msg: message to parse
    @type msg: Message

    @returns: parsed flags
    @rtype: set of strings
    """

    return set(manc_parse_flag_list(msg, "|"))
コード例 #4
0
ファイル: bpatterns.py プロジェクト: KDE/pology
    def hook(msg, cat):
        if _flag_no_bad_patterns in manc_parse_flag_list(msg, "|"):
            return 0

        parts = []
        nbad = 0
        for i in range(len(msg.msgstr)):
            indspans = _match_patterns(msg.msgstr[i], patterns_cmp)
            spans = []
            for pind, span in indspans:
                emsg = _("@info",
                         "Bad pattern '%(pattern)s' detected.",
                         pattern=patterns_str[pind])
                spans.append(span + (emsg, ))
                nbad += 1
            if spans:
                parts.append(("msgstr", i, spans))

        if partrep:
            return parts
        else:
            if parts:
                report_msg_content(msg, cat, highlight=parts, delim=("-" * 20))
            return nbad
コード例 #5
0
ファイル: pohybdl.py プロジェクト: KDE/pology
def hybdl(path, path0, accnohyb=False):

    cat = Catalog(path)
    cat0 = Catalog(path0, monitored=False)

    nhybridized = 0
    nstopped = 0
    for msg in cat:

        if "no-hybdl" in manc_parse_flag_list(msg, "|"):
            continue

        # Unembed diff if message was diffed for review.
        # Replace ediff with manual review flag.
        diffed = False
        for flag in msg.flag:
            if flag.startswith("ediff"):
                msg.flag.remove(flag)
                diffed = True
        if diffed:
            msg_ediff_to_new(msg, msg)
            msg.flag.add(u"reviewed")

        # Fetch original message.
        msg0 = cat0.get(msg)
        if msg0 is None:
            warning_on_msg(
                _("@info", "Message does not exist in the original catalog."),
                msg, cat)
            nstopped += 1
            continue
        if len(msg.msgstr) != len(msg0.msgstr):
            warning_on_msg(
                _(
                    "@info", "Number of translations not same as in "
                    "the original message."), msg, cat)
            nstopped += 1
            continue
        if msg.msgstr == msg0.msgstr:
            # No changes, nothing new to hybridize.
            continue

        # Hybridize translation.
        textsh = []
        textshinv = []
        for text0, text in zip(msg0.msgstr, msg.msgstr):
            texth = tohi(text0, text, parthyb=True)
            textsh.append(texth)
            if not accnohyb:
                texthinv = tohi(text, text0, parthyb=True)
                textshinv.append(texthinv)
        if accnohyb or textsh == textshinv:
            for i, texth in zip(range(len(msg.msgstr)), textsh):
                msg.msgstr[i] = texth
            nhybridized += 1
        else:
            nstopped += 1
            msgh = MessageUnsafe(msg)
            msgh.msgstr = textsh
            msghinv = MessageUnsafe(msg)
            msghinv.msgstr = textshinv
            msg_ediff(msghinv, msgh, emsg=msgh, colorize=True)
            report_msg_content(msgh, cat, delim=("-" * 20))

    if nstopped == 0:
        if cat.sync():
            report("! %s (%d)" % (path, nhybridized))
    else:
        warning(
            n_("@info", "%(num)d message in '%(file)s' cannot be "
               "cleanly hybridized.",
               "%(num)d messages in '%(file)s' cannot be "
               "cleanly hybridized.",
               num=nstopped,
               file=path))
        nhybridized = 0

    return nhybridized
コード例 #6
0
ファイル: check_spell.py プロジェクト: KDE/pology
    def process(self, msg, cat):

        if not msg.translated:
            return

        id = 0  # Count msgstr plural forms
        failedSuggs = []  # pairs of wrong words and suggestions

        for msgstr in msg.msgstr:
            # Skip message with context in the ignoredContext list
            skip = False
            for context in self.ignoredContext:
                if context in (msg.msgctxt or u"").lower():
                    skip = True
                    break
                for comment in msg.auto_comment:
                    if context in comment.lower():
                        skip = True
                        break
                if skip:
                    break
            if skip:
                break

            # Skip message if explicitly requested.
            if flag_no_check_spell in manc_parse_flag_list(msg, "|"):
                continue

            # Apply precheck filters.
            for pfilter, pfname in self.pfilters:
                try:  # try as type F1A hook
                    msgstr = pfilter(msgstr)
                except TypeError:
                    try:  # try as type F3* hook
                        msgstr = pfilter(msgstr, msg, cat)
                    except TypeError:
                        raise SieveError(
                            _("@info",
                              "Cannot execute filter '%(filt)s'.",
                              filt=pfname))

            # Split text into words.
            if not self.simsp:
                words = proper_words(msgstr, True, cat.accelerator(),
                                     msg.format)
            else:
                # NOTE: Temporary, remove when proper_words becomes smarter.
                words = msgstr.split()

            # Eliminate from checking words matching the skip regex.
            if self.skipRx:
                words = [x for x in words if not self.skipRx.search(x)]

            # Eliminate from checking words explicitly listed as good.
            locally_ignored = manc_parse_list(msg, elist_well_spelled, ",")
            words = [x for x in words if x not in locally_ignored]

            for word in words:
                # Encode word for Aspell.
                encodedWord = word.encode(self.encoding)
                spell = self.aspell.check(encodedWord)
                if spell is False:
                    try:
                        self.nmatch += 1
                        if self.unknownWords is not None:
                            self.unknownWords.add(word)
                        else:
                            encodedSuggestions = self.aspell.suggest(
                                encodedWord)
                            suggestions = [
                                i.decode(self.encoding)
                                for i in encodedSuggestions
                            ]
                            failedSuggs.append((word, suggestions))
                            if self.xmlFile:
                                xmlError = spell_xml_error(
                                    msg, cat, word, suggestions, id)
                                self.xmlFile.writelines(xmlError)
                            else:
                                spell_error(msg, cat, word, suggestions)
                    except UnicodeEncodeError:
                        warning(
                            _("@info", "Cannot encode word '%(word)s' in "
                              "selected encoding '%(enc)s'.",
                              word=word,
                              enc=self.encoding))
            id += 1  # Increase msgstr id count

        if failedSuggs and self.lokalize:
            repls = [_("@label", "Spelling errors:")]
            for word, suggs in failedSuggs:
                if suggs:
                    fmtsuggs = format_item_list(suggs)
                    repls.append(
                        _("@item",
                          "%(word)s (suggestions: %(wordlist)s)",
                          word=word,
                          wordlist=fmtsuggs))
                else:
                    repls.append("%s" % (word))
            report_msg_to_lokalize(msg, cat, cjoin(repls, "\n"))
コード例 #7
0
ファイル: check_spell_ec.py プロジェクト: KDE/pology
    def process(self, msg, cat):

        if not msg.translated:
            return

        failed_w_suggs = []

        for msgstr in msg.msgstr:

            # Skip message if explicitly requested.
            if flag_no_check_spell in manc_parse_flag_list(msg, "|"):
                continue

            # Apply precheck filters.
            for pfilter, pfname in self.pfilters:
                try:  # try as type F1A hook
                    msgstr = pfilter(msgstr)
                except TypeError:
                    try:  # try as type F3* hook
                        msgstr = pfilter(msgstr, msg, cat)
                    except TypeError:
                        raise SieveError(
                            _("@info",
                              "Cannot execute filter '%(filt)s'.",
                              filt=pfname))

            # Split text into words.
            # TODO: See to use markup types somehow.
            words = proper_words(msgstr, True, cat.accelerator(), msg.format)

            # Eliminate from checking words matching the skip regex.
            if self.skip_rx:
                words = [x for x in words if not self.skip_rx.search(x)]

            # Eliminate from checking words explicitly listed as good.
            locally_ignored = manc_parse_list(msg, elist_well_spelled, ",")
            words = [x for x in words if x not in locally_ignored]

            for word in words:
                if not self.checker.check(word):
                    failed = True
                    self.unknown_words.add(word)

                    if not self.words_only or self.lokalize:
                        suggs = self.checker.suggest(word)
                        incmp = False
                        if len(suggs) > 5:  # do not put out too many words
                            suggs = suggs[:5]
                            incmp = True
                        failed_w_suggs.append((word, suggs))

                    if not self.words_only:
                        if suggs:
                            fsuggs = format_item_list(suggs, incmp=incmp)
                            report_on_msg(
                                _("@info", "Unknown word '%(word)s' "
                                  "(suggestions: %(wordlist)s).",
                                  word=word,
                                  wordlist=fsuggs), msg, cat)
                        else:
                            report_on_msg(
                                _("@info",
                                  "Unknown word '%(word)s'.",
                                  word=word), msg, cat)

        if self.lokalize and failed_w_suggs:
            repls = [_("@label", "Spelling errors:")]
            for word, suggs in failed_w_suggs:
                if suggs:
                    fmtsuggs = format_item_list(suggs, incmp=incmp)
                    repls.append(
                        _("@item",
                          "%(word)s (suggestions: %(wordlist)s)",
                          word=word,
                          wordlist=fmtsuggs))
                else:
                    repls.append("%s" % (word))
            report_msg_to_lokalize(msg, cat, cjoin(repls, "\n"))
コード例 #8
0
    def spcheck(text, msg, cat):

        # Check if new spell checker should be constructed.
        if lang is not None:
            clang = lang
        elif cat.language() is not None:
            clang = cat.language()
        elif provider != "aspell-raw":
            clang = enchant_cfg.string("language")
        else:
            clang = None
        if not clang:
            raise PologyError(
                _("@info",
                  "Cannot determine language for catalog '%(file)s'.",
                  file=cat.filename))
        if envs is not None:
            cenvs = envs
        elif cat.environment() is not None:
            cenvs = cat.environment()
        elif provider != "aspell-raw":
            envs_str = enchant_cfg.string("environment")
            cenvs = envs_str.split(",") if envs_str else []
        else:
            cenvs = []
        ckey = (clang, tuple(cenvs))
        if ckey not in checkers:
            if provider != "aspell-raw":
                checkers[ckey] = _construct_enchant(provider, clang, cenvs,
                                                    encoding, variety, suponly)
            else:
                checkers[ckey] = _construct_aspell(clang, cenvs, encoding,
                                                   variety, extopts, suponly)

        checker = checkers[ckey]

        # Prepare shortcut reports.
        if spanrep: defret = []
        else: defret = 0

        # Skip message if explicitly requested.
        if flag_no_check_spell in manc_parse_flag_list(msg, "|"):
            return defret

        # Split text into words and spans: [(word, (start, end)), ...]
        word_spans = wsplit(text, msg, cat)

        # Ignore words explicitly listed as good.
        ignored_words = set(manc_parse_list(msg, elist_well_spelled, ","))
        word_spans = [x for x in word_spans if x[0] not in ignored_words]

        spans = []
        for word, span in word_spans:
            encword = word.encode(encoding)
            if not checker.check(encword):
                encsuggs = checker.suggest(encword)
                maxsugg = 5  # limit to some reasonable number
                incmp = False
                if maxsugg > 0 and len(encsuggs) > maxsugg:
                    encsuggs = encsuggs[:maxsugg]
                    incmp = True
                suggs = [x.decode(encoding) for x in encsuggs]
                if maxsugg != 0 and suggs:
                    fmtsuggs = format_item_list(suggs, incmp=incmp)
                    snote = _("@info", "Unknown word '%(word)s' "
                              "(suggestions: %(wordlist)s).",
                              word=word,
                              wordlist=fmtsuggs)
                else:
                    snote = _("@info", "Unknown word '%(word)s'.", word=word)
                spans.append(span + (snote, ))

        if spanrep:
            return spans
        else:
            for span in spans:
                if span[2:]:
                    report_on_msg(span[2], msg, cat)
            return len(spans)