<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals


class LinguistRegistry(object):
    """
    Linguist Registry.
    """

    def __init__(self):
        """
        Initializes a new registry.
        """
        self._registry = {}

    @property
    def identifiers(self):
        """
        Returns registered identifiers (as list).
        """
        return self._registry.keys()

    @property
    def translations(self):
        """
        Returns registered translation classes (as list).
        """
        return self._registry.values()

    def register(self, translations):
        """
        Registers the given ``translations`` classes which must be a subclass of
        ``base.ModelTranslationBase``.
        """
        from .descriptors import contribute_to_model

        if not isinstance(translations, (list, tuple)):
            translations = [translations]

        for translation in translations:
            self.validate_translation(translation)
            contribute_to_model(translation)
            self._registry[translation.identifier] = translation

    def unregister(self, identifier):
        """
        Unregisters the translation class with the given ``identifier``.
        If ``identifier`` does not exist, raises ``exceptions.Unregistered``.
        """
        self.validate_identifier(identifier)
        del self._registry[identifier]

    def get_translation(self, identifier):
        """
        Returns the translation class with the given ``identifier``.
        If ``identifier`` does not exist, raises ``exceptions.Unregistered``.
        """
        self.validate_identifier(identifier)
        return self._registry[identifier]

    def validate_model(self, model):
        """
        Validates the given model.

        This model must be a ``django.db.models.Model`` class.
        If this model is a valid class, just returns ``True``.
        Otherwise, raises ``exceptions.InvalidModel``.
        """
        import types
        from django.db import models
        from .exceptions import InvalidModel

        err_msg = 'Class "%s" is not a subclass of django.db.models.Model'

        if not isinstance(model, (type, types.ClassType)):
            raise InvalidModel(err_msg % model)

        if not issubclass(model, models.Model):
            raise InvalidModel(err_msg % model.__class__.__name__)

        return True

    def validate_identifier(self, identifier, in_registered=False):
        """
        Checks if the given ``identifier`` is either ``in_registered`` or not.
        Otherwise, raises ``Unregistered`` or  ``AlreadyRegistered``
        """
        from .exceptions import AlreadyRegistered, Unregistered

        if not isinstance(identifier, str):
            raise TypeError('Identifier must be a string')

        if len(identifier) > 100:
            raise TypeError('Identifier cannot have more than 100 characters')

        if in_registered:
            if identifier in self.identifiers:
                raise AlreadyRegistered(
                    'Model identifier "%s" is already registered' % identifier)
        else:
            if identifier not in self.identifiers:
                raise Unregistered(<commit_msg>Registry: fix validate identifier (str -> six.string_types).<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals


class LinguistRegistry(object):
    """
    Linguist Registry.
    """

    def __init__(self):
        """
        Initializes a new registry.
        """
        self._registry = {}

    @property
    def identifiers(self):
        """
        Returns registered identifiers (as list).
        """
        return self._registry.keys()

    @property
    def translations(self):
        """
        Returns registered translation classes (as list).
        """
        return self._registry.values()

    def register(self, translations):
        """
        Registers the given ``translations`` classes which must be a subclass of
        ``base.ModelTranslationBase``.
        """
        from .descriptors import contribute_to_model

        if not isinstance(translations, (list, tuple)):
            translations = [translations]

        for translation in translations:
            self.validate_translation(translation)
            contribute_to_model(translation)
            self._registry[translation.identifier] = translation

    def unregister(self, identifier):
        """
        Unregisters the translation class with the given ``identifier``.
        If ``identifier`` does not exist, raises ``exceptions.Unregistered``.
        """
        self.validate_identifier(identifier)
        del self._registry[identifier]

    def get_translation(self, identifier):
        """
        Returns the translation class with the given ``identifier``.
        If ``identifier`` does not exist, raises ``exceptions.Unregistered``.
        """
        self.validate_identifier(identifier)
        return self._registry[identifier]

    def validate_model(self, model):
        """
        Validates the given model.

        This model must be a ``django.db.models.Model`` class.
        If this model is a valid class, just returns ``True``.
        Otherwise, raises ``exceptions.InvalidModel``.
        """
        import types
        from django.db import models
        from .exceptions import InvalidModel

        err_msg = 'Class "%s" is not a subclass of django.db.models.Model'

        if not isinstance(model, (type, types.ClassType)):
            raise InvalidModel(err_msg % model)

        if not issubclass(model, models.Model):
            raise InvalidModel(err_msg % model.__class__.__name__)

        return True

    def validate_identifier(self, identifier, in_registered=False):
        """
        Checks if the given ``identifier`` is either ``in_registered`` or not.
        Otherwise, raises ``Unregistered`` or  ``AlreadyRegistered``
        """
        from django.utils.six import string_types
        from .exceptions import AlreadyRegistered, Unregistered

        if not isinstance(identifier, string_types):
            raise TypeError('Identifier must be a string')

        if len(identifier) > 100:
            raise TypeError('Identifier cannot have more than 100 characters')

        if in_registered:
            if identifier in self.identifiers:
                raise AlreadyRegistered(
                    'Model identifier "%s" is already registered' % identifier)
        else:
            if identifier not in self.identifiers:
                raise Unregistered(
{'fraction_non_alphanumeric': 0.09301270417422867, 'fraction_numerical': 0.002117362371445856, 'mean_word_length': 2.286779324055666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '5029608', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1550, 'n_words': 502}