
 XTB                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l m Z m Z m Z d  d l m Z d  d l	 m
 Z
 m Z d  d l m Z d d l m Z Gd d	   d	 e  Z e d
 d  Z d S)    N)CounterOrderedDictdefaultdict)partial)AppRegistryNotReadyImproperlyConfigured)	lru_cache   )	AppConfigc               @   s9  e  Z d  Z d Z f  d d  Z d d d  Z d d   Z d	 d
   Z d d   Z d d   Z	 e
 j
 d d  d d d d   Z d d d  Z d d   Z d d   Z d d   Z d d   Z e
 j
 d d  d d    Z d d    Z d! d"   Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d S)-Appsz
    A registry that stores the configuration of installed applications.

    It also keeps track of models eg. to provide reverse-relations.
    c             C   s   | d  k r1 t  t j t d  r1 t d   n  t t  |  _ t   |  _ g  |  _	 d |  _
 |  _ |  _ t j   |  _ t t  |  _ | d  k	 r |  j |  n  d  S)Nappsz+You must supply an installed_apps argument.F)hasattrsysmodules__name__RuntimeErrorr   r   
all_modelsapp_configsstored_app_configs
apps_readymodels_readyready	threadingLock_locklist_pending_operationspopulate)selfinstalled_apps r    B/home/ubuntu/projects/ifolica/build/django/django/apps/registry.py__init__   s    "		zApps.__init__Nc             C   s  |  j  r d S|  j t|  j  r$ d S|  j r< t d   n  xm | D]e } t | t  ra | } n t j |  } | j |  j k r t d | j   n  | |  j | j <qC Wt	 d d   |  j j
   D  } d d   | j   D } | r	t d d	 j |    n  d
 |  _ x4 |  j j
   D]# } |  j | j } | j |  q"W|  j   d
 |  _ x |  j   D] } | j    qiWd
 |  _  Wd QXd S)z
        Loads application configurations and models.

        This method imports each application module and then each model module.

        It is thread safe and idempotent, but not reentrant.
        Nzpopulate() isn't reentrantz0Application labels aren't unique, duplicates: %sc             s   s   |  ] } | j  Vq d  S)N)name).0
app_configr    r    r!   	<genexpr>_   s    z Apps.populate.<locals>.<genexpr>c             S   s(   g  |  ] \ } } | d  k r |  q S)r	   r    )r$   r#   countr    r    r!   
<listcomp>a   s   	 z!Apps.populate.<locals>.<listcomp>z/Application names aren't unique, duplicates: %sz, T)r   r   r   r   
isinstancer
   createlabelr   r   valuesmost_commonjoinr   r   Zimport_modelsclear_cacher   get_app_configs)r   r   entryr%   counts
duplicatesr   r    r    r!   r   :   s@    	
				
	zApps.populatec             C   s   |  j  s t d   n  d S)zL
        Raises an exception if all apps haven't been imported yet.
        zApps aren't loaded yet.N)r   r   )r   r    r    r!   check_apps_readyw   s    	zApps.check_apps_readyc             C   s   |  j  s t d   n  d S)zN
        Raises an exception if all models haven't been imported yet.
        zModels aren't loaded yet.N)r   r   )r   r    r    r!   check_models_ready~   s    	zApps.check_models_readyc             C   s   |  j    |  j j   S)zN
        Imports applications and returns an iterable of app configs.
        )r4   r   r,   )r   r    r    r!   r0      s    
zApps.get_app_configsc             C   s   |  j    y |  j | SWnc t k
 r~ d | } x8 |  j   D]* } | j | k r@ | d | j 7} Pq@ q@ Wt |   Yn Xd S)z
        Imports applications and returns an app config for the given label.

        Raises LookupError if no application exists with this label.
        z!No installed app with label '%s'.z Did you mean '%s'?N)r4   r   KeyErrorr0   r#   r+   LookupError)r   	app_labelmessager%   r    r    r!   get_app_config   s    

