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 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 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
def test_make_name_invalid_chars(self): # Invalid characters are removed for template names. self.assertEqual('my-domain', make_name('my - do@ #*$&main'))
def test_make_name_lowercase(self): # Upper case letters are converted to lower case for template names. self.assertEqual('mydomain', make_name('MyDomain'))
def test_make_name_underscore(self): # Underscores are converted to dashes for template names. self.assertEqual('my-domain', make_name('my_domain'))