Exemplo n.º 1
0
    def _getOrCreatePOTemplateForPath(self, path):
        """Find the POTemplate that path should be imported into.

        If no existing template could be found it creates one if possible.

        :param path: The path of the file to be imported.
        :return: The POTemplate instance or None.
        """
        if path not in self.filenames:
            # The file is not a template file.
            return None

        potemplate = self._potemplateset.getPOTemplateByPath(path)
        if potemplate is None:
            domain = make_domain(path)
            name = make_name(domain)
            if name == '':
                # A generic name does not contain a translation domain.
                potemplate = self._getOrCreateGenericTemplate(path)
            else:
                potemplate = self._potemplateset.getPOTemplateByName(name)
                if potemplate is None:
                    # Still no template found, create a new one.
                    if not self._potemplateset.isNameUnique(name):
                        # The name probably matches an inactive template.
                        return None
                    potemplate = self._potemplateset.new(
                        name, domain, path, self.owner)
        return potemplate
    def initial_values(self):
        """Initialize some values on the form, when it's possible."""
        field_values = {}
        if self.request.method == 'POST':
            # We got a form post, we don't need to do any initialization.
            return field_values
        # Fill the know values.
        field_values['path'] = self.context.path

        importer = getUtility(ITranslationImporter)
        if importer.isTemplateName(self.context.path):
            file_type = TranslationFileType.POT
        elif importer.isTranslationName(self.context.path):
            file_type = TranslationFileType.PO
        else:
            file_type = TranslationFileType.UNSPEC
        field_values['file_type'] = file_type

        if self.context.sourcepackagename is not None:
            field_values['sourcepackagename'] = self.context.sourcepackagename
        if self.context.is_targeted_to_ubuntu:
            if self.context.potemplate is None:
                # Default for Ubuntu templates is to
                # include them in languagepacks.
                field_values['languagepack'] = True
            else:
                field_values['languagepack'] = (
                    self.context.potemplate.languagepack)
        if (file_type
                in (TranslationFileType.POT, TranslationFileType.UNSPEC)):
            potemplate = self.context.potemplate
            if potemplate is None:
                domain = make_domain(self.context.path)
                field_values['name'] = make_name(domain)
                field_values['translation_domain'] = domain
            else:
                field_values['name'] = potemplate.name
                field_values['translation_domain'] = (
                    potemplate.translation_domain)
        if file_type in (TranslationFileType.PO, TranslationFileType.UNSPEC):
            field_values['potemplate'] = self.context.potemplate
            if self.context.pofile is not None:
                field_values['language'] = self.context.pofile.language
            else:
                # The entries that are translations usually have the language
                # code
                # as its filename. We try to get it to use as a suggestion.
                language_set = getUtility(ILanguageSet)
                filename = os.path.basename(self.context.path)
                guessed_language, file_ext = filename.split(u'.', 1)
                language = language_set.getLanguageByCode(guessed_language)
                if language is not None:
                    field_values['language'] = language
                    # Need to warn the user that we guessed the language
                    # information.
                    self.request.response.addWarningNotification(
                        "Review the language selection as we guessed it and"
                        " could not be accurate.")

        return field_values
 def test_make_domain_extracts_domain_from_meaningful_paths(self):
     # make_domain will spot a meaningful identifier in a typical
     # useful template file path.
     for path in self.meaningful_paths:
         domain = make_domain(path)
         self.assertEqual(
             self.meaningful_path_domain, domain,
             "Path '%s' yielded domain '%s'; should have found '%s'."
             % (path, domain, self.meaningful_path_domain))
    def initial_values(self):
        """Initialize some values on the form, when it's possible."""
        field_values = {}
        if self.request.method == "POST":
            # We got a form post, we don't need to do any initialization.
            return field_values
        # Fill the know values.
        field_values["path"] = self.context.path

        importer = getUtility(ITranslationImporter)
        if importer.isTemplateName(self.context.path):
            file_type = TranslationFileType.POT
        elif importer.isTranslationName(self.context.path):
            file_type = TranslationFileType.PO
        else:
            file_type = TranslationFileType.UNSPEC
        field_values["file_type"] = file_type

        if self.context.sourcepackagename is not None:
            field_values["sourcepackagename"] = self.context.sourcepackagename
        if self.context.is_targeted_to_ubuntu:
            if self.context.potemplate is None:
                # Default for Ubuntu templates is to
                # include them in languagepacks.
                field_values["languagepack"] = True
            else:
                field_values["languagepack"] = self.context.potemplate.languagepack
        if file_type in (TranslationFileType.POT, TranslationFileType.UNSPEC):
            potemplate = self.context.potemplate
            if potemplate is None:
                domain = make_domain(self.context.path)
                field_values["name"] = make_name(domain)
                field_values["translation_domain"] = domain
            else:
                field_values["name"] = potemplate.name
                field_values["translation_domain"] = potemplate.translation_domain
        if file_type in (TranslationFileType.PO, TranslationFileType.UNSPEC):
            field_values["potemplate"] = self.context.potemplate
            if self.context.pofile is not None:
                field_values["language"] = self.context.pofile.language
            else:
                # The entries that are translations usually have the language
                # code
                # as its filename. We try to get it to use as a suggestion.
                language_set = getUtility(ILanguageSet)
                filename = os.path.basename(self.context.path)
                guessed_language, file_ext = filename.split(u".", 1)
                language = language_set.getLanguageByCode(guessed_language)
                if language is not None:
                    field_values["language"] = language
                    # Need to warn the user that we guessed the language
                    # information.
                    self.request.response.addWarningNotification(
                        "Review the language selection as we guessed it and" " could not be accurate."
                    )

        return field_values
 def test_make_domain_falls_back_on_default(self):
     # When a path contains no usable identifier, make_domain falls
     # back on the default you pass it.
     default_domain = 'default_fallback'
     for path in self.meaningless_paths:
         domain = make_domain(path, default=default_domain)
         self.assertEqual(
             default_domain, domain,
             "Path '%s' yielded domain '%s'; expected default '%s'."
             % (path, domain, default_domain))
 def test_make_domain_finds_no_domain_in_meaningless_paths(self):
     # make_domain will not find any usable identifiers in a typical
     # meaningless template file path, and default to the empty
     # string.
     for path in self.meaningless_paths:
         domain = make_domain(path)
         self.assertEqual(
             '', domain,
             "Path '%s' yielded domain '%s'; should have found nothing."
             % (path, domain))
