<s> import os <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . files . storage import default_storage , Storage , FileSystemStorage <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . functional import empty , memoize , LazyObject <EOL> from django . utils . importlib import import_module <EOL> from django . utils . _os import safe_join <EOL> from django . contrib . staticfiles import utils <EOL> from django . contrib . staticfiles . storage import AppStaticStorage <EOL> _finders = SortedDict ( ) <EOL> class BaseFinder ( object ) : <EOL> """<STR_LIT>""" <EOL> def find ( self , path , all = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def list ( self , ignore_patterns ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class FileSystemFinder ( BaseFinder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , apps = None , * args , ** kwargs ) : <EOL> self . locations = [ ] <EOL> self . storages = SortedDict ( ) <EOL> if not isinstance ( settings . STATICFILES_DIRS , ( list , tuple ) ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for root in settings . STATICFILES_DIRS : <EOL> if isinstance ( root , ( list , tuple ) ) : <EOL> prefix , root = root <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> if os . path . abspath ( settings . STATIC_ROOT ) == os . path . abspath ( root ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if ( prefix , root ) not in self . locations : <EOL> self . locations . append ( ( prefix , root ) ) <EOL> for prefix , root in self . locations : <EOL> filesystem_storage = FileSystemStorage ( location = root ) <EOL> filesystem_storage . prefix = prefix <EOL> self . storages [ root ] = filesystem_storage <EOL> super ( FileSystemFinder , self ) . __init__ ( * args , ** kwargs ) <EOL> def find ( self , path , all = False ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> for prefix , root in self . locations : <EOL> matched_path = self . find_location ( root , path , prefix ) <EOL> if matched_path : <EOL> if not all : <EOL> return matched_path <EOL> matches . append ( matched_path ) <EOL> return matches <EOL> def find_location ( self , root , path , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> if prefix : <EOL> prefix = '<STR_LIT>' % ( prefix , os . sep ) <EOL> if not path . startswith ( prefix ) : <EOL> return None <EOL> path = path [ len ( prefix ) : ] <EOL> path = safe_join ( root , path ) <EOL> if os . path . exists ( path ) : <EOL> return path <EOL> def list ( self , ignore_patterns ) : <EOL> """<STR_LIT>""" <EOL> for prefix , root in self . locations : <EOL> storage = self . storages [ root ] <EOL> for path in utils . get_files ( storage , ignore_patterns ) : <EOL> yield path , storage <EOL> class AppDirectoriesFinder ( BaseFinder ) : <EOL> """<STR_LIT>""" <EOL> storage_class = AppStaticStorage <EOL> def __init__ ( self , apps = None , * args , ** kwargs ) : <EOL> self . apps = [ ] <EOL> self . storages = SortedDict ( ) <EOL> if apps is None : <EOL> apps = settings . INSTALLED_APPS <EOL> for app in apps : <EOL> app_storage = self . storage_class ( app ) <EOL> if os . path . isdir ( app_storage . location ) : <EOL> self . storages [ app ] = app_storage <EOL> if app not in self . apps : <EOL> self . apps . append ( app ) <EOL> super ( AppDirectoriesFinder , self ) . __init__ ( * args , ** kwargs ) <EOL> def list ( self , ignore_patterns ) : <EOL> """<STR_LIT>""" <EOL> for storage in self . storages . itervalues ( ) : <EOL> if storage . exists ( '<STR_LIT>' ) : <EOL> for path in utils . get_files ( storage , ignore_patterns ) : <EOL> yield path , storage <EOL> def find ( self , path , all = False ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> for app in self . apps : <EOL> match = self . find_in_app ( app , path ) <EOL> if match : <EOL> if not all : <EOL> return match <EOL> matches . append ( match ) <EOL> return matches <EOL> def find_in_app ( self , app , path ) : <EOL> """<STR_LIT>""" <EOL> storage = self . storages . get ( app , None ) <EOL> if storage : <EOL> if storage . prefix : <EOL> prefix = '<STR_LIT>' % ( storage . prefix , os . sep ) <EOL> if not path . startswith ( prefix ) : <EOL> return None <EOL> path = path [ len ( prefix ) : ] <EOL> if storage . exists ( path ) : <EOL> matched_path = storage . path ( path ) <EOL> if matched_path : <EOL> return matched_path <EOL> class BaseStorageFinder ( BaseFinder ) : <EOL> """<STR_LIT>""" <EOL> storage = None <EOL> def __init__ ( self , storage = None , * args , ** kwargs ) : <EOL> if storage is not None : <EOL> self . storage = storage <EOL> if self . storage is None : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ ) <EOL> if not isinstance ( self . storage , ( Storage , LazyObject ) ) : <EOL> self . storage = self . storage ( ) <EOL> super ( BaseStorageFinder , self ) . __init__ ( * args , ** kwargs ) <EOL> def find ( self , path , all = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . storage . path ( '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> pass <EOL> else : <EOL> if self . storage . exists ( path ) : <EOL> match = self . storage . path ( path ) <EOL> if all : <EOL> match = [ match ] <EOL> return match <EOL> return [ ] <EOL> def list ( self , ignore_patterns ) : <EOL> """<STR_LIT>""" <EOL> for path in utils . get_files ( self . storage , ignore_patterns ) : <EOL> yield path , self . storage <EOL> class DefaultStorageFinder ( BaseStorageFinder ) : <EOL> """<STR_LIT>""" <EOL> storage = default_storage <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DefaultStorageFinder , self ) . __init__ ( * args , ** kwargs ) <EOL> base_location = getattr ( self . storage , '<STR_LIT>' , empty ) <EOL> if not base_location : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ ) <EOL> def find ( path , all = False ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> for finder in get_finders ( ) : <EOL> result = finder . find ( path , all = all ) <EOL> if not all and result : <EOL> return result <EOL> if not isinstance ( result , ( list , tuple ) ) : <EOL> result = [ result ] <EOL> matches . extend ( result ) <EOL> if matches : <EOL> return matches <EOL> return all and [ ] or None <EOL> def get_finders ( ) : <EOL> for finder_path in settings . STATICFILES_FINDERS : <EOL> yield get_finder ( finder_path ) <EOL> def _get_finder ( import_path ) : <EOL> """<STR_LIT>""" <EOL> module , attr = import_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> mod = import_module ( module ) <EOL> except ImportError , e : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % <EOL> ( module , e ) ) <EOL> try : <EOL> Finder = getattr ( mod , attr ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( module , attr ) ) <EOL> if not issubclass ( Finder , BaseFinder ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % <EOL> ( Finder , BaseFinder ) ) <EOL> return Finder ( ) <EOL> get_finder = memoize ( _get_finder , _finders , <NUM_LIT:1> ) </s>
