ó
ø¢TQc           @   s  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d d	 l Z d d	 l Z d d	 l Z d Z d e f d „  ƒ  YZ e ƒ  Z e j Z e j Z e j Z e j Z e j Z e j Z e j Z e j Z d	 S(   s?   Utilities for loading models and the modules that contain them.iÿÿÿÿ(   t   settings(   t   ImproperlyConfigured(   t
   SortedDict(   t   import_module(   t   module_has_submodule(   t   upath(   t   sixNt   get_appst   get_appt
   get_modelst	   get_modelt   register_modelst   load_appt   app_cache_readyt   AppCachec           B   sÑ   e  Z d  Z e d e ƒ  d i  d e ƒ  d i  d e d i  d g  d d	 d
 i  ƒ 	Z d „  Z d „  Z d „  Z	 e d „ Z
 d „  Z d „  Z e d „ Z d „  Z d e e e e d „ Z e e d „ Z d „  Z RS(   s”   
    A cache that stores installed applications and their models. Used to
    provide reverse-relations and for app introspection (e.g. admin).
    t	   app_storet
   app_labelst
   app_modelst
   app_errorst   loadedt   handledt	   postponedt   nesting_leveli    t   _get_models_cachec         C   s   |  j  |  _ d  S(   N(   t   _AppCache__shared_statet   __dict__(   t   self(    (    s1   ../Django//lib/python/django/db/models/loading.pyt   __init__0   s    c         C   s©   |  j  r d St j ƒ  z€ |  j  r' d Sx6 t j D]+ } | |  j k rL q1 n  |  j | t ƒ q1 W|  j s– x |  j	 D] } |  j | ƒ qs Wt |  _  n  Wd t j
 ƒ  Xd S(   sÝ   
        Fill in all the cache information. This method is threadsafe, in the
        sense that every caller will see the same state upon return, and if the
        cache is already initialised, it does no work.
        N(   R   t   impt   acquire_lockR    t   INSTALLED_APPSR   R   t   TrueR   R   t   release_lock(   R   t   app_name(    (    s1   ../Django//lib/python/django/db/models/loading.pyt	   _populate3   s    	
		c         C   s   | j  j d ƒ d S(   s<   
        Return app_label for given models module.

        t   .iþÿÿÿ(   t   __name__t   split(   R   t   app_mod(    (    s1   ../Django//lib/python/django/db/models/loading.pyt
   _label_forP   s    c         C   sß   d |  j | <|  j d 7_ t | ƒ } y t d | ƒ } WnP t k
 r |  j d 8_ t | d ƒ sm d S| r‡ |  j j | ƒ d S‚  n X|  j d 8_ | |  j k rÛ t	 |  j ƒ |  j | <| |  j
 |  j | ƒ <n  | S(   sm   
        Loads the app with the provided fully qualified name, and returns the
        model module.
        i   s   .modelst   modelsN(   t   NoneR   R   R   t   ImportErrorR   R   t   appendR   t   lenR   R'   (   R   R!   t   can_postponet
   app_moduleR(   (    (    s1   ../Django//lib/python/django/db/models/loading.pyR   W   s$    	c         C   s   |  j  S(   s¿   
        Returns true if the model cache is fully populated.

        Useful for code that wants to cache the results of get_models() for
        themselves once it is safe to do so.
        (   R   (   R   (    (    s1   ../Django//lib/python/django/db/models/loading.pyR   {   s    c         C   s]   |  j  ƒ  g  |  j j ƒ  D] \ } } | | f ^ q } | j ƒ  g  | D] } | d ^ qI S(   s<   Returns a list of all installed modules that contain models.i   (   R"   R   t   itemst   sort(   R   t   kt   vt   appst   elt(    (    s1   ../Django//lib/python/django/db/models/loading.pyR   „   s    
.
c         C   s©   |  j  ƒ  t j ƒ  zƒ xl t j D]a } | | j d ƒ d k r! |  j | t ƒ } | d k r{ | rh d St	 d | ƒ ‚ q‚ | Sq! q! Wt	 d | ƒ ‚ Wd t j
 ƒ  Xd S(   sŸ   
        Returns the module containing the models for the given app_label. If
        the app has no models in it and 'emptyOK' is True, returns None.
        R#   iÿÿÿÿs0   App with label %s is missing a models.py module.s$   App with label %s could not be foundN(   R"   R   R   R    R   R%   R   t   FalseR)   R   R    (   R   t	   app_labelt   emptyOKR!   t   mod(    (    s1   ../Django//lib/python/django/db/models/loading.pyR      s    

c         C   s   |  j  ƒ  |  j S(   s:   Returns the map of known problems with the INSTALLED_APPS.(   R"   R   (   R   (    (    s1   ../Django//lib/python/django/db/models/loading.pyt   get_app_errors¤   s    
c            s*  | ˆ ˆ  | ˆ f } y |  j  | SWn t k
 r7 n X|  j ƒ  | r‡ | |  j k r~ |  j j |  j | ƒ t ƒ  ƒ g } qÙ g  } nR | rÇ g  t j	 |  j
 ƒ D] } |  j j | t ƒ  ƒ ^ q  } n t j |  j ƒ } g  }	 x7 | D]/ }
 |	 j ‡ ‡  ‡ f d †  |
 j ƒ  Dƒ ƒ qæ W|	 |  j  | <|	 S(   sr  
        Given a module containing models, returns a list of the models.
        Otherwise returns a list of all installed models.

        By default, auto-created models (i.e., m2m models without an
        explicit intermediate table) are not included. However, if you
        specify include_auto_created=True, they will be.

        By default, models created to satisfy deferred attribute
        queries are *not* included in the list of models. However, if
        you specify include_deferred, they will be.

        By default, models that aren't part of installed apps will *not*
        be included in the list of models. However, if you specify
        only_installed=False, they will be.

        By default, models that have been swapped out will *not* be
        included in the list of models. However, if you specify
        include_swapped, they will be.
        c         3   sK   |  ]A } | j  s ˆ r | j j s, ˆ r | j j s? ˆ  r | Vq d  S(   N(   t	   _deferredt   _metat   auto_createdt   swapped(   t   .0t   model(   t   include_swappedt   include_deferredt   include_auto_created(    s1   ../Django//lib/python/django/db/models/loading.pys	   <genexpr>Õ   s    (   R   t   KeyErrorR"   R   R   t   getR'   R   R   t   iterkeysR   t
   itervaluest   extendt   values(   R   R&   RB   RA   t   only_installedR@   t	   cache_keyt   app_listR6   t
   model_listt   app(    (   RA   R@   RB   s1   ../Django//lib/python/django/db/models/loading.pyR	   ©   s*    
	7'c         C   sQ   | r |  j  ƒ  n  | r, | |  j k r, d S|  j j | t ƒ  ƒ j | j ƒ  ƒ S(   s•   
        Returns the model matching the given app_label and case-insensitive
        model_name.

        Returns None if no model is found.
        N(   R"   R   R)   R   RD   R   t   lower(   R   R6   t
   model_namet
   seed_cacheRI   (    (    s1   ../Django//lib/python/django/db/models/loading.pyR
   Ý   s
    c         G   så   xÑ | D]É } | j  j j ƒ  } |  j j | t ƒ  ƒ } | | k rÆ t j j t	 t
 j | j j ƒ ƒ } t j j t	 t
 j | | j j ƒ ƒ } t j j | ƒ d t j j | ƒ d k rÆ q qÆ n  | | | <q W|  j j ƒ  d S(   sB   
        Register a set of models as belonging to an app.
        i    N(   R;   t   object_nameRN   R   t
   setdefaultR   t   ost   patht   abspathR   t   syst   modulest
   __module__t   __file__t   splitextR   t   clear(   R   R6   R(   R?   RO   t
   model_dictt   fname1t   fname2(    (    s1   ../Django//lib/python/django/db/models/loading.pyR   ë   s    %),	N(   R$   RX   t   __doc__t   dictR   R5   R   R   R"   R'   R   R   R   R   R9   R)   R   R	   R
   R   (    (    (    s1   ../Django//lib/python/django/db/models/loading.pyR      s0   						$				3(   s   get_appss   get_apps
   get_modelss	   get_models   register_modelss   load_apps   app_cache_ready(   R_   t   django.confR    t   django.core.exceptionsR   t   django.utils.datastructuresR   t   django.utils.importlibR   t   django.utils.module_loadingR   t   django.utils._osR   t   django.utilsR   R   RV   RS   t   __all__t   objectR   t   cacheR   R   R9   R	   R
   R   R   R   (    (    (    s1   ../Django//lib/python/django/db/models/loading.pyt   <module>   s,    ï								