Exemple #1
0
    def run_checks(self, unit=None, project=None, batch=False):
        """Update checks for this unit."""
        if unit is None:
            try:
                unit = self.units[0]
            except IndexError:
                return

        content_hash = unit.content_hash
        src = unit.get_source_plurals()
        if project is None:
            project = self.component.project

        # Fetch old checks
        if self.component.checks_cache is not None:
            old_checks = self.component.checks_cache.get(
                (content_hash, None), []
            )
        else:
            old_checks = set(
                Check.objects.filter(
                    content_hash=content_hash,
                    project=project,
                    language=None
                ).values_list('check', flat=True)
            )
        create = []

        # Run all source checks
        for check, check_obj in CHECKS.items():
            if batch and check_obj.batch_update:
                if check in old_checks:
                    # Do not remove batch checks in batch processing
                    old_checks.remove(check)
                continue
            if check_obj.source and check_obj.check_source(src, unit):
                if check in old_checks:
                    # We already have this check
                    old_checks.remove(check)
                else:
                    # Create new check
                    create.append(Check(
                        content_hash=content_hash,
                        project=project,
                        language=None,
                        ignore=False,
                        check=check
                    ))

        if create:
            Check.objects.bulk_create_ignore(create)

        # Remove stale checks
        if old_checks:
            Check.objects.filter(
                content_hash=content_hash,
                project=project,
                language=None,
                check__in=old_checks
            ).delete()
Exemple #2
0
    def run_checks(self, units=None, unit=None):
        """Update checks for this unit."""
        units = self.units if units is None else units
        if not units:
            return
        unit = units[0] if unit is None else unit

        content_hash = unit.content_hash
        src = unit.get_source_plurals()
        project = self.component.project
        was_change = False
        has_checks = None

        # Fetch old checks
        old_checks = set(
            Check.objects.filter(content_hash=content_hash,
                                 project=project,
                                 language=None).values_list('check',
                                                            flat=True))

        # Run all source checks
        for check, check_obj in CHECKS.items():
            if CHECKS[check].source and check_obj.check_source(src, unit):
                if check in old_checks:
                    # We already have this check
                    old_checks.remove(check)
                else:
                    # Create new check
                    Check.objects.create(content_hash=content_hash,
                                         project=project,
                                         language=None,
                                         ignore=False,
                                         check=check)
                    was_change = True
                    has_checks = True

        # Remove stale checks
        if old_checks:
            Check.objects.filter(content_hash=content_hash,
                                 project=project,
                                 language=None,
                                 check__in=old_checks).delete()
            was_change = True

        # Update unit flags
        if was_change:
            for unit in units:
                unit.update_has_failing_check(was_change, has_checks)
Exemple #3
0
    def run_batch_checks(self, attr_name):
        """Run batch executed checks"""
        from weblate.trans.models import Unit

        create = []
        meth_name = 'check_{}_project'.format(attr_name)
        for check, check_obj in CHECKS.items():
            if not getattr(check_obj, attr_name) or not check_obj.batch_update:
                continue
            self.log_info('running batch check: %s', check)
            # List of triggered checks
            data = getattr(check_obj, meth_name)(self)
            # Fetch existing check instances
            existing = set(
                Check.objects.filter(
                    unit__translation__component__project=self, check=check
                ).values_list('unit__content_hash', 'unit__translation__language_id')
            )
            # Create new check instances
            for item in data:
                if 'translation__language' not in item:
                    item['translation__language'] = self.source_language.id
                key = (item['content_hash'], item['translation__language'])
                if key in existing:
                    existing.discard(key)
                else:
                    units = Unit.objects.filter(
                        translation__component__project=self,
                        translation__language_id=item['translation__language'],
                        content_hash=item['content_hash'],
                    )
                    for unit in units:
                        create.append(Check(unit=unit, check=check, ignore=False))
            # Remove stale instances
            if existing:
                query = functools.reduce(
                    lambda q, value: q
                    | (
                        Q(unit__content_hash=value[0])
                        & Q(unit__translation__language_id=value[1])
                    ),
                    existing,
                    Q(),
                )
                Check.objects.filter(check=check).filter(query).delete()
        # Create new checks
        if create:
            Check.objects.bulk_create_ignore(create)