zApps.get_app_configmaxsizeFc             C   sM   |  j    g  } x6 |  j j   D]% } | j t | j | |    q  W| S)a  
        Returns a list of all installed models.

        By default, the following models aren't included:

        - auto-created models for many-to-many relations without
          an explicit intermediate table,
        - models created to satisfy deferred attribute queries,
        - models that have been swapped out.

        Set the corresponding keyword argument to True to include such models.
        )r5   r   r,   extendr   
get_models)r   include_auto_createdinclude_swappedresultr%   r    r    r!   r=      s
    
#zApps.get_modelsc             C   sJ   |  j    | d k r. | j d  \ } } n  |  j |  j | j    S)a  
        Returns the model matching the given app_label and model_name.

        As a shortcut, this function also accepts a single argument in the
        form <app_label>.<model_name>.

        model_name is case-insensitive.

        Raises LookupError if no application exists with this label, or no
        model exists with this name in the application. Raises ValueError if
        called with a single argument that doesn't contain exactly one dot.
        N.)r5   splitr:   	get_modellower)r   r8   
model_namer    r    r!   rC      s    
zApps.get_modelc             C   s   | j  j } |  j | } | | k r | j | | j k rt | j | | j k rt t j d | | f t d d q t d | | | | | f   n  | | | <|  j	 |  |  j
   d  S)NzModel '%s.%s' was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.
stacklevel   z7Conflicting '%s' models in application '%s': %s and %s.)_metarE   r   r   
__module__warningswarnRuntimeWarningr   do_pending_operationsr/   )r   r8   modelrE   Z
app_modelsr    r    r!   register_model   s    

zApps.register_modelc                s0   |  j    t   f d d   |  j j   D  S)z
        Checks whether an application with this name exists in the registry.

        app_name is the full name of the app eg. 'django.contrib.admin'.
        c             3   s   |  ] } | j    k Vq d  S)N)r#   )r$   ac)app_namer    r!   r&      s    z$Apps.is_installed.<locals>.<genexpr>)r4   anyr   r,   )r   rQ   r    )rQ   r!   is_installed   s    
