U
    a'                     @   s:  d dl 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	Zd
ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd dZdd  ZG d!d" d"eZG d#d$ d$ZG d%d& d&ZdS )'    N)import_module)Path)Local)settings)ImproperlyConfigured)cached_property)import_stringdefaultZ_django_versionc                   @   s   e Zd ZdS )ErrorN__name__
__module____qualname__ r   r   ~/home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/db/utils.pyr
      s   r
   c                   @   s   e Zd ZdS )InterfaceErrorNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )DatabaseErrorNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )	DataErrorNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )OperationalErrorNr   r   r   r   r   r       s   r   c                   @   s   e Zd ZdS )IntegrityErrorNr   r   r   r   r   r   $   s   r   c                   @   s   e Zd ZdS )InternalErrorNr   r   r   r   r   r   (   s   r   c                   @   s   e Zd ZdS )ProgrammingErrorNr   r   r   r   r   r   ,   s   r   c                   @   s   e Zd ZdS )NotSupportedErrorNr   r   r   r   r   r   0   s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )DatabaseErrorWrapperz~
    Context manager and decorator that reraises backend-specific database
    exceptions using Django's common wrappers.
    c                 C   s
   || _ dS )zx
        wrapper is a database wrapper.

        It must have a Database attribute defining PEP-249 exceptions.
        N)wrapper)selfr   r   r   r   __init__:   s    zDatabaseErrorWrapper.__init__c                 C   s   d S Nr   r   r   r   r   	__enter__B   s    zDatabaseErrorWrapper.__enter__c              	   C   sp   |d krd S t ttttttttf	D ]H}t	| j
j|j}t||r"||j }|t tfkr^d| j
_|||q"d S NT)r   r   r   r   r   r   r   r   r
   getattrr   ZDatabaser   
issubclassargsZerrors_occurredwith_traceback)r   exc_type	exc_value	tracebackZdj_exc_typeZdb_exc_typeZdj_exc_valuer   r   r   __exit__E   s$    

zDatabaseErrorWrapper.__exit__c                    s    fdd}|S )Nc               
      s&     | |W  5 Q R  S Q R X d S r   r   )r#   kwargsfuncr   r   r   inner_   s    z,DatabaseErrorWrapper.__call__.<locals>.innerr   )r   r+   r,   r   r*   r   __call__\   s    zDatabaseErrorWrapper.__call__N)r   r   r   __doc__r   r   r(   r-   r   r   r   r   r   4   s
   r   c              
   C   s   | dkrd} zt d|  W S  tk
r } zhtttjd }dd t|gD }| dd |D krtt	t
|}td| d	|f |n W 5 d
}~X Y nX d
S )z
    Return a database backend's "base" module given a fully qualified database
    backend name, or raise an error if it doesn't exist.
    z&django.db.backends.postgresql_psycopg2zdjango.db.backends.postgresqlz%s.basebackendsc                 S   s"   g | ]\}}}|r|d kr|qS )>   dummybasepostgresql_psycopg2r   ).0_nameispkgr   r   r   
<listcomp>t   s    z load_backend.<locals>.<listcomp>c                 S   s   g | ]}d | qS )zdjango.db.backends.%sr   )r3   br   r   r   r7   x   s     zg%r isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    %sz, N)r   ImportErrorstrr   __file__parentpkgutiliter_modulesmapreprsortedr   join)backend_nameZe_userZbackend_dirZbuiltin_backendsZbackend_reprsr   r   r   load_backende   s&    
rD   c                   @   s   e Zd ZdS )ConnectionDoesNotExistNr   r   r   r   r   rE      s   rE   c                   @   sb   e Zd ZdddZe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d ZdS )ConnectionHandlerNc                 C   s   || _ tdd| _dS )z{
        databases is an optional dictionary of database definitions (structured
        like settings.DATABASES).
        T)Zthread_criticalN)
_databasesr   _connections)r   	databasesr   r   r   r      s    zConnectionHandler.__init__c                 C   sb   | j d krtj| _ | j i kr*tddii| _ t| j kr@tdt | j t i kr\d| j t d< | j S )NENGINEdjango.db.backends.dummyz You must define a '%s' database.)rG   r   Z	DATABASESDEFAULT_DB_ALIASr   r   r   r   r   rI      s    

 
zConnectionHandler.databasesc                 C   s   z| j | }W n  tk
r.   td| Y nX |dd |dd |dd |d dksh|d spd|d< |d	d
 |di  |dd dD ]}||d qdS )z}
        Put the defaults into the settings dictionary for a given connection
        where no settings is provided.
        The connection %s doesn't existZATOMIC_REQUESTSFZ
AUTOCOMMITTrJ   rK   zdjango.db.backends.ZCONN_MAX_AGEr   OPTIONSZ	TIME_ZONEN)NAMEUSERZPASSWORDZHOSTZPORT rI   KeyErrorrE   
setdefault)r   aliasconnZsettingr   r   r   ensure_defaults   s    z!ConnectionHandler.ensure_defaultsc                 C   sV   z| j | }W n  tk
r.   td| Y nX |di }dD ]}||d q@dS )zY
        Make sure the test settings are available in the 'TEST' sub-dictionary.
        rM   ZTEST)CHARSETZ	COLLATIONrO   ZMIRRORNrR   )r   rU   rV   Ztest_settingskeyr   r   r   prepare_test_settings   s    z'ConnectionHandler.prepare_test_settingsc                 C   s`   t | j|rt| j|S | | | | | j| }t|d }|||}t| j|| |S )NrJ   )	hasattrrH   r!   rW   rZ   rI   rD   ZDatabaseWrappersetattr)r   rU   dbbackendrV   r   r   r   __getitem__   s    


zConnectionHandler.__getitem__c                 C   s   t | j|| d S r   )r\   rH   )r   rY   valuer   r   r   __setitem__   s    zConnectionHandler.__setitem__c                 C   s   t | j| d S r   )delattrrH   )r   rY   r   r   r   __delitem__   s    zConnectionHandler.__delitem__c                 C   s
   t | jS r   )iterrI   r   r   r   r   __iter__   s    zConnectionHandler.__iter__c                    s    fdd D S )Nc                    s   g | ]} | qS r   r   )r3   rU   r   r   r   r7      s     z)ConnectionHandler.all.<locals>.<listcomp>r   r   r   r   r   all   s    zConnectionHandler.allc              	   C   s@   | D ]6}zt | j|}W n tk
r0   Y qY nX |  qd S r   )r!   rH   AttributeErrorclose)r   rU   
connectionr   r   r   	close_all   s    
zConnectionHandler.close_all)N)r   r   r   r   r   rI   rW   rZ   r_   ra   rc   re   rf   rj   r   r   r   r   rF      s   

rF   c                   @   s\   e Zd ZdddZedd Zdd ZedZed	Zd
d Z	dd Z
dd ZdddZdS )ConnectionRouterNc                 C   s
   || _ dS )zT
        If routers is not specified, default to settings.DATABASE_ROUTERS.
        N)_routers)r   routersr   r   r   r      s    zConnectionRouter.__init__c                 C   sJ   | j d krtj| _ g }| j D ](}t|tr6t| }n|}|| q|S r   )rl   r   ZDATABASE_ROUTERS
isinstancer:   r   append)r   rm   rrouterr   r   r   rm      s    


zConnectionRouter.routersc                    s    fdd}|S )Nc              	      sr   d }| j D ]@}zt| }W n tk
r0   Y q
X ||f|}|r
|  S q
|d}|d k	rn|jjrn|jjS tS )Ninstance)rm   r!   rg   get_stater]   rL   )r   modelhintsZ	chosen_dbrq   methodrr   actionr   r   	_route_db   s    


z0ConnectionRouter._router_func.<locals>._route_dbr   )ry   rz   r   rx   r   _router_func   s    zConnectionRouter._router_funcdb_for_readdb_for_writec              	   K   sZ   | j D ]B}z
|j}W n tk
r(   Y qX |||f|}|d k	r|  S q|jj|jjkS r   )rm   allow_relationrg   rt   r]   )r   Zobj1obj2rv   rq   rw   allowr   r   r   r~     s    


zConnectionRouter.allow_relationc              	   K   sR   | j D ]F}z
|j}W n tk
r,   Y qY nX |||f|}|d k	r|  S qdS r    )rm   allow_migraterg   )r   r]   	app_labelrv   rq   rw   r   r   r   r   r      s    



zConnectionRouter.allow_migratec                 C   s   | j ||jj|jj|dS )N)
model_nameru   )r   Z_metar   r   )r   r]   ru   r   r   r   allow_migrate_model.  s    z$ConnectionRouter.allow_migrate_modelFc                    s    |j |d} fdd|D S )z8Return app models allowed to be migrated on provided db.)include_auto_createdc                    s   g | ]}  |r|qS r   )r   )r3   ru   r]   r   r   r   r7   9  s      z:ConnectionRouter.get_migratable_models.<locals>.<listcomp>)Z
get_models)r   Z
app_configr]   r   modelsr   r   r   get_migratable_models6  s    z&ConnectionRouter.get_migratable_models)N)F)r   r   r   r   r   rm   r{   r|   r}   r~   r   r   r   r   r   r   r   rk      s   

rk   ) r=   	importlibr   pathlibr   Zasgiref.localr   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   rL   ZDJANGO_VERSION_PICKLE_KEY	Exceptionr
   r   r   r   r   r   r   r   r   r   rD   rE   rF   rk   r   r   r   r   <module>   s.   1a