Exemple #4
0
    def run_checks(self, unit=None, project=None, batch=False):
        """Update checks for this unit."""
        if unit is None:
            try:
                unit = self.units[0]
            except IndexError:
                return

        content_hash = unit.content_hash
        src = unit.get_source_plurals()
        if project is None:
            project = self.component.project

        # Fetch old checks
        old_checks = set(
            Check.objects.filter(content_hash=content_hash,
                                 project=project,
                                 language=None).values_list('check',
                                                            flat=True))

        # Run all source checks
        for check, check_obj in CHECKS.items():
            if batch and check_obj.batch_update:
                if check in old_checks:
                    # Do not remove batch checks in batch processing
                    old_checks.remove(check)
                continue
            if check_obj.source and check_obj.check_source(src, unit):
                if check in old_checks:
                    # We already have this check
                    old_checks.remove(check)
                else:
                    # Create new check
                    Check.objects.create(content_hash=content_hash,
                                         project=project,
                                         language=None,
                                         ignore=False,
                                         check=check)

        # Remove stale checks
        if old_checks:
            Check.objects.filter(content_hash=content_hash,
                                 project=project,
                                 language=None,
                                 check__in=old_checks).delete()
Exemple #5
0
class Check(UnitData):
    check = models.CharField(max_length=50, choices=CHECKS.get_choices())
    ignore = models.BooleanField(db_index=True, default=False)

    objects = CheckManager()

    @cached_property
    def check_obj(self):
        try:
            return CHECKS[self.check]
        except KeyError:
            return None

    class Meta(object):
        unique_together = ('content_hash', 'project', 'language', 'check')
        index_together = [
            ('project', 'language', 'content_hash'),
        ]

    def __str__(self):
        return '{0}/{1}: {2}'.format(
            self.project,
            self.language,
            self.check,
        )

    def get_description(self):
        if self.check_obj:
            return self.check_obj.description
        return self.check

    def get_severity(self):
        if self.check_obj:
            return self.check_obj.severity
        return 'info'

    def get_doc_url(self):
        if self.check_obj:
            return self.check_obj.get_doc_url()
        return ''

    def set_ignore(self):
        """Set ignore flag."""
        self.ignore = True
        self.save()
Exemple #6
0
    def run_checks(self, unit=None):
        """Update checks for this unit."""
        if unit is None:
            try:
                unit = self.units[0]
            except IndexError:
                return

        content_hash = unit.content_hash
        src = unit.get_source_plurals()
        project = self.component.project

        # Fetch old checks
        old_checks = set(
            Check.objects.filter(
                content_hash=content_hash,
                project=project,
                language=None
            ).values_list('check', flat=True)
        )

        # Run all source checks
        for check, check_obj in CHECKS.items():
            if check_obj.source and check_obj.check_source(src, unit):
                if check in old_checks:
                    # We already have this check
                    old_checks.remove(check)
                else:
                    # Create new check
                    Check.objects.create(
                        content_hash=content_hash,
                        project=project,
                        language=None,
                        ignore=False,
                        check=check
                    )

        # Remove stale checks
        if old_checks:
            Check.objects.filter(
                content_hash=content_hash,
                project=project,
                language=None,
                check__in=old_checks
            ).delete()
