<commit_before>"""
A replacement for ``django.conf.urls.include`` that takes a decorator,
or an iterable of view decorators as the first argument and applies them, in
reverse order, to all views in the included urlconf.
"""

from __future__ import unicode_literals
from builtins import object, str
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver

try:
    from importlib import import_module
except ImportError:
    # For python 2.6
    from django.utils.importlib import import_module


class DecoratedPatterns(object):
    """
    A wrapper for an urlconf that applies a decorator to all its views.
    """
    def __init__(self, urlconf_name, decorators):
        self.urlconf_name = urlconf_name
        try:
            iter(decorators)
        except TypeError:
            decorators = [decorators]
        self.decorators = decorators
        if not isinstance(urlconf_name, str):
            self._urlconf_module = self.urlconf_name
        else:
            self._urlconf_module = None

    def decorate_pattern(self, pattern):
        if isinstance(pattern, RegexURLResolver):
            regex = pattern.regex.pattern
            urlconf_module = pattern.urlconf_name
            default_kwargs = pattern.default_kwargs
            namespace = pattern.namespace
            app_name = pattern.app_name
            urlconf = DecoratedPatterns(urlconf_module, self.decorators)
            decorated = RegexURLResolver(
                regex, urlconf, default_kwargs,
                app_name, namespace
            )
        else:
            callback = pattern.callback
            for decorator in reversed(self.decorators):
                callback = decorator(callback)
            decorated = RegexURLPattern(
                pattern.regex.pattern,
                callback,
                pattern.default_args,
                pattern.name
            )
        return decorated

    def _get_urlconf_module(self):
        if self._urlconf_module is None:
            self._urlconf_module = import_module(self.urlconf_name)
        return self._urlconf_module
    urlconf_module = property(_get_urlconf_module)

    def _get_urlpatterns(self):
        try:
            patterns = self.urlconf_module.urlpatterns
        except AttributeError:
            patterns = self.urlconf_module
        return [self.decorate_pattern(pattern) for pattern in patterns]
    urlpatterns = property(_get_urlpatterns)

    def __getattr__(self, name):
        return getattr(self.urlconf_module, name)<commit_msg>Import urlconf_module in __init__, avoids infinite recursion<commit_after>"""
A replacement for ``django.conf.urls.include`` that takes a decorator,
or an iterable of view decorators as the first argument and applies them, in
reverse order, to all views in the included urlconf.
"""

from __future__ import unicode_literals
from builtins import object, str
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver

try:
    from importlib import import_module
except ImportError:
    # For python 2.6
    from django.utils.importlib import import_module


class DecoratedPatterns(object):
    """
    A wrapper for an urlconf that applies a decorator to all its views.
    """
    def __init__(self, urlconf_name, decorators):
        try:
            iter(decorators)
        except TypeError:
            decorators = [decorators]
        self.decorators = decorators
        if isinstance(urlconf_name, str):
            self.urlconf_module = import_module(urlconf_name)
        else:
            self.urlconf_module = urlconf_name

    def decorate_pattern(self, pattern):
        if isinstance(pattern, RegexURLResolver):
            regex = pattern.regex.pattern
            urlconf_module = pattern.urlconf_name
            default_kwargs = pattern.default_kwargs
            namespace = pattern.namespace
            app_name = pattern.app_name
            urlconf = DecoratedPatterns(urlconf_module, self.decorators)
            decorated = RegexURLResolver(
                regex, urlconf, default_kwargs,
                app_name, namespace
            )
        else:
            callback = pattern.callback
            for decorator in reversed(self.decorators):
                callback = decorator(callback)
            decorated = RegexURLPattern(
                pattern.regex.pattern,
                callback,
                pattern.default_args,
                pattern.name
            )
        return decorated

    def _get_urlpatterns(self):
        try:
            patterns = self.urlconf_module.urlpatterns
        except AttributeError:
            patterns = self.urlconf_module
        return [self.decorate_pattern(pattern) for pattern in patterns]
    urlpatterns = property(_get_urlpatterns)

    def __getattr__(self, name):
        return getattr(self.urlconf_module, name)
{'fraction_non_alphanumeric': 0.0611178126256534, 'fraction_numerical': 0.0008041817450743868, 'mean_word_length': 2.393587994542974, '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': '2116637', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1278, 'n_words': 350}