Exemplo n.º 1
0
    def merge_upload(self, request, fileobj, overwrite, author=None, merge_header=True, method=""):
        """
        Top level handler for file uploads.
        """
        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls(fileobj, self.subproject.template_store)
        except:
            # Fallback to automatic detection
            fileobj.seek(0)
            store = AutoFormat(fileobj)

        # Optionally set authorship
        if author is None:
            author = self.get_author_name(request.user)

        # List translations we should process
        translations = Translation.objects.filter(language=self.language, subproject__project=self.subproject.project)
        # Filter out those who don't want automatic update, but keep ourselves
        translations = translations.filter(Q(pk=self.pk) | Q(subproject__allow_translation_propagation=True))

        ret = False

        if method in ("", "fuzzy"):
            # Do actual merge
            for translation in translations:
                ret |= translation.merge_store(request, author, store, overwrite, merge_header, (method == "fuzzy"))
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store)

        return ret, store.count_units()
Exemplo n.º 2
0
    def merge_upload(self,
                     request,
                     fileobj,
                     overwrite,
                     author=None,
                     merge_header=True,
                     method=''):
        '''
        Top level handler for file uploads.
        '''
        filecopy = fileobj.read()
        fileobj.close()
        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls(
                StringIOMode(fileobj.name, filecopy),
                self.subproject.template_store)
        except Exception:
            # Fallback to automatic detection
            store = AutoFormat(StringIOMode(fileobj.name, filecopy), )

        # Optionally set authorship
        if author is None:
            author = self.get_author_name(request.user)

        # List translations we should process
        # Filter out those who don't want automatic update, but keep ourselves
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project).filter(
                Q(pk=self.pk)
                | Q(subproject__allow_translation_propagation=True))

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            if self.subproject.has_template():
                # Merge on units level
                self.merge_translations(request, author, store, overwrite,
                                        (method == 'fuzzy'))
            else:
                # Merge on file level
                for translation in translations:
                    ret |= translation.merge_store(request, author, store,
                                                   overwrite, merge_header,
                                                   (method == 'fuzzy'))
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store)

        return ret, store.count_units()
Exemplo n.º 3
0
    def merge_upload(self,
                     request,
                     fileobj,
                     overwrite,
                     author=None,
                     merge_header=True,
                     method=''):
        '''
        Top level handler for file uploads.
        '''
        filecopy = fileobj.read()
        fileobj.close()
        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls(
                StringIOMode(fileobj.name, filecopy),
                self.subproject.template_store)
        except Exception:
            # Fallback to automatic detection
            store = AutoFormat(StringIOMode(fileobj.name, filecopy), )

        # Optionally set authorship
        if author is None:
            author = self.get_author_name(request.user)

        # List translations we should process
        # Filter out those who don't want automatic update, but keep ourselves
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project).filter(
                Q(pk=self.pk)
                | Q(subproject__allow_translation_propagation=True))

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            if self.subproject.has_template():
                # Merge on units level
                self.merge_translations(request, author, store, overwrite,
                                        (method == 'fuzzy'))
            else:
                # Merge on file level
                for translation in translations:
                    ret |= translation.merge_store(request, author, store,
                                                   overwrite, merge_header,
                                                   (method == 'fuzzy'))
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store)

        return ret, store.count_units()
    def test_content(self):
        """Test content based guess from ttkit"""
        with open(TEST_PO, 'rb') as handle:
            data = handle.read()

        handle = BytesIO(data)
        store = AutoFormat.parse(handle)
        self.assertIsInstance(store, AutoFormat)
        self.assertIsInstance(store.store, pofile)
Exemplo n.º 5
0
    def upload(self, request, project, language, fileobj, method):
        """Handle dictionary upload."""
        from weblate.trans.models.change import Change
        store = AutoFormat.parse(fileobj)

        ret = 0

        # process all units
        for dummy, unit in store.iterate_merge(False):
            source = unit.get_source()
            target = unit.get_target()

            # Ignore too long words
            if len(source) > 190 or len(target) > 190:
                continue

            # Get object
            try:
                word, created = self.get_or_create(
                    project=project,
                    language=language,
                    source=source,
                    defaults={
                        'target': target,
                    },
                )
            except Dictionary.MultipleObjectsReturned:
                word = self.filter(
                    project=project,
                    language=language,
                    source=source
                )[0]
                created = False

            # Already existing entry found
            if not created:
                # Same as current -> ignore
                if target == word.target:
                    continue
                if method == 'add':
                    # Add word
                    word = self.create(
                        user=request.user,
                        action=Change.ACTION_DICTIONARY_UPLOAD,
                        project=project,
                        language=language,
                        source=source,
                        target=target
                    )
                elif method == 'overwrite':
                    # Update word
                    word.target = target
                    word.save()

            ret += 1

        return ret