Exemple #7
0
    def run_checks(self, same_state=True, same_content=True):
        """Update checks for this unit."""
        was_change = False
        has_checks = None
        is_source = self.translation.is_source

        src = self.get_source_plurals()
        tgt = self.get_target_plurals()

        old_checks = set(self.check_set.values_list('check', flat=True))
        create = []

        # Run all checks
        for check, check_obj in CHECKS.items():
            # Do not remove batch checks in batch processing
            if self.is_batch_update and check_obj.batch_update:
                old_checks.discard(check)
                continue

            # Does the check fire?
            if (not is_source and check_obj.target and check_obj.check_target(
                    src, tgt, self)) or (is_source and check_obj.source and
                                         check_obj.check_source(src, self)):
                if check in old_checks:
                    # We already have this check
                    old_checks.remove(check)
                else:
                    # Create new check
                    create.append(Check(unit=self, ignore=False, check=check))
                    was_change = True
                    has_checks = True

        if create:
            Check.objects.bulk_create_ignore(create)

        # Delete no longer failing checks
        if old_checks:
            was_change = True
            Check.objects.filter(unit=self, check__in=old_checks).delete()

        # Update failing checks flag
        if not self.is_batch_update and (was_change or not same_content):
            self.update_has_failing_check(was_change, has_checks)
Exemple #8
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from weblate.checks import CHECKS

EXTRA_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items()
    if v.default_disabled
}

EXTRA_FLAGS['rst-text'] = ugettext_lazy('RST text')
EXTRA_FLAGS['md-text'] = ugettext_lazy('Markdown text')
EXTRA_FLAGS['xml-text'] = ugettext_lazy('XML text')
EXTRA_FLAGS['dos-eol'] = ugettext_lazy('DOS line endings')
EXTRA_FLAGS['url'] = ugettext_lazy('URL')
EXTRA_FLAGS['auto-java-messageformat'] = ugettext_lazy(
    'Automatically detect Java MessageFormat'
)

IGNORE_CHECK_FLAGS = {CHECKS[x].ignore_string for x in CHECKS}

Exemple #9
0
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _, ugettext_lazy

from weblate.checks import CHECKS

EXTRA_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items() if v.default_disabled
}

EXTRA_FLAGS['rst-text'] = ugettext_lazy('RST text')
EXTRA_FLAGS['xml-text'] = ugettext_lazy('XML text')
EXTRA_FLAGS['dos-eol'] = ugettext_lazy('DOS end of lines')
EXTRA_FLAGS['auto-java-messageformat'] = ugettext_lazy(
    'Automatically detect Java MessageFormat')

IGNORE_CHECK_FLAGS = {CHECKS[x].ignore_string for x in CHECKS}


def validate_filemask(val):
    """Validate file mask that it contains *."""
    if '*' not in val:
        raise ValidationError(
Exemple #10
0
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

import six
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from weblate.checks import CHECKS
from weblate.fonts.utils import get_font_weight

PLAIN_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items() if v.default_disabled and not v.param_type
}
TYPED_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items() if v.param_type
}
TYPED_FLAGS_ARGS = {
    v.enable_string: v.param_type
    for k, v in CHECKS.items() if v.param_type
}

PLAIN_FLAGS["rst-text"] = ugettext_lazy("RST text")
PLAIN_FLAGS["md-text"] = ugettext_lazy("Markdown text")
PLAIN_FLAGS["xml-text"] = ugettext_lazy("XML text")
PLAIN_FLAGS["dos-eol"] = ugettext_lazy("DOS line endings")
PLAIN_FLAGS["url"] = ugettext_lazy("URL")
Exemple #11
0
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

import six
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from weblate.checks import CHECKS
from weblate.fonts.utils import get_font_weight

PLAIN_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items()
    if v.default_disabled and not v.param_type
}
TYPED_FLAGS = {v.enable_string: v.name for k, v in CHECKS.items() if v.param_type}
TYPED_FLAGS_ARGS = {
    v.enable_string: v.param_type for k, v in CHECKS.items() if v.param_type
}