Exemplo n.º 7
0
    def approve(self, entry):
        """Check the given ImportQueueEntry against the internal approval
        list and set its values accordingly.

        :param entry: The queue entry that needs to be approved.
        """
        if entry is None:
            return None

        if not self.is_approval_possible:
            return entry
        potemplate = None
        # Path must be a template path.
        if entry.path not in self._potemplates:
            return entry

        product_name = get_product_name(entry.productseries)
        domain = make_domain(entry.path, default=product_name)
        if self._potemplates[entry.path] is None:
            if self.unmatched_objects > 0:
                # Unmatched entries in database, do not approve.
                return entry
            # Path must provide a translation domain.
            if domain == '':
                return entry
            # No (possibly) matching entry found: create one.
            name = make_name(domain)
            if not self._potemplateset.isNameUnique(name):
                # The name probably matches an inactive template.
                return entry
            potemplate = self._potemplateset.new(
                name, domain, entry.path, entry.importer)
            self._potemplates[entry.path] = potemplate
            self._n_matched += 1
        else:
            # A matching entry is found, the import can be approved.
            potemplate = self._potemplates[entry.path]
            potemplate.path = entry.path
            if domain != '':
                potemplate.translation_domain = domain

        # Approve the entry
        entry.potemplate = potemplate
        if entry.status == RosettaImportStatus.NEEDS_REVIEW:
            entry.setStatus(RosettaImportStatus.APPROVED,
                            getUtility(ILaunchpadCelebrities).rosetta_experts)
        return entry