Exemplo n.º 6
0
    def test_content(self):
        """Test content based guess from ttkit"""
        with open(TEST_PO, 'rb') as handle:
            data = handle.read()

        handle = BytesIO(data)
        store = AutoFormat.parse(handle)
        self.assertIsInstance(store, AutoFormat)
        self.assertIsInstance(store.store, pofile)
Exemplo n.º 7
0
    def upload(self, request, project, language, fileobj, method):
        '''
        Handles dictionary upload.
        '''
        from weblate.trans.models.changes import Change
        store = AutoFormat.parse(fileobj)

        ret = 0

        # process all units
        for dummy, unit in store.iterate_merge(False):
            source = unit.get_source()
            target = unit.get_target()

            # Ignore too long words
            if len(source) > 190 or len(target) > 190:
                continue

            # Get object
            word, created = self.get_or_create(
                project=project,
                language=language,
                source=source,
                defaults={
                    'target': target,
                },
            )

            # Already existing entry found
            if not created:
                # Same as current -> ignore
                if target == word.target:
                    continue
                if method == 'add':
                    # Add word
                    word = self.create(
                        request,
                        action=Change.ACTION_DICTIONARY_UPLOAD,
                        project=project,
                        language=language,
                        source=source,
                        target=target
                    )
                elif method == 'overwrite':
                    # Update word
                    word.target = target
                    word.save()

            ret += 1

        return ret
Exemplo n.º 8
0
    def upload(self, request, project, language, fileobj, method):
        """
        Handles dictionary update.
        """
        # Load file using translate-toolkit
        store = AutoFormat.load(fileobj)

        ret, skipped = self.import_store(request, project, language, store, method)

        if ret == 0 and skipped > 0 and isinstance(store, csvfile):
            # Retry with different CSV scheme
            fileobj.seek(0)
            store = csvfile(fileobj, ("source", "target"))
            ret, skipped = self.import_store(request, project, language, store, method)

        return ret
Exemplo n.º 9
0
    def upload(self, request, project, language, fileobj, method):
        '''
        Handles dictionary update.
        '''
        # Load file using translate-toolkit
        store = AutoFormat.load(fileobj)

        ret, skipped = self.import_store(request, project, language, store,
                                         method)

        if ret == 0 and skipped > 0 and isinstance(store, csvfile):
            # Retry with different CSV scheme
            fileobj.seek(0)
            store = csvfile(fileobj, ('source', 'target'))
            ret, skipped = self.import_store(request, project, language, store,
                                             method)

        return ret
Exemplo n.º 10
0
    def upload(self, request, project, language, fileobj, method):
        '''
        Handles dictionary update.
        '''
        filecopy = fileobj.read()
        fileobj.close()
        # Load file using translate-toolkit
        store = AutoFormat.load(StringIOMode(fileobj.name, filecopy))

        ret, skipped = self.import_store(request, project, language, store,
                                         method)

        if ret == 0 and skipped > 0 and isinstance(store, csvfile):
            # Retry with different CSV scheme
            store = csvfile(StringIOMode(fileobj.name, filecopy),
                            ('source', 'target'))
            ret, skipped = self.import_store(request, project, language, store,
                                             method)

        return ret
Exemplo n.º 11
0
    def upload(self, request, project, language, fileobj, method):
        '''
        Handles dictionary update.
        '''
        filecopy = fileobj.read()
        fileobj.close()
        # Load file using translate-toolkit
        store = AutoFormat.load(StringIOMode(fileobj.name, filecopy))

        ret, skipped = self.import_store(
            request, project, language, store, method
        )

        if ret == 0 and skipped > 0 and isinstance(store, csvfile):
            # Retry with different CSV scheme
            store = csvfile(
                StringIOMode(fileobj.name, filecopy),
                ('source', 'target')
            )
            ret, skipped = self.import_store(
                request, project, language, store, method
            )

        return ret
Exemplo n.º 12
0
 def single_test(self, filename, fileclass):
     with open(filename, 'r') as handle:
         store = AutoFormat.parse(handle)
         self.assertIsInstance(store, fileclass)
