Ejemplo n.º 1
0
def fix_pofile_plurals(pofile, logger, ztm):
    """Fix plural translations for PO files with mismatching headers."""
    logger.debug("Checking if PO file %d needs fixing" % pofile.id)
    plural_forms_mapping = get_mapping_for_pofile_plurals(pofile)
    if plural_forms_mapping is not None:
        logger.info("Fixing PO file %s" % pofile.title)
        pluralmessages = TranslationMessage.select("""
            POTMsgSet.id = TranslationMessage.potmsgset AND
            POTMsgSet.msgid_plural IS NOT NULL AND
            TranslationMessage.pofile = %s""" % sqlvalues(pofile),
            clauseTables=["POTMsgSet"])
        for message in pluralmessages:
            logger.debug("\tFixing translations for '%s'" % (
                message.potmsgset.singular_text))

            for form in xrange(TranslationConstants.MAX_PLURAL_FORMS):
                new_form = plural_forms_mapping[form]
                assert new_form < TranslationConstants.MAX_PLURAL_FORMS, (
                    "Translation with plural form %d in plurals mapping." %
                    new_form)
                translation = getattr(message, 'msgstr%d' % new_form)
                setattr(message, 'msgstr%d' % form, translation)

        # We also need to update the header so we don't try to re-do the
        # migration in the future.
        header = POHeader(pofile.header)
        header.plural_form_expression = pofile.language.pluralexpression
        header.has_plural_forms = True
        pofile.header = header.getRawContent()
        ztm.commit()
Ejemplo n.º 2
0
    def mergeTranslationMessages(self):
        """Share `TranslationMessage`s between templates where possible."""
        order_check = OrderingCheck(
            key=methodcaller('sharingKey'), reverse=True)
        for template_number, template in enumerate(self.potemplates):
            log.info("Merging template %d/%d." % (
                template_number + 1, len(self.potemplates)))
            deletions = 0
            order_check.check(template)
            potmsgset_ids = self._getPOTMsgSetIds(template)
            for potmsgset_id in potmsgset_ids:
                potmsgset = POTMsgSet.get(potmsgset_id)

                tm_ids = self._partitionTranslationMessageIds(potmsgset)
                before = sum([len(sublist) for sublist in tm_ids], 0)

                for ids in tm_ids:
                    for id in ids:
                        message = TranslationMessage.get(id)
                        removeSecurityProxy(message).shareIfPossible()

                self.tm.endTransaction(intermediate=True)

                after = potmsgset.getAllTranslationMessages().count()
                deletions += max(0, before - after)

            report = "Deleted TranslationMessages: %d." % deletions
            if deletions > 0:
                log.info(report)
            else:
                log.log(DEBUG2, report)
Ejemplo n.º 3
0
    def mergeTranslationMessages(self):
        """Share `TranslationMessage`s between templates where possible."""
        order_check = OrderingCheck(key=methodcaller('sharingKey'),
                                    reverse=True)
        for template_number, template in enumerate(self.potemplates):
            log.info("Merging template %d/%d." %
                     (template_number + 1, len(self.potemplates)))
            deletions = 0
            order_check.check(template)
            potmsgset_ids = self._getPOTMsgSetIds(template)
            for potmsgset_id in potmsgset_ids:
                potmsgset = POTMsgSet.get(potmsgset_id)

                tm_ids = self._partitionTranslationMessageIds(potmsgset)
                before = sum([len(sublist) for sublist in tm_ids], 0)

                for ids in tm_ids:
                    for id in ids:
                        message = TranslationMessage.get(id)
                        removeSecurityProxy(message).shareIfPossible()

                self.tm.endTransaction(intermediate=True)

                after = potmsgset.getAllTranslationMessages().count()
                deletions += max(0, before - after)

            report = "Deleted TranslationMessages: %d." % deletions
            if deletions > 0:
                log.info(report)
            else:
                log.log(DEBUG2, report)
Ejemplo n.º 4
0
def fix_pofile_plurals(pofile, logger, ztm):
    """Fix plural translations for PO files with mismatching headers."""
    logger.debug("Checking if PO file %d needs fixing" % pofile.id)
    plural_forms_mapping = get_mapping_for_pofile_plurals(pofile)
    if plural_forms_mapping is not None:
        logger.info("Fixing PO file %s" % pofile.title)
        pluralmessages = TranslationMessage.select("""
            POTMsgSet.id = TranslationMessage.potmsgset AND
            POTMsgSet.msgid_plural IS NOT NULL AND
            TranslationMessage.pofile = %s""" % sqlvalues(pofile),
                                                   clauseTables=["POTMsgSet"])
        for message in pluralmessages:
            logger.debug("\tFixing translations for '%s'" %
                         (message.potmsgset.singular_text))

            for form in xrange(TranslationConstants.MAX_PLURAL_FORMS):
                new_form = plural_forms_mapping[form]
                assert new_form < TranslationConstants.MAX_PLURAL_FORMS, (
                    "Translation with plural form %d in plurals mapping." %
                    new_form)
                translation = getattr(message, 'msgstr%d' % new_form)
                setattr(message, 'msgstr%d' % form, translation)

        # We also need to update the header so we don't try to re-do the
        # migration in the future.
        header = POHeader(pofile.header)
        header.plural_form_expression = pofile.language.pluralexpression
        header.has_plural_forms = True
        pofile.header = header.getRawContent()
        ztm.commit()
