Пример #1
0
def resolve_auto_format(apps, schema_editor):
    Component = apps.get_model('trans', 'Component')
    for component in Component.objects.filter(file_format='auto'):
        path = get_path(component)
        template = None
        if component.template:
            template = AutodetectFormat.parse(
                os.path.join(path, component.template))
        try:
            translation = component.translation_set.all()[0]
        except IndexError:
            if template is None and component.new_base:
                template = AutodetectFormat.parse(
                    os.path.join(path, component.new_base))
            if template is not None:
                update_format(component, template)
                continue
            raise Exception(
                'Existing translation component with auto format and '
                'without any translations, can not detect file format. '
                'Please edit the format manually and rerun migration. '
                'Affected component: {}/{}'.format(component.project.slug,
                                                   component.slug))
        store = AutodetectFormat.parse(
            os.path.join(path, translation.filename), template)
        update_format(component, store)
Пример #2
0
def resolve_auto_format(apps, schema_editor):
    Component = apps.get_model("trans", "Component")
    db_alias = schema_editor.connection.alias
    for component in Component.objects.using(db_alias).filter(file_format="auto"):
        path = get_path(component)
        template = None
        if component.template:
            template = AutodetectFormat.parse(os.path.join(path, component.template))
        try:
            translation = component.translation_set.all()[0]
        except IndexError:
            if template is None and component.new_base:
                template = AutodetectFormat.parse(
                    os.path.join(path, component.new_base)
                )
            if template is not None:
                update_format(component, template)
                continue
            raise Exception(
                "Existing translation component with auto format and "
                "without any translations, can not detect file format. "
                "Please edit the format manually and rerun migration. "
                "Affected component: {}/{}".format(
                    component.project.slug, component.slug
                )
            )
        store = AutodetectFormat.parse(
            os.path.join(path, translation.filename), template
        )
        update_format(component, store)
Пример #3
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 = AutodetectFormat.parse(handle)
        self.assertIsInstance(store, AutodetectFormat)
        self.assertIsInstance(store.store, pofile)
Пример #4
0
    def upload(self, request, project, language, fileobj, method):
        """Handle dictionary upload."""
        from weblate.trans.models.change import Change
        store = AutodetectFormat.parse(fileobj)

        ret = 0

        # process all units
        for dummy, unit in store.iterate_merge(False):
            source = unit.source
            target = unit.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
                    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
Пример #5
0
    def upload(self, request, glossary, language, fileobj, method):
        """Handle glossary upload."""
        from weblate.trans.models.change import Change

        store = AutodetectFormat.parse(fileobj)

        ret = 0

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

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

            # Get object
            try:
                term, created = self.get_or_create(
                    glossary=glossary,
                    language=language,
                    source=source,
                    defaults={"target": target},
                )
            except Term.MultipleObjectsReturned:
                term = self.filter(glossary=glossary,
                                   language=language,
                                   source=source)[0]
                created = False

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

            ret += 1

        return ret
Пример #6
0
    def upload(self, request, project, language, fileobj, method):
        """Handle dictionary upload."""
        from weblate.trans.models.change import Change

        store = AutodetectFormat.parse(fileobj)

        ret = 0

        # process all units
        for _unused, unit in store.iterate_merge(False):
            source = unit.source
            target = unit.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
                    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
Пример #7
0
 def single_test(self, filename, fileclass):
     with open(filename, 'rb') as handle:
         store = AutodetectFormat.parse(handle)
         self.assertIsInstance(store, fileclass)
     self.assertEqual(fileclass, detect_filename(filename))