PLAIN_FLAGS["rst-text"] = ugettext_lazy("RST text")
PLAIN_FLAGS["md-text"] = ugettext_lazy("Markdown text")
PLAIN_FLAGS["xml-text"] = ugettext_lazy("XML text")
PLAIN_FLAGS["dos-eol"] = ugettext_lazy("DOS line endings")
PLAIN_FLAGS["url"] = ugettext_lazy("URL")
PLAIN_FLAGS["auto-java-messageformat"] = ugettext_lazy(
    "Automatically detect Java MessageFormat"
)
Exemple #12
0
class Check(models.Model):
    unit = models.ForeignKey("trans.Unit", on_delete=models.deletion.CASCADE)
    check = models.CharField(max_length=50, choices=CHECKS.get_choices())
    ignore = models.BooleanField(db_index=True, default=False)

    @cached_property
    def check_obj(self):
        try:
            return CHECKS[self.check]
        except KeyError:
            return None

    class Meta:
        unique_together = ("unit", "check")

    def __str__(self):
        return "{0}: {1}".format(self.unit, self.check)

    def is_enforced(self):
        return self.check in self.unit.translation.component.enforced_checks

    def get_description(self):
        if self.check_obj:
            try:
                return self.check_obj.get_description(self)
            except IndexError:
                return self.check_obj.description
        return self.check

    def get_fixup(self):
        if self.check_obj:
            try:
                return self.check_obj.get_fixup(self.unit)
            except IndexError:
                return None
        return None

    def get_fixup_json(self):
        fixup = self.get_fixup()
        if not fixup:
            return None
        return json.dumps(fixup)

    def get_name(self):
        if self.check_obj:
            return self.check_obj.name
        return self.check

    def get_severity(self):
        if self.check_obj:
            return self.check_obj.severity
        return "info"

    def get_doc_url(self):
        if self.check_obj:
            return self.check_obj.get_doc_url()
        return ""

    def set_ignore(self, state=True):
        """Set ignore flag."""
        self.ignore = state
        self.save()
Exemple #13
0
class Check(models.Model):
    unit = models.ForeignKey("trans.Unit", on_delete=models.deletion.CASCADE)
    check = models.CharField(max_length=50, choices=CHECKS.get_choices())
    ignore = models.BooleanField(db_index=True, default=False)

    objects = CheckManager()

    @cached_property
    def check_obj(self):
        try:
            return CHECKS[self.check]
        except KeyError:
            return None

    class Meta(object):
        unique_together = ('unit', 'check')

    def __str__(self):
        return '{0}: {1}'.format(self.unit, self.check)

    def get_description(self):
        if self.check_obj:
            try:
                return self.check_obj.get_description(self.unit)
            except IndexError:
                return self.check_obj.description
        return self.check

    def get_fixup(self):
        if self.check_obj:
            try:
                return self.check_obj.get_fixup(self.unit)
            except IndexError:
                return None
        return None

    def get_fixup_json(self):
        fixup = self.get_fixup()
        if not fixup:
            return None
        return json.dumps(fixup)

    def get_name(self):
        if self.check_obj:
            return self.check_obj.name
        return self.check

    def get_severity(self):
        if self.check_obj:
            return self.check_obj.severity
        return 'info'

    def get_doc_url(self):
        if self.check_obj:
            return self.check_obj.get_doc_url()
        return ''

    def set_ignore(self):
        """Set ignore flag."""
        self.ignore = True
        self.save()
Exemple #14
0
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _, ugettext_lazy

from weblate.checks import CHECKS

EXTRA_FLAGS = {
    v.enable_string: v.name
    for k, v in CHECKS.items()
    if v.default_disabled
}

EXTRA_FLAGS['rst-text'] = ugettext_lazy('RST text')
EXTRA_FLAGS['md-text'] = ugettext_lazy('Mardown text')
EXTRA_FLAGS['xml-text'] = ugettext_lazy('XML text')
EXTRA_FLAGS['dos-eol'] = ugettext_lazy('DOS end of lines')
EXTRA_FLAGS['url'] = ugettext_lazy('URL')
EXTRA_FLAGS['auto-java-messageformat'] = ugettext_lazy(
    'Automatically detect Java MessageFormat'
)

IGNORE_CHECK_FLAGS = {CHECKS[x].ignore_string for x in CHECKS}