Exemplo n.º 13
0
 def single_test(self, filename, fileclass):
     with open(filename, 'rb') as handle:
         store = AutoFormat.parse(handle)
         self.assertIsInstance(store, fileclass)
     self.assertEqual(fileclass, detect_filename(filename))
Exemplo n.º 14
0
    def merge_upload(self, request, fileobj, overwrite, author=None,
                     merge_header=True, method='', fuzzy='',
                     merge_comments=False):
        """Top level handler for file uploads."""
        filecopy = fileobj.read()
        fileobj.close()

        # Strip possible UTF-8 BOM
        if filecopy[:3] == codecs.BOM_UTF8:
            filecopy = filecopy[3:]

        # Load backend file
        try:
            # First try using own loader
            store = self.store.parse(
                StringIOMode(fileobj.name, filecopy),
                self.subproject.template_store
            )
        except Exception:
            # Fallback to automatic detection
            store = AutoFormat.parse(
                StringIOMode(fileobj.name, filecopy),
            )

        # Optionally set authorship
        if author is None:
            author = get_author_name(request.user)

        # Check valid plural forms
        if hasattr(store.store, 'parseheader'):
            header = store.store.parseheader()
            if 'Plural-Forms' in header and \
                    self.language.get_plural_form() != header['Plural-Forms']:
                raise Exception('Plural forms do not match the language.')

        # List translations we should process
        # Filter out those who don't want automatic update, but keep ourselves
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project
        ).filter(
            Q(pk=self.pk) | Q(subproject__allow_translation_propagation=True)
        )

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            if self.subproject.has_template():
                # Merge on units level
                ret = self.merge_translations(
                    request,
                    store,
                    overwrite,
                    (method == 'fuzzy'),
                    fuzzy
                )
            else:
                # Merge on file level
                for translation in translations:
                    ret |= translation.merge_store(
                        request,
                        author,
                        store,
                        overwrite,
                        merge_header,
                        (method == 'fuzzy'),
                        fuzzy,
                        merge_comments=merge_comments,
                    )
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store, fuzzy)

        return ret, store.count_units()
Exemplo n.º 15
0
    def merge_upload(self,
                     request,
                     fileobj,
                     overwrite,
                     author=None,
                     merge_header=True,
                     method='',
                     fuzzy='',
                     merge_comments=False):
        """Top level handler for file uploads."""
        filecopy = fileobj.read()
        fileobj.close()

        # Strip possible UTF-8 BOM
        if filecopy[:3] == codecs.BOM_UTF8:
            filecopy = filecopy[3:]

        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls.parse(
                StringIOMode(fileobj.name, filecopy),
                self.subproject.template_store)
        except Exception:
            # Fallback to automatic detection
            store = AutoFormat.parse(StringIOMode(fileobj.name, filecopy), )

        # Optionally set authorship
        if author is None:
            author = get_author_name(request.user)

        # Check valid plural forms
        if hasattr(store.store, 'parseheader'):
            header = store.store.parseheader()
            if 'Plural-Forms' in header and \
                    self.language.get_plural_form() != header['Plural-Forms']:
                raise Exception('Plural forms do not match the language.')

        # List translations we should process
        # Filter out those who don't want automatic update, but keep ourselves
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project).filter(
                Q(pk=self.pk)
                | Q(subproject__allow_translation_propagation=True))

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            if self.subproject.has_template():
                # Merge on units level
                ret = self.merge_translations(request, store, overwrite,
                                              (method == 'fuzzy'), fuzzy)
            else:
                # Merge on file level
                for translation in translations:
                    ret |= translation.merge_store(
                        request,
                        author,
                        store,
                        overwrite,
                        merge_header,
                        (method == 'fuzzy'),
                        fuzzy,
                        merge_comments=merge_comments,
                    )
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store, fuzzy)

        return ret, store.count_units()
Exemplo n.º 16
0
 def single_test(self, filename, fileclass):
     with open(filename, 'r') as handle:
         store = AutoFormat.parse(handle)
         self.assertIsInstance(store, fileclass)
Exemplo n.º 17
0
 def single_test(self, filename, fileclass):
     with open(filename, 'rb') as handle:
         store = AutoFormat.parse(handle)
         self.assertIsInstance(store, fileclass)
     self.assertEqual(fileclass, detect_filename(filename))