<commit_before>import imp
from django.conf import settings
from webassets.env import Environment
from webassets.importlib import import_module


__all__ = ('register',)


class DjangoEnvironment(Environment):
    """For Django, we need to redirect all the configuration values this
    object holds to Django's own settings object.

    We do this by hooking into __getattribute__ and __setattribute__,
    rather than reimplementing the get_foo/set_foo() methods, which means
    we won't have to reimplement any validation the parent class may do.
    """

    _mapping = {
        'debug': 'ASSETS_DEBUG',
        'cache': 'ASSETS_CACHE',
        'updater': 'ASSETS_UPDATER',
        'auto_create': 'ASSETS_AUTO_CREATE',
        'expire': 'ASSETS_EXPIRE',
        'directory': 'MEDIA_ROOT',
        'url': 'MEDIA_URL',
    }

    def __init__(self):
        # Have the parent initialize the default values
        super(DjangoEnvironment, self).__init__(None, None)

        # Then, from this point on, redirect various attributes
        # to the Django settings object.
        self.django_settings = settings

    def get_config(self, key, default=None):
        return getattr(self.django_settings, key, default)

    def __getattribute__(self, name):
        if name == '_mapping':
            return super(DjangoEnvironment, self).__getattribute__(name)
        if not name in self._mapping:
            return super(DjangoEnvironment, self).__getattribute__(name)

        django_key = self._mapping[name]
        if not hasattr(self.django_settings, django_key):
            return super(DjangoEnvironment, self).__getattribute__(name)

        return getattr(self.django_settings, django_key)

    def __setattr__(self, name, value):
        if not hasattr(self, 'django_settings'):
            # The parent's __init__ is probably settings defaults
            return super(DjangoEnvironment, self).__setattr__(name, value)
        if not name in self._mapping:
            return super(DjangoEnvironment, self).__setattr__(name, value)

        setattr(self.django_settings, self._mapping[name], value)


# Django has a global state, a global configuration, and so we need a
# global instance of a asset environment.
env = None

def get_env():
    global env
    if env is None:
        env = DjangoEnvironment()
    return env

def reset():
    global env
    env = None

# The user needn't know about the env though, we can expose the
# relevant functionality directly. This is also for backwards-compatibility
# with times where ``django-assets`` was a standalone library.
def register(*a, **kw):
    return get_env().register(*a, **kw)


# Finally, we'd like to autoload the ``assets`` module of each Django.
try:
    from django.utils.importlib import import_module
except ImportError:
    # django-1.0 compatibility
    import warnings
    warnings.warn('django-assets may not be compatible with Django versions '
                  'earlier than 1.1', DeprecationWarning)
    def import_module(app):<commit_msg>Make sure the autoloading code is actually executed.<commit_after>import imp
from django.conf import settings
from webassets.env import Environment
from webassets.importlib import import_module


__all__ = ('register',)


class DjangoEnvironment(Environment):
    """For Django, we need to redirect all the configuration values this
    object holds to Django's own settings object.

    We do this by hooking into __getattribute__ and __setattribute__,
    rather than reimplementing the get_foo/set_foo() methods, which means
    we won't have to reimplement any validation the parent class may do.
    """

    _mapping = {
        'debug': 'ASSETS_DEBUG',
        'cache': 'ASSETS_CACHE',
        'updater': 'ASSETS_UPDATER',
        'auto_create': 'ASSETS_AUTO_CREATE',
        'expire': 'ASSETS_EXPIRE',
        'directory': 'MEDIA_ROOT',
        'url': 'MEDIA_URL',
    }

    def __init__(self):
        # Have the parent initialize the default values
        super(DjangoEnvironment, self).__init__(None, None)

        # Then, from this point on, redirect various attributes
        # to the Django settings object.
        self.django_settings = settings

    def get_config(self, key, default=None):
        return getattr(self.django_settings, key, default)

    def __getattribute__(self, name):
        if name == '_mapping':
            return super(DjangoEnvironment, self).__getattribute__(name)
        if not name in self._mapping:
            return super(DjangoEnvironment, self).__getattribute__(name)

        django_key = self._mapping[name]
        if not hasattr(self.django_settings, django_key):
            return super(DjangoEnvironment, self).__getattribute__(name)

        return getattr(self.django_settings, django_key)

    def __setattr__(self, name, value):
        if not hasattr(self, 'django_settings'):
            # The parent's __init__ is probably settings defaults
            return super(DjangoEnvironment, self).__setattr__(name, value)
        if not name in self._mapping:
            return super(DjangoEnvironment, self).__setattr__(name, value)

        setattr(self.django_settings, self._mapping[name], value)


# Django has a global state, a global configuration, and so we need a
# global instance of a asset environment.
env = None

def get_env():
    global env
    if env is None:
        env = DjangoEnvironment()

        # Load application's ``assets``  modules. We need to do this in
        # a delayed fashion, since the main django_assets module imports
        # this, and the application ``assets`` modules we load will import
        # ``django_assets``, thus giving us a classic circular dependency
        # issue.
        autoload()
    return env

def reset():
    global env
    env = None

# The user needn't know about the env though, we can expose the
# relevant functionality directly. This is also for backwards-compatibility
# with times where ``django-assets`` was a standalone library.
def register(*a, **kw):
    return get_env().register(*a, **kw)


# Finally, we'd like to autoload the ``assets`` module of each Django.
try:
    from django.utils.importlib import import_module
except ImportError:
    # django-1.0 compatibility
    import warnings
    warnings.warn('django-assets may not be compatible with Django versions '
                  'earlier than 1.1', DeprecationWarning)
    def import_module(app):
{'fraction_non_alphanumeric': 0.08429237947122861, 'fraction_numerical': 0.001244167962674961, 'mean_word_length': 3.157078215901745, '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': '85712', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1704, 'n_words': 613}