<commit_before>from collections import namedtuple

from django.utils import translation

from elasticsearch_dsl import F, query
from elasticsearch_dsl.filter import Bool
from rest_framework.filters import BaseFilterBackend

from olympia import amo


def get_locale_analyzer(lang):
    analyzer = amo.SEARCH_LANGUAGE_TO_ANALYZER.get(lang)
    return analyzer


class AddonFilterParam(object):
    """Helper to build a simple ES lookup query from a request.GET param."""
    operator = 'term'  # ES filter to use when filtering.
    query_param = None
    reverse_dict = None
    valid_values = None
    es_field = None

    def get_value(self, request):
        value = request.GET[self.query_param]
        try:
            # Try the int first.
            value = int(value)
        except ValueError:
            # Fall back on the string, it should be a key in the reverse dict.
            value = self.get_value_from_reverse_dict(request)
        if value in self.valid_values:
            return value
        raise ValueError

    def get_value_from_reverse_dict(self, request):
        value = request.GET[self.query_param]
        return self.reverse_dict.get(value.lower())

    def get_value_from_object_from_reverse_dict(self, request):
        value = request.GET[self.query_param]
        value = self.reverse_dict.get(value.lower())
        if value is None:
            raise ValueError
        return value.id


class AddonAppFilterParam(AddonFilterParam):
    query_param = 'app'
    reverse_dict = amo.APPS
    valid_values = amo.APP_IDS
    es_field = 'app'

    def get_value_from_reverse_dict(self, request):
        return self.get_value_from_object_from_reverse_dict(request)


class AddonPlatformFilterParam(AddonFilterParam):
    query_param = 'platform'
    reverse_dict = amo.PLATFORM_DICT
    valid_values = amo.PLATFORMS
    es_field = 'platforms'<commit_msg>Add explicit exception message and remove unused import.<commit_after>from django.utils import translation

from elasticsearch_dsl import F, query
from elasticsearch_dsl.filter import Bool
from rest_framework.filters import BaseFilterBackend

from olympia import amo


def get_locale_analyzer(lang):
    analyzer = amo.SEARCH_LANGUAGE_TO_ANALYZER.get(lang)
    return analyzer


class AddonFilterParam(object):
    """Helper to build a simple ES lookup query from a request.GET param."""
    operator = 'term'  # ES filter to use when filtering.
    query_param = None
    reverse_dict = None
    valid_values = None
    es_field = None

    def get_value(self, request):
        value = request.GET[self.query_param]
        try:
            # Try the int first.
            value = int(value)
        except ValueError:
            # Fall back on the string, it should be a key in the reverse dict.
            value = self.get_value_from_reverse_dict(request)
        if value in self.valid_values:
            return value
        raise ValueError('Invalid value (not present in self.valid_values).')

    def get_value_from_reverse_dict(self, request):
        value = request.GET[self.query_param]
        return self.reverse_dict.get(value.lower())

    def get_value_from_object_from_reverse_dict(self, request):
        value = request.GET[self.query_param]
        value = self.reverse_dict.get(value.lower())
        if value is None:
            raise ValueError('Invalid value (not found in reverse dict).')
        return value.id


class AddonAppFilterParam(AddonFilterParam):
    query_param = 'app'
    reverse_dict = amo.APPS
    valid_values = amo.APP_IDS
    es_field = 'app'

    def get_value_from_reverse_dict(self, request):
        return self.get_value_from_object_from_reverse_dict(request)


class AddonPlatformFilterParam(AddonFilterParam):
    query_param = 'platform'
    reverse_dict = amo.PLATFORM_DICT
    valid_values = amo.PLATFORMS
    es_field = 'platforms'
{'fraction_non_alphanumeric': 0.07003089598352215, 'fraction_numerical': 0.0, 'mean_word_length': 3.124203821656051, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641182', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1137, 'n_words': 305}