<commit_before>import functools
import hashlib
from importlib import import_module

from django.conf import settings
from django.utils import six

from channels import channel_backends, DEFAULT_CHANNEL_BACKEND


def consumer(*channels, **kwargs):
    """
    Decorator that registers a function as a consumer.
    """
    # We can't put a kwarg after *args in py2
    alias = kwargs.get("alias", DEFAULT_CHANNEL_BACKEND)
    # Upconvert if you just pass in a string
    if isinstance(channels, six.string_types):
        channels = [channels]
    # Get the channel 
    channel_backend = channel_backends[alias]
    # Return a function that'll register whatever it wraps
    def inner(func):
        channel_backend.registry.add_consumer(func, channels)
        return func
    return inner


# TODO: Sessions, auth

def send_channel_session(func):
    """
    Provides a session-like object called "channel_session" to consumers
    as a message attribute that will auto-persist across consumers with<commit_msg>Initial django session and auth decorators<commit_after>import functools
import hashlib
from importlib import import_module

from django.conf import settings
from django.utils import six
from django.contrib import auth

from channels import channel_backends, DEFAULT_CHANNEL_BACKEND


def consumer(*channels, **kwargs):
    """
    Decorator that registers a function as a consumer.
    """
    # We can't put a kwarg after *args in py2
    alias = kwargs.get("alias", DEFAULT_CHANNEL_BACKEND)
    # Upconvert if you just pass in a string
    if isinstance(channels, six.string_types):
        channels = [channels]
    # Get the channel 
    channel_backend = channel_backends[alias]
    # Return a function that'll register whatever it wraps
    def inner(func):
        channel_backend.registry.add_consumer(func, channels)
        return func
    return inner


# TODO: Sessions, auth

def http_session(func):
    """
    Wraps a HTTP or WebSocket consumer (or any consumer of messages
    that provides a "COOKIES" or "GET" attribute) to provide a "session"
    attribute that behaves like request.session; that is, it's hung off of
    a per-user session key that is saved in a cookie or passed as the
    "session_key" GET parameter.

    It won't automatically create and set a session cookie for users who
    don't have one - that's what SessionMiddleware is for, this is a simpler
    read-only version for more low-level code.

    If a user does not have a session we can inflate, the "session" attribute will
    be None, rather than an empty session you can write to.
    """
    @functools.wraps(func)
    def inner(*args, **kwargs):
        if "COOKIES" not in kwargs and "GET" not in kwargs:
            print kwargs
            raise ValueError("No COOKIES or GET sent to consumer; this decorator can only be used on messages containing at least one.")
        # Make sure there's a session key
        session_key = None
        if "GET" in kwargs:
            session_key = kwargs['GET'].get("session_key")
        if "COOKIES" in kwargs and session_key is None:
            session_key = kwargs['COOKIES'].get(settings.SESSION_COOKIE_NAME)
        # Make a session storage
        if session_key:
            session_engine = import_module(settings.SESSION_ENGINE)
            session = session_engine.SessionStore(session_key=session_key)
        else:
            session = None
        kwargs['session'] = session
        # Run the consumer
        result = func(*args, **kwargs)
        # Persist session if needed (won't be saved if error happens)
        if session is not None and session.modified:
            session.save()
        return result
    return inner


def http_django_auth(func):
    """
    Wraps a HTTP or WebSocket consumer (or any consumer of messages
    that provides a "COOKIES" attribute) to provide both a "session"
    attribute and a "user" attibute, like AuthMiddleware does.

    This runs http_session() to get a session to hook auth off of.
    If the user does not have a session cookie set, both "session"
    and "user" will be None.
    """
    @http_session
    @functools.wraps(func)
    def inner(*args, **kwargs):
        # If we didn't get a session, then we don't get a user
        if kwargs['session'] is None:
            kwargs['user'] = None
        # Otherwise, be a bit naughty and make a fake Request with just
        # a "session" attribute (later on, perhaps refactor contrib.auth to
        # pass around session rather than request)
        else:
            fake_request = type("FakeRequest", (object, ), {"session": kwargs['session']})
            kwargs['user'] = auth.get_user(fake_request)
        # Run the consumer
        return func(*args, **kwargs)
    return inner


def send_channel_session(func):
    """
    Provides a session-like object called "channel_session" to consumers
    as a message attribute that will auto-persist across consumers with
{'fraction_non_alphanumeric': 0.0716158967117208, 'fraction_numerical': 0.00040346984062941297, 'mean_word_length': 2.985530546623794, 'pattern_counts': {'":': 1, '<': 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': '732517', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1315, 'n_words': 547}