<s> from __future__ import with_statement <EOL> from threading import Lock <EOL> from django . conf import settings as django_settings <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> _load_lock = Lock ( ) <EOL> _by_local_name = { } <EOL> _by_application_name = { } <EOL> def _require_loaded_apps ( f ) : <EOL> def g ( * args , ** kwargs ) : <EOL> if not _loaded : <EOL> _load_apps ( ) <EOL> return f ( * args , ** kwargs ) <EOL> return g <EOL> def _load_app ( filter_func ) : <EOL> with _load_lock : <EOL> for application in django_settings . APPLICATIONS : <EOL> if not filter_func ( application ) : <EOL> continue <EOL> app = application . get ( ) <EOL> _by_local_name [ app . local_name ] = app <EOL> if not app . application_name in _by_application_name : <EOL> _by_application_name [ app . application_name ] = [ ] <EOL> _by_application_name [ app . application_name ] . append ( app ) <EOL> def app_by_local_name ( local_name ) : <EOL> try : <EOL> return _by_local_name [ local_name ] <EOL> except KeyError : <EOL> _load_app ( lambda app : app . local_name == local_name ) <EOL> return _by_local_name [ local_name ] <EOL> def app_by_application_name ( application_name ) : <EOL> try : <EOL> return _by_application_name [ application_name ] [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> _load_app ( lambda app : app . application_name == application_name ) <EOL> return _by_application_name [ application_name ] [ <NUM_LIT:0> ] <EOL> def apps_by_application_name ( application_name ) : <EOL> try : <EOL> return _by_application_name [ application_name ] <EOL> except KeyError : <EOL> _load_app ( lambda app : app . application_name == application_name ) <EOL> return _by_application_name [ application_name ] <EOL> def get_app ( application_name = None , local_name = None ) : <EOL> if local_name : <EOL> return app_by_local_name ( local_name ) <EOL> else : <EOL> return app_by_application_name ( application_name ) <EOL> def all_apps ( ) : <EOL> return [ app . get ( ) for app in django_settings . APPLICATIONS ] <EOL> def has_app_by_application_name ( application_name ) : <EOL> try : <EOL> app_by_application_name ( application_name ) <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> return True <EOL> def has_app_by_local_name ( local_name ) : <EOL> try : <EOL> app_by_local_name ( local_name ) <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> return True <EOL> def has_app ( application_name = None , local_name = None ) : <EOL> if local_name : <EOL> return has_app_by_local_name ( local_name ) <EOL> else : <EOL> return has_app_by_application_name ( application_name ) <EOL> class Applications ( object ) : <EOL> def __getattr__ ( self , key ) : <EOL> return app_by_local_name ( key ) <EOL> __getitem__ = __getattr__ <EOL> applications = Applications ( ) </s>
