<commit_before>"""
The registry module provides tools to maintain a registry of channels.

The first thing that should happen when django starts is registration of
channels. It should happen first, because channels are required for
autocomplete widgets. And autocomplete widgets are required for forms. And
forms are required for ModelAdmin.

It looks like this:

- in ``yourapp/autocomplete_light_registry.py``, register your channels with
  ``autocomplete_light.register()``,
- in ``urls.py``, do ``autocomplete_light.autodiscover()`` **before**
  ``admin.autodiscover()``.

ChannelRegistry
    Subclass of Python's dict type with registration/unregistration methods.

registry
    Instance of ChannelRegistry.

register
    Proxy registry.register.

autodiscover
    Find channels and fill registry.
"""

from django.db import models

from .channel import ChannelBase

__all__ = ('ChannelRegistry', 'registry', 'register', 'autodiscover')


class ChannelRegistry(dict):
    """
    Dict with some shortcuts to handle a registry of channels.
    """

    def __init__(self):
        self._models = {}

    def channel_for_model(self, model):
        """Return the channel class for a given model."""
        try:
            return self._models[model]
        except KeyError:
            return

    def unregister(self, name):
        """
        Unregister a channel.
        """
        channel = self[name]
        del self[name]

        if channel.model:
            del self._models[channel.model]

    def register(self, *args, **kwargs):
        """
        Proxy registry.register_model_channel() or registry.register_channel()
        if there is no apparent model for the channel.

        Example usages::

            # Will create and register SomeModelChannel, if SomeChannel.model
            # is None (which is the case by default):
            autocomplete_light.register(SomeModel)

            # Same but using SomeChannel as base:
            autocomplete_light.register(SomeModel, SomeChannel)

            # Register a channel without model, ensure that SomeChannel.model
            # is None (which is the default):
            autocomplete_light.register(SomeChannel)

            # As of 0.5, you may also pass attributes*, ie.:
            autocomplete_light.register(SomeModel, search_field='search_names',
                result_template='somemodel_result.html')

        You may pass attributes via kwargs, only if the registry creates a
        type:

        - if no channel class is passed,
        - or if the channel class has no model attribute,
        - and if the channel classs is not generic
        """

        channel = None
        model = None

        for arg in args:
            if issubclass(arg, models.Model):
                model = arg
            elif issubclass(arg, ChannelBase):
                channel = arg

        if channel and getattr(channel, 'model'):
            model = channel.model

        if model:
            self.register_model_channel(model, channel, **kwargs)
        else:
            self.register_channel(channel)

    def register_model_channel(self, model, channel=None, **kwargs):
        """
        Add a model to the registry, optionnaly with a given channel class.

        model
            The model class to register.

        channel
            The channel class to register the model with, default to
            ChannelBase.

        kwargs
            Extra attributes to set to the channel class, if created by this
            method.

        Three cases are possible:

        - specify model class and ModelNameChannel will be generated extending
          ChannelBase, with attribute model=model
        - specify a model and a channel class that does not have a model
          attribute, and a ModelNameChannel will be generated, with attribute
          model=model
        - specify a model and a channel class with a model attribute, and the
          channel is directly registered

        To keep things simple, the name of a channel is it's class name.
        """
        kwargs.update({'model': model})
        channel_name = kwargs.pop('channel_name', '%sChannel' % model.__name__)

        if channel is None:
            channel = type(channel_name, (ChannelBase,), kwargs)
        elif channel.model is None:
            channel = type(channel_name, (channel,), kwargs)

        self.register_channel(channel)
        self._models[channel.model] = channel

    def register_channel(self, channel):
        """
        Register a channel without model, like a generic channel.
        """
        self[channel.__name__] = channel


