o
    بfA&                     @   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                   @      e Zd ZdS )ErrorN__name__
__module____qualname__ r   r   G/var/www/dscantool/venv/lib/python3.10/site-packages/django/db/utils.pyr          r   c                   @   r
   )InterfaceErrorNr   r   r   r   r   r      r   r   c                   @   r
   )DatabaseErrorNr   r   r   r   r   r      r   r   c                   @   r
   )	DataErrorNr   r   r   r   r   r      r   r   c                   @   r
   )OperationalErrorNr   r   r   r   r   r      r   r   c                   @   r
   )IntegrityErrorNr   r   r   r   r   r   #   r   r   c                   @   r
   )InternalErrorNr   r   r   r   r   r   '   r   r   c                   @   r
   )ProgrammingErrorNr   r   r   r   r   r   +   r   r   c                   @   r
   )NotSupportedErrorNr   r   r   r   r   r   /   r   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   
   || _ 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__9   s   
zDatabaseErrorWrapper.__init__c                 C   s   d S Nr   r   r   r   r   	__enter__A   s   zDatabaseErrorWrapper.__enter__c              	   C   sp   |d u rd S t ttttttttf	D ]$}t	| j
j|j}t||r5||j }|t tfv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__D   s(   

zDatabaseErrorWrapper.__exit__c                    s    fdd}|S )Nc                     s6     | i |W  d    S 1 sw   Y  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+   r0   r   r   r   r   r   3   s    r   c              
   C   s   | dkrd} zt d|  W S  tyJ } z1tttjd }dd t|gD }| dd |D vrEtt	t
|}td| d	|f | d
}~ww )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 vr|qS )>   Zpostgresql_psycopg2basedummyr   ).0_nameispkgr   r   r   
<listcomp>s   s    z load_backend.<locals>.<listcomp>c                 S   s   g | ]}d | qS )zdjango.db.backends.%sr   )r5   br   r   r   r9   w       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_backendd   s*   
rG   c                   @   r
   )ConnectionDoesNotExistNr   r   r   r   r   rH      r   rH   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S )z{
        databases is an optional dictionary of database definitions (structured
        like settings.DATABASES).
        N)
_databasesr   _connections)r   	databasesr   r   r   r      s   zConnectionHandler.__init__c                 C   sb   | j d u r	tj| _ | j i krtddii| _ t| j v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.)rJ   r   Z	DATABASESDEFAULT_DB_ALIASr   r!   r   r   r   rL      s   


zConnectionHandler.databasesc                 C   s   z| j | }W n ty   td| w |dd |dd |dd |d dks1|d s5d|d< |d	d
 |di  |dd dD ]}||d qId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
AUTOCOMMITTrM   rN   zdjango.db.backends.ZCONN_MAX_AGEr   OPTIONSZ	TIME_ZONEN)NAMEUSERZPASSWORDZHOSTZPORT rL   KeyErrorrH   
setdefault)r   aliasconnZsettingr   r   r   ensure_defaults   s    z!ConnectionHandler.ensure_defaultsc                 C   sP   z| j | }W n ty   td| w |di }dD ]}||d qdS )zY
        Make sure the test settings are available in the 'TEST' sub-dictionary.
        rP   TEST)CHARSETZ	COLLATIONrR   ZMIRRORNrU   )r   rX   rY   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 )NrM   )	hasattrrK   r$   rZ   r^   rL   rG   ZDatabaseWrappersetattr)r   rX   dbbackendrY   r   r   r   __getitem__   s   


zConnectionHandler.__getitem__c                 C   s   t | j|| d S r    )r`   rK   )r   r]   valuer   r   r   __setitem__      zConnectionHandler.__setitem__c                 C   s   t | j| d S r    )delattrrK   )r   r]   r   r   r   __delitem__   s   zConnectionHandler.__delitem__c                 C   s
   t | jS r    )iterrL   r!   r   r   r   __iter__   s   
zConnectionHandler.__iter__c                    s    fdd D S )Nc                    s   g | ]} | qS r   r   )r5   rX   r!   r   r   r9      r;   z)ConnectionHandler.all.<locals>.<listcomp>r   r!   r   r!   r   all   rf   zConnectionHandler.allc              	   C   s:   | D ]}zt | j|}W n	 ty   Y qw |  qd S r    )r$   rK   AttributeErrorclose)r   rX   
connectionr   r   r   	close_all   s   
zConnectionHandler.close_allr    )r   r   r   r   r   rL   rZ   r^   rc   re   rh   rj   rk   ro   r   r   r   r   rI      s    

rI   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   r   )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 u r	tj| _ g }| j D ]}t|trt| }n|}|| q|S r    )rq   r   ZDATABASE_ROUTERS
isinstancer=   r   append)r   rr   rrouterr   r   r   rr      s   


zConnectionRouter.routersc                    s    fdd}|S )Nc              	      st   d }| j D ]!}zt| }W n	 ty   Y qw ||fi |}|r&|  S q|d}|d ur8|jjr8|jjS tS )Ninstance)rr   r$   rl   get_statera   rO   )r   modelhintsZ	chosen_dbrv   methodrw   actionr   r   	_route_db   s   

z0ConnectionRouter._router_func.<locals>._route_dbr   )r~   r   r   r}   r   _router_func   s   zConnectionRouter._router_funcdb_for_readdb_for_writec              	   K   s\   | j D ]"}z|j}W n	 ty   Y qw |||fi |}|d ur%|  S q|jj|jjkS r    )rr   allow_relationrl   ry   ra   )r   Zobj1obj2r{   rv   r|   allowr   r   r   r     s   

zConnectionRouter.allow_relationc              	   K   sP   | j D ]"}z|j}W n	 ty   Y qw |||fi |}|d ur%|  S qdS r#   )rr   allow_migraterl   )r   ra   	app_labelr{   rv   r|   r   r   r   r   r     s   

zConnectionRouter.allow_migratec                 C   s   | j ||jj|jj|dS )N)
model_namerz   )r   _metar   r   )r   ra   rz   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   )r5   rz   ra   r   r   r   r9   3  s    z:ConnectionRouter.get_migratable_models.<locals>.<listcomp>)Z
get_models)r   Z
app_configra   r   modelsr   r   r   get_migratable_models0  s   z&ConnectionRouter.get_migratable_modelsr    )F)r   r   r   r   r   rr   r   r   r   r   r   r   r   r   r   r   r   rp      s    

rp   ) r@   	importlibr   pathlibr   	threadingr   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   rO   ZDJANGO_VERSION_PICKLE_KEY	Exceptionr   r   r   r   r   r   r   r   r   r   rG   rH   rI   rp   r   r   r   r   <module>   s0    1\