Ejemplo n.º 5
0
    def _scrubPOTMsgSetTranslations(self, potmsgset):
        """Map out translations for `potmsgset`, and eliminate duplicates.

        In the transition period for message sharing, there may be
        duplicate TranslationMessages that may upset assumptions in the
        code.  Clean those up.
        """
        # XXX JeroenVermeulen 2009-06-15
        # spec=message-sharing-prevent-duplicates: We're going to have a
        # unique index again at some point that will prevent this.  When
        # it becomes impossible to test this function, this whole
        # migration phase can be scrapped.
        ids_per_language = self._partitionTranslationMessageIds(potmsgset)

        self.tm.endTransaction(intermediate=True)

        deletions = 0

        for ids in ids_per_language:
            translations = {}

            for tm_id in ids:
                tm = TranslationMessage.get(tm_id)
                key = self._getPOTMsgSetTranslationMessageKey(tm)

                if key in translations:
                    language_code = tm.language.code
                    log.info(
                        "Cleaning up identical '%s' message for: \"%s\"" % (
                            language_code, potmsgset.singular_text))

                    existing_tm = translations[key]
                    assert tm != existing_tm, (
                        "Message is duplicate of itself.")
                    assert tm.potmsgset == existing_tm.potmsgset, (
                        "Different potmsgsets considered identical.")
                    assert tm.potemplate == existing_tm.potemplate, (
                        "Different potemplates considered identical.")

                    # Transfer any current/imported flags to the existing
                    # message, and delete the duplicate.
                    bequeathe_flags(tm, existing_tm)
                    deletions += 1
                else:
                    translations[key] = tm

            self.tm.endTransaction(intermediate=True)

        report = "Deleted TranslationMessages: %d" % deletions
        if deletions > 0:
            log.info(report)
        else:
            log.log(DEBUG2, report)
Ejemplo n.º 6
0
    def _scrubPOTMsgSetTranslations(self, potmsgset):
        """Map out translations for `potmsgset`, and eliminate duplicates.

        In the transition period for message sharing, there may be
        duplicate TranslationMessages that may upset assumptions in the
        code.  Clean those up.
        """
        # XXX JeroenVermeulen 2009-06-15
        # spec=message-sharing-prevent-duplicates: We're going to have a
        # unique index again at some point that will prevent this.  When
        # it becomes impossible to test this function, this whole
        # migration phase can be scrapped.
        ids_per_language = self._partitionTranslationMessageIds(potmsgset)

        self.tm.endTransaction(intermediate=True)

        deletions = 0

        for ids in ids_per_language:
            translations = {}

            for tm_id in ids:
                tm = TranslationMessage.get(tm_id)
                key = self._getPOTMsgSetTranslationMessageKey(tm)

                if key in translations:
                    language_code = tm.language.code
                    log.info("Cleaning up identical '%s' message for: \"%s\"" %
                             (language_code, potmsgset.singular_text))

                    existing_tm = translations[key]
                    assert tm != existing_tm, (
                        "Message is duplicate of itself.")
                    assert tm.potmsgset == existing_tm.potmsgset, (
                        "Different potmsgsets considered identical.")
                    assert tm.potemplate == existing_tm.potemplate, (
                        "Different potemplates considered identical.")

                    # Transfer any current/imported flags to the existing
                    # message, and delete the duplicate.
                    bequeathe_flags(tm, existing_tm)
                    deletions += 1
                else:
                    translations[key] = tm

            self.tm.endTransaction(intermediate=True)

        report = "Deleted TranslationMessages: %d" % deletions
        if deletions > 0:
            log.info(report)
        else:
            log.log(DEBUG2, report)
Ejemplo n.º 7
0
def make_translationmessage(factory,
                            pofile=None,
                            potmsgset=None,
                            ubuntu=True,
                            upstream=True,
                            diverged=False,
                            translations=None):
    """Creates a TranslationMessage directly and sets relevant parameters.

    This is very low level function used to test core Rosetta
    functionality such as setCurrentTranslation() method.  If not used
    correctly, it will trigger unique constraints.
    """
    if pofile is None:
        pofile = factory.makePOFile('sr')
    if potmsgset is None:
        potmsgset = factory.makePOTMsgSet(potemplate=pofile.potemplate)
    if translations is None:
        translations = [factory.getUniqueString()]
    if diverged:
        potemplate = pofile.potemplate
    else:
        potemplate = None

    # Parameters we don't care about are origin, submitter and
    # validation_status.
    origin = RosettaTranslationOrigin.SCM
    submitter = pofile.owner
    validation_status = TranslationValidationStatus.UNKNOWN

    translations = dict(enumerate(translations))

    potranslations = removeSecurityProxy(potmsgset)._findPOTranslations(
        translations)
    new_message = TranslationMessage(potmsgset=potmsgset,
                                     potemplate=potemplate,
                                     pofile=None,
                                     language=pofile.language,
                                     origin=origin,
                                     submitter=submitter,
                                     msgstr0=potranslations[0],
                                     msgstr1=potranslations[1],
                                     msgstr2=potranslations[2],
                                     msgstr3=potranslations[3],
                                     msgstr4=potranslations[4],
                                     msgstr5=potranslations[5],
                                     validation_status=validation_status,
                                     is_current_ubuntu=ubuntu,
                                     is_current_upstream=upstream)
    return new_message