def _autodiscover(registry):<commit_msg>Documented and cleaned channel_name implementation<commit_after>"""
The registry module provides tools to maintain a registry of channels.

The first thing that should happen when django starts is registration of
channels. It should happen first, because channels are required for
autocomplete widgets. And autocomplete widgets are required for forms. And
forms are required for ModelAdmin.

It looks like this:

- in ``yourapp/autocomplete_light_registry.py``, register your channels with
  ``autocomplete_light.register()``,
- in ``urls.py``, do ``autocomplete_light.autodiscover()`` **before**
  ``admin.autodiscover()``.

ChannelRegistry
    Subclass of Python's dict type with registration/unregistration methods.

registry
    Instance of ChannelRegistry.

register
    Proxy registry.register.

autodiscover
    Find channels and fill registry.
"""

from django.db import models

from .channel import ChannelBase

__all__ = ('ChannelRegistry', 'registry', 'register', 'autodiscover')


class ChannelRegistry(dict):
    """
    Dict with some shortcuts to handle a registry of channels.
    """

    def __init__(self):
        self._models = {}

    def channel_for_model(self, model):
        """Return the channel class for a given model."""
        try:
            return self._models[model]
        except KeyError:
            return

    def unregister(self, name):
        """
        Unregister a channel.
        """
        channel = self[name]
        del self[name]

        if channel.model:
            del self._models[channel.model]

    def register(self, *args, **kwargs):
        """
        Proxy registry.register_model_channel() or registry.register_channel()
        if there is no apparent model for the channel.

        Example usages::

            # Will create and register SomeModelChannel, if SomeChannel.model
            # is None (which is the case by default):
            autocomplete_light.register(SomeModel)

            # Same but using SomeChannel as base:
            autocomplete_light.register(SomeModel, SomeChannel)

            # Register a channel without model, ensure that SomeChannel.model
            # is None (which is the default):
            autocomplete_light.register(SomeChannel)

            # As of 0.5, you may also pass attributes*, ie.:
            autocomplete_light.register(SomeModel, search_field='search_names',
                result_template='somemodel_result.html')

        You may pass attributes via kwargs, only if the registry creates a
        type:

        - if no channel class is passed,
        - or if the channel class has no model attribute,
        - and if the channel classs is not generic
        """

        channel = None
        model = None

        for arg in args:
            if issubclass(arg, models.Model):
                model = arg
            elif issubclass(arg, ChannelBase):
                channel = arg

        if channel and getattr(channel, 'model'):
            model = channel.model

        if model:
            self.register_model_channel(model, channel, **kwargs)
        else:
            self.register_channel(channel)

    def register_model_channel(self, model, channel=None, channel_name=None,
        **kwargs):
        """
        Add a model to the registry, optionnaly with a given channel class.

        model
            The model class to register.

        channel
            The channel class to register the model with, default to
            ChannelBase.

        channel_name
            Register channel under channel_name, default is ModelNameChannel.

        kwargs
            Extra attributes to set to the channel class, if created by this
            method.

        Three cases are possible:

        - specify model class and ModelNameChannel will be generated extending
          ChannelBase, with attribute model=model
        - specify a model and a channel class that does not have a model
          attribute, and a ModelNameChannel will be generated, with attribute
          model=model
        - specify a model and a channel class with a model attribute, and the
          channel is directly registered

        To keep things simple, the name of a channel is it's class name.
        """
        kwargs.update({'model': model})
        if channel_name is None:
            channel_name = '%sChannel' % model.__name__

        if channel is None:
            channel = type(channel_name, (ChannelBase,), kwargs)
        elif channel.model is None:
            channel = type(channel_name, (channel,), kwargs)

        self.register_channel(channel)
        self._models[channel.model] = channel

    def register_channel(self, channel):
        """
        Register a channel without model, like a generic channel.
        """
        self[channel.__name__] = channel


def _autodiscover(registry):
{'fraction_non_alphanumeric': 0.07228789622345434, 'fraction_numerical': 0.0004184538131603724, 'mean_word_length': 2.321751216122307, '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': '5500340', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2303, 'n_words': 905}