<s> import imp <EOL> import threading <EOL> from importlib import import_module <EOL> from django . apps import apps <EOL> from django . contrib . staticfiles import finders <EOL> from django . conf import settings <EOL> from webassets . env import ( <EOL> BaseEnvironment , ConfigStorage , Resolver , url_prefix_join ) <EOL> from django_assets . glob import Globber , has_magic <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class DjangoConfigStorage ( ConfigStorage ) : <EOL> _mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def _transform_key ( self , key ) : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if getattr ( settings , '<STR_LIT>' , None ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> if key . lower ( ) == '<STR_LIT:url>' : <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if getattr ( settings , '<STR_LIT>' , None ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> return self . _mapping . get ( key . lower ( ) , key . upper ( ) ) <EOL> def __contains__ ( self , key ) : <EOL> return hasattr ( settings , self . _transform_key ( key ) ) <EOL> def __getitem__ ( self , key ) : <EOL> if self . __contains__ ( key ) : <EOL> value = self . _get_deprecated ( key ) <EOL> if value is not None : <EOL> return value <EOL> return getattr ( settings , self . _transform_key ( key ) ) <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % <EOL> self . _transform_key ( key ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not self . _set_deprecated ( key , value ) : <EOL> setattr ( settings , self . _transform_key ( key ) , value ) <EOL> def __delitem__ ( self , key ) : <EOL> self . __setitem__ ( key , None ) <EOL> class StorageGlobber ( Globber ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , storage ) : <EOL> self . storage = storage <EOL> def isdir ( self , path ) : <EOL> return False <EOL> def islink ( self , path ) : <EOL> return False <EOL> def listdir ( self , path ) : <EOL> directories , files = self . storage . listdir ( path ) <EOL> return directories + files <EOL> def exists ( self , path ) : <EOL> try : <EOL> return self . storage . exists ( path ) <EOL> except NotImplementedError : <EOL> return False <EOL> class DjangoResolver ( Resolver ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def use_staticfiles ( self ) : <EOL> return settings . ASSETS_DEBUG and '<STR_LIT>' in settings . INSTALLED_APPS <EOL> def glob_staticfiles ( self , item ) : <EOL> for finder in finders . get_finders ( ) : <EOL> if hasattr ( finder , '<STR_LIT>' ) : <EOL> storages = finder . storages . values ( ) <EOL> elif hasattr ( finder , '<STR_LIT>' ) : <EOL> storages = [ finder . storage ] <EOL> else : <EOL> continue <EOL> for storage in storages : <EOL> globber = StorageGlobber ( storage ) <EOL> for file in globber . glob ( item ) : <EOL> yield storage . path ( file ) <EOL> def search_for_source ( self , ctx , item ) : <EOL> if not self . use_staticfiles : <EOL> return Resolver . search_for_source ( self , ctx , item ) <EOL> if has_magic ( item ) : <EOL> return list ( self . glob_staticfiles ( item ) ) <EOL> else : <EOL> f = finders . find ( item ) <EOL> if f is not None : <EOL> return f <EOL> raise IOError ( <EOL> "<STR_LIT>" % item ) <EOL> def resolve_source_to_url ( self , ctx , filepath , item ) : <EOL> if not self . use_staticfiles : <EOL> return Resolver . resolve_source_to_url ( self , ctx , filepath , item ) <EOL> return url_prefix_join ( ctx . url , item ) <EOL> class DjangoEnvironment ( BaseEnvironment ) : <EOL> """<STR_LIT>""" <EOL> config_storage_class = DjangoConfigStorage <EOL> resolver_class = DjangoResolver <EOL> env = None <EOL> env_lock = threading . RLock ( ) <EOL> def get_env ( ) : <EOL> with env_lock : <EOL> global env <EOL> if env is None : <EOL> env = DjangoEnvironment ( ) <EOL> autoload ( ) <EOL> return env <EOL> def reset ( ) : <EOL> global env <EOL> env = None <EOL> def register ( * a , ** kw ) : <EOL> return get_env ( ) . register ( * a , ** kw ) <EOL> _ASSETS_LOADED = False <EOL> def autoload ( ) : <EOL> """<STR_LIT>""" <EOL> global _ASSETS_LOADED <EOL> if _ASSETS_LOADED : <EOL> return False <EOL> from django . conf import settings <EOL> for app in apps . get_app_configs ( ) : <EOL> try : <EOL> app_path = app . path <EOL> except AttributeError : <EOL> continue <EOL> try : <EOL> imp . find_module ( '<STR_LIT>' , [ app_path ] ) <EOL> except ImportError : <EOL> continue <EOL> import_module ( "<STR_LIT>" . format ( app . name ) ) <EOL> for module in getattr ( settings , '<STR_LIT>' , [ ] ) : <EOL> import_module ( "<STR_LIT:%s>" % module ) <EOL> _ASSETS_LOADED = True </s>
