3
giD                 @   st   d dl Z d dlZd dlZd dlZd dlmZmZ d dl mZ d dlm	Z	m
Z
 ddlmZ G dd dZedd	ZdS )
    N)Counterdefaultdict)partial)AppRegistryNotReadyImproperlyConfigured   )	AppConfigc               @   s   e Zd ZdZf 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Zd0d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 )1Appsz
    A registry that stores the configuration of installed applications.

    It also keeps track of models, e.g. to provide reverse relations.
    c             C   s   |d kr t tjt dr tdtt| _i | _g | _	d | _
 | _| _tj | _tj | _d| _tt| _|d k	r~| j| d S )Nappsz+You must supply an installed_apps argument.F)hasattrsysmodules__name__RuntimeErrorr   dict
all_modelsapp_configsstored_app_configs
apps_readymodels_readyready	threadingEventready_eventRLock_lockloadinglist_pending_operationspopulate)selfinstalled_apps r"   R/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/apps/registry.py__init__   s    	



zApps.__init__Nc             C   s0  | j r
dS | j | j rdS | jr,tdd| _xR|D ]J}t|trL|}n
tj|}|j| jkrpt	d|j || j|j< | |_
q8W tdd | jj D }dd |j D }|rt	d	d
j| d| _x| jj D ]}|j  qW | j  d| _x| j D ]}|j   q W d| _ | jj  W dQ R X dS )z
        Load application configurations and models.

        Import each application module and then each model module.

        It is thread-safe and idempotent, but not reentrant.
        Nzpopulate() isn't reentrantTz0Application labels aren't unique, duplicates: %sc             s   s   | ]}|j V  qd S )N)name).0
app_configr"   r"   r#   	<genexpr>f   s    z Apps.populate.<locals>.<genexpr>c             S   s   g | ]\}}|d kr|qS )r   r"   )r&   r%   countr"   r"   r#   
<listcomp>h   s    z!Apps.populate.<locals>.<listcomp>z/Application names aren't unique, duplicates: %sz, )r   r   r   r   
isinstancer   createlabelr   r   r
   r   valuesmost_commonjoinr   import_modelsclear_cacher   get_app_configsr   set)r    r!   entryr'   counts
duplicatesr"   r"   r#   r   =   sD    





zApps.populatec             C   s$   | j s ddlm} |j tddS )z9Raise an exception if all apps haven't been imported yet.r   )settingszApps aren't loaded yet.N)r   Zdjango.confr8   ZINSTALLED_APPSr   )r    r8   r"   r"   r#   check_apps_ready   s    zApps.check_apps_readyc             C   s   | j stddS )z;Raise 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 )z:Import applications and return an iterable of app configs.)r9   r   r.   )r    r"   r"   r#   r3      s    zApps.get_app_configsc             C   sh   | j   y
| j| S  tk
rb   d| }x*| j D ]}|j|kr4|d|j 7 }P q4W t|Y nX dS )z
        Import applications and returns an app config for the given label.

        Raise LookupError if no application exists with this label.
        z!No installed app with label '%s'.z Did you mean '%s'?N)r9   r   KeyErrorr3   r%   r-   LookupError)r    	app_labelmessager'   r"   r"   r#   get_app_config   s    

zApps.get_app_config)maxsizeFc             C   s6   | j   g }x$| jj D ]}|j|j|| qW |S )a\  
        Return 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 that have been swapped out.

        Set the corresponding keyword argument to True to include such models.
        )r:   r   r.   extend
get_models)r    include_auto_createdinclude_swappedresultr'   r"   r"   r#   rB      s
    zApps.get_modelsTc             C   s\   |r| j   n| j  |dkr,|jd\}}| j|}| rN|jdkrN|j  |j||dS )a  
        Return the model matching the given app_label and model_name.

        As a shortcut, app_label may be in the form <app_label>.<model_name>.

        model_name is case-insensitive.

        Raise LookupError if no application exists with this label, or no
        model exists with this name in the application. Raise ValueError if
        called with a single argument that doesn't contain exactly one dot.
        N.)require_ready)r:   r9   splitr?   modelsr1   	get_model)r    r=   
model_namerG   r'   r"   r"   r#   rJ      s    

zApps.get_modelc             C   s   |j j}| j| }||krl|j|| jkrT|j|| jkrTtjd||f tdd ntd|||| |f |||< | 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.   )
stacklevelz7Conflicting '%s' models in application '%s': %s and %s.)_metarK   r   r   
__module__warningswarnRuntimeWarningr   do_pending_operationsr2   )r    r=   modelrK   Z
app_modelsr"   r"   r#   register_model   s    

zApps.register_modelc                s$   | j   t fdd| jj D S )z
        Check whether an application with this name exists in the registry.

        app_name is the full name of the app e.g. '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>)r9   anyr   r.   )r    rW   r"   )rW   r#   is_installed   s    zApps.is_installedc             C   sx   | j   g }xN| jj D ]@}|j|jr|t|jd }|dksN|d dkr|j| qW |rtt|dd dd S dS )a  
        Look for an app config containing a given object.

        object_name is the dotted Python path to the object.

        Return the app config for the inner application in case of nesting.
        Return None if the object isn't in any registered app config.
        N r   rF   c             S   s   t | j S )N)lenr%   )rV   r"   r"   r#   <lambda>  s    z0Apps.get_containing_app_config.<locals>.<lambda>)key)r9   r   r.   
startswithr%   r[   appendsorted)r    Zobject_name
candidatesr'   subpathr"   r"   r#   get_containing_app_config   s    	zApps.get_containing_app_configc             C   s0   | j | j|j }|dkr,td||f |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   getlowerr<   )r    r=   rK   rT   r"   r"   r#   get_registered_model  s
    zApps.get_registered_modelc             C   sR   xL| j ddD ]<}|jj}|r.||kr.|jjS |jjr|jj|kr|jjS qW d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.
        T)rD   N)rB   rN   swappedZ	swappabler-   )r    Z	to_stringrT   rg   r"   r"   r#   get_swappable_settings_name  s    z Apps.get_swappable_settings_namec                sn   t   dd | j D } j|s:tddj |  | jj| j  fdd| jj D | _| j	  dS )at  
        Restrict 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 in the sense that it doesn't trigger any imports.
        c             S   s   h | ]
}|j qS r"   )r%   )r&   r'   r"   r"   r#   	<setcomp>8  s    z*Apps.set_available_apps.<locals>.<setcomp>z?Available apps isn't a subset of installed apps, extra apps: %sz, c                s    i | ]\}}|j  kr||qS r"   )r%   )r&   r-   r'   )	availabler"   r#   
<dictcomp>@  s   z+Apps.set_available_apps.<locals>.<dictcomp>N)
r4   r3   issubset
ValueErrorr0   r   r_   r   itemsr2   )r    rj   	installedr"   )rj   r#   set_available_apps+  s    

zApps.set_available_appsc             C   s   | j j | _| j  dS )z/Cancel a previous call to set_available_apps().N)r   popr   r2   )r    r"   r"   r#   unset_available_appsG  s    zApps.unset_available_appsc             C   sP   | j std| jj| j i | _d | _ | _ | _| _ | j  | j	| dS )a  
        Enable 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 (e.g. 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   r_   r   r   r   r   r2   r   )r    ro   r"   r"   r#   set_installed_appsL  s    zApps.set_installed_appsc             C   s*   | j j | _d | _ | _| _| j  dS )z/Cancel a previous call to set_installed_apps().TN)r   rq   r   r   r   r   r2   )r    r"   r"   r#   unset_installed_appse  s    zApps.unset_installed_appsc             C   sH   | j j  | jrDx2| jj D ]$}x|j ddD ]}|jj  q.W qW dS )z|
        Clear all internal caches, for methods that alter the app registry.

        This is mostly used in tests.
        T)rC   N)rB   cache_clearr   r   r.   rN   Z_expire_cache)r    r'   rT   r"   r"   r#   r2   k  s
    
zApps.clear_cachec                sj   |s|  nZ|^} fdd | _ yj| }W n$ tk
r\   j| j  Y n
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).
        c                s    t  j| }j|f  d S )N)r   funclazy_model_operation)rT   Znext_function)apply_next_modelmore_modelsr    r"   r#   rx     s    z3Apps.lazy_model_operation.<locals>.apply_next_modelN)rv   rf   r<   r   r_   )r    functionZ
model_keysZ
next_modelZmodel_classr"   )rx   ry   r    r#   rw   {  s    
zApps.lazy_model_operationc             C   s4   |j j|j jf}x| jj|g D ]}|| q W d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)rN   r=   rK   r   rq   )r    rT   r]   rz   r"   r"   r#   rS     s    zApps.do_pending_operations)N)FF)NT)r   rO   __qualname____doc__r$   r   r9   r:   r3   r?   	functools	lru_cacherB   rJ   rU   rY   rc   rf   rh   rp   rr   rs   rt   r2   rw   rS   r"   r"   r"   r#   r	      s,   )
B

	'r	   )r!   )r}   r   r   rP   collectionsr   r   r   Zdjango.core.exceptionsr   r   configr   r	   r
   r"   r"   r"   r#   <module>   s      "