zApps.is_installedc             C   s   |  j    g  } xq |  j j   D]` } | j | j  r  | t | j  d  } | d k sm | d d k r | j |  q q  q  W| r t | d d d   d Sd S)a  
        Look for an app config containing a given object.

        object_name is the dotted Python path to the object.

        Returns the app config for the inner application in case of nesting.
        Returns None if the object isn't in any registered app config.
        N r   rA   keyc             S   s   t  |  j  S)N)lenr#   )rP   r    r    r!   <lambda>   s    z0Apps.get_containing_app_config.<locals>.<lambda>)r4   r   r,   
startswithr#   rV   appendsorted)r   Zobject_nameZ
candidatesr%   subpathr    r    r!   get_containing_app_config   s    	
zApps.get_containing_app_configc             C   sE   |  j  | j | j    } | d k rA t d | | f   n  | S)z
        Similar to get_model(), but doesn't require that an app exists with
        the given app_label.

        It's safe to call this method at import time, even while the registry
        is being populated.
        NzModel '%s.%s' not registered.)r   getrD   r7   )r   r8   rE   rN   r    r    r!   get_registered_model   s
    zApps.get_registered_modelc             C   sq   xj |  j  d d  D]V } | j j } | rA | | k rA | j j S| j j r | j j | k r | j j Sq Wd S)a  
        For a given model string (e.g. "auth.User"), return the name of the
        corresponding settings name if it refers to a swappable model. If the
        referred model is not swappable, return None.

        This method is decorated with lru_cache because it's performance
        critical when it comes to migrations. Since the swappable settings don't
        change after Django has loaded the settings, there is no reason to get
        the respective settings attribute over and over again.
        r?   TN)r=   rH   swappedZ	swappabler+   )r   Z	to_stringrN   r_   r    r    r!   get_swappable_settings_name  s    
z Apps.get_swappable_settings_namec                s   t       t  d d   |  j   D  }   j |  sZ t d d j   |    n  |  j j |  j  t   f d d   |  j j	   D  |  _ |  j
   d S)au  
        Restricts the set of installed apps used by get_app_config[s].

        available must be an iterable of application names.

        set_available_apps() must be balanced with unset_available_apps().

        Primarily used for performance optimization in TransactionTestCase.

        This method is safe is the sense that it doesn't trigger any imports.
        c             s   s   |  ] } | j  Vq d  S)N)r#   )r$   r%   r    r    r!   r&   (  s    z*Apps.set_available_apps.<locals>.<genexpr>z?Available apps isn't a subset of installed apps, extra apps: %sz, c             3   s0   |  ]& \ } } | j    k r | | f Vq d  S)N)r#   )r$   r+   r%   )	availabler    r!   r&   1  s   	N)setr0   issubset
ValueErrorr.   r   rY   r   r   itemsr/   )r   ra   	installedr    )ra   r!   set_available_apps  s    zApps.set_available_appsc             C   s    |  j  j   |  _ |  j   d S)zB
        Cancels a previous call to set_available_apps().
        N)r   popr   r/   )r   r    r    r!   unset_available_apps6  s    zApps.unset_available_appsc             C   si   |  j  s t d   n  |  j j |  j  t   |  _ d |  _ |  _ |  _  |  j   |  j	 |  d S)a  
        Enables a different set of installed apps for get_app_config[s].

        installed must be an iterable in the same format as INSTALLED_APPS.

        set_installed_apps() must be balanced with unset_installed_apps(),
        even if it exits with an exception.

        Primarily used as a receiver of the setting_changed signal in tests.

        This method may trigger new imports, which may add new models to the
        registry of all imported models. They will stay in the registry even
        after unset_installed_apps(). Since it isn't possible to replay
        imports safely (eg. that could lead to registering listeners twice),
        models are registered when they're imported and never removed.
        zApp registry isn't ready yet.FN)
r   r   r   rY   r   r   r   r   r/   r   )r   rf   r    r    r!   set_installed_apps=  s    	
zApps.set_installed_appsc             C   s7   |  j  j   |  _ d |  _ |  _ |  _ |  j   d S)zB
        Cancels a previous call to set_installed_apps().
        TN)r   rh   r   r   r   r   r/   )r   r    r    r!   unset_installed_appsV  s    zApps.unset_installed_appsc             C   sa   |  j  j   |  j r] xD |  j j   D]0 } x' | j  d d  D] } | j j   q? Wq& Wn  d S)z}
        Clears all internal caches, for methods that alter the app registry.

        This is mostly used in tests.
        r>   TN)r=   cache_clearr   r   r,   rH   Z_expire_cache)r   r%   rN   r    r    r!   r/   ^  s
    	zApps.clear_cachec                s   | s |   n | d | d d  }      f d d     |   _  y  j |   } Wn& t k
 r  j | j    Yn X  |  d S)a`  
        Take a function and a number of ("app_label", "modelname") tuples, and
        when all the corresponding models have been imported and registered,
        call the function with the model classes as its arguments.

        The function passed to this method must accept exactly n models as
        arguments, where n=len(model_keys).
        r   r	   Nc                s&   t    j |   }  j |   d  S)N)r   funclazy_model_operation)rN   Znext_function)apply_next_modelmore_modelsr   r    r!   ro     s    z3Apps.lazy_model_operation.<locals>.apply_next_model)rm   r^   r7   r   rY   )r   functionZ
model_keysZ
next_modelZmodel_classr    )ro   rp   r   r!   rn   n  s    

	zApps.lazy_model_operationc             C   sF   | j  j | j  j f } x' |  j j | g   D] } | |  q. Wd S)z
        Take a newly-prepared model and pass it to each function waiting for
        it. This is called at the very end of `Apps.register_model()`.
        N)rH   r8   rE   r   rh   )r   rN   rU   rq   r    r    r!   rM     s    zApps.do_pending_operations)r   rI   __qualname____doc__r"   r   r4   r5   r0   r:   r   r=   rC   rO   rS   r\   r^   r`   rg   ri   rj   rk   r/   rn   rM   r    r    r    r!   r      s,   &=	'r   r   )r   r   rJ   collectionsr   r   r   	functoolsr   Zdjango.core.exceptionsr   r   Zdjango.utilsr   configr
   objectr   r   r    r    r    r!   <module>   s    