3
(h                 @   sT   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 dZ
G dd deZdS )	    N)import_module)AppRegistryNotReadyImproperlyConfigured)upath)module_has_submodulemodelsc               @   s^   e Zd ZdZdd Zdd Zdd Zedd	 Zd
d Z	dd Z
dddZdd Zdd ZdS )	AppConfigzH
    Class representing a Django application and its configuration.
    c             C   sb   || _ || _t| ds&|jdd | _t| ds<| jj | _t| dsR| j|| _d | _	d | _
d S )Nlabel.   verbose_namepath)namemodulehasattr
rpartitionr	   titler   _path_from_moduler   models_moduler   )selfapp_name
app_module r   6/tmp/pip-install-q3hcpn_q/Django/django/apps/config.py__init__   s    


zAppConfig.__init__c             C   s   d| j j| jf S )Nz<%s: %s>)	__class____name__r	   )r   r   r   r   __repr__3   s    zAppConfig.__repr__c             C   s   t t|dg }t|dkrLt|dd}|dk	r@tjj|g}nt t|}t|dkrjtd||f n|s|td|f t|d S )z;Attempt to determine app's filesystem path from its module.__path__   __file__NzThe app module %r has multiple filesystem locations (%r); you must configure this app with an AppConfig subclass with a 'path' class attribute.zThe app module %r has no filesystem location, you must configure this app with an AppConfig subclass with a 'path' class attribute.r   )	listgetattrlenosr   dirnamesetr   r   )r   r   pathsfilenamer   r   r   r   6   s    
zAppConfig._path_from_modulec       	   #   C   s  yt |}W n. tk
r:   d}|jd\}}}|s6 Y n8X y
|j}W n tk
r`   | ||S X |jd\}}}t |}yt||} W n( tk
r   |dkrt | n Y nX t| tstd| y
| j	}W n  tk
r   td| Y nX t |}| ||S )zU
        Factory that creates an app config from an entry in INSTALLED_APPS.
        Nr
   z#'%s' isn't a subclass of AppConfig.z"'%s' must supply a name attribute.)
r   ImportErrorr   Zdefault_app_configAttributeErrorr"   
issubclassr   r   r   )	clsentryr   Zmod_path_Zcls_namemodr   r   r   r   r   createQ   s:    




zAppConfig.createc             C   s   | j dkrtd| j dS )zJ
        Raises an exception if models haven't been imported yet.
        Nz.Models for app '%s' haven't been imported yet.)r   r   r	   )r   r   r   r   check_models_ready   s    
zAppConfig.check_models_readyc             C   sB   | j   y| j|j  S  tk
r<   td| j|f Y nX dS )z
        Returns the model with the given case-insensitive model_name.

        Raises LookupError if no model exists with this name.
        z#App '%s' doesn't have a '%s' model.N)r1   r   lowerKeyErrorLookupErrorr	   )r   Z
model_namer   r   r   	get_model   s    zAppConfig.get_modelFc             c   sF   | j   x8| jj D ]*}|jjr(| r(q|jjr8| r8q|V  qW dS )a  
        Returns an iterable of 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.
        Keyword arguments aren't documented; they're a private API.
        N)r1   r   valuesZ_metaZauto_createdZswapped)r   Zinclude_auto_createdZinclude_swappedmodelr   r   r   
get_models   s    zAppConfig.get_modelsc             C   s.   || _ t| jtr*d| jtf }t|| _d S )Nz%s.%s)r   r   r   MODELS_MODULE_NAMEr   r   r   )r   Z
all_modelsZmodels_module_namer   r   r   import_models   s    zAppConfig.import_modelsc             C   s   dS )zT
        Override this method in subclasses to run code when Django starts.
        Nr   )r   r   r   r   ready   s    zAppConfig.readyN)FF)r   
__module____qualname____doc__r   r   r   classmethodr0   r1   r5   r8   r:   r;   r   r   r   r   r      s   #B
r   )r$   	importlibr   Zdjango.core.exceptionsr   r   Zdjango.utils._osr   Zdjango.utils.module_loadingr   r9   objectr   r   r   r   r   <module>   s   