a
    ܌xd                     @   s|   d Z ddlZ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dlmZmZmZ ddlmZ G d	d
 d
eZdS )z.
sqldns.py

Prints Data Source Name on stdout
    N)settings)BaseCommandCommandError)color_style)DEFAULT_DB_ALIAS)SQLITE_ENGINESPOSTGRESQL_ENGINESMYSQL_ENGINES)RemovedInNextVersionWarningc                       sJ   e Zd ZdZdZdZ fddZdd Zdd	 Zd
d Z	dddZ
  ZS )Commandz1Prints DSN on stdout, as specified in settings.pyFTc                    s|   t  | |jddddtdd |jdtdt d	 |jd
dddd dd |jddddddd |jddddddd d S )Nz-Rz--routerstorerouterzPUse this router-database other then default (deprecated: use --database instead))actiondestdefaulthelpz
--databasezGNominates a database to run command for. Defaults to the "%s" database.)r   r   z-sz--stylestylez,DSN format style: keyvalue, uri, pgpass, allz-az--all
store_trueallFz Show DSN for all database routesz-qz--quietquietzQuiet mode only show DSN)superadd_argumentsadd_argumentr   )selfparser	__class__ d/var/www/html/Ranjet/env/lib/python3.9/site-packages/django_extensions/management/commands/sqldsn.pyr      s2    zCommand.add_argumentsc                 O   s   t  | _|d }|r tj }n0|d g}|d tkrPtjdtdd |d g}t	|D ](\}}|dkrtt
jd | || qXd S )	Nr   databaser   z2--router is deprecated. You should use --database.   )
stacklevelr   
)r   r   r   	DATABASESkeysr   warningswarnr
   	enumeratesysstdoutwriteshow_dsn)r   argsoptionsZall_databasesZ	databasesir   r   r   r   handle3   s    

zCommand.handlec              
   C   s(  t j|}|d }|d }|d u r0td| |d}|d}|d}|d}	|d}
|d	}g }|tv r|d
|	 nZ|tv r|| |
||	|| n8|t	v r|
| j|
||	|||d n|| jd |stj| jd||f  |D ]}tjd| q
d S )Nr   r   zUnknown database %sZENGINEUSERZPASSWORDNAMEZHOSTZPORTz{})	dsn_stylez#Unknown database, cant generate DSNz(DSN for database '%s' with engine '%s':
z{}
)r   r#   getr   r   appendformatr	   _mysqlr   extend_postgresqlr   ERRORr(   r)   r*   Z	SQL_TABLE)r   r   r-   Zdbinfor   r2   Zenginedbuserdbpassdbnamedbhostdbportdsnoutputr   r   r   r+   D   s2    






zCommand.show_dsnc                 C   s&   d}|d ur|d7 }| |||||S )Nz.host="{0}", db="{2}", user="{3}", passwd="{4}"z, port="{1}")r5   )r   r=   r>   r<   r:   r;   dsnstrr   r   r   r6   e   s    zCommand._mysqlNc           	      C   s   g }|du s|dks|dkrHd}|dur0|d7 }| |||||| |dv r|d}|durd|d7 }| |||||| |d	v rd
}| |j|rdj||dn||||d |dv r| dtttd|||||g |S )z`PostgreSQL psycopg2 driver accepts two syntaxes

        Plus a string for .pgpass file
        Nr   Zkeyvaluez1host='{0}' dbname='{2}' user='{3}' password='{4}'z port='{1}')r   kwargsz6host='{0}', database='{2}', user='{3}', password='{4}'z, port='{1}')r   uriz,postgresql://{user}:{password}@{host}/{name}z{host}:{port})hostport)rD   nameuserpassword)r   Zpgpass:)r4   r5   joinmapstrfilter)	r   r=   r>   r<   r:   r;   r2   r?   rA   r   r   r   r8   m   sD    &zCommand._postgresql)N)__name__
__module____qualname__r   Zrequires_system_checksZcan_import_settingsr   r/   r+   r6   r8   __classcell__r   r   r   r   r      s   !r   )__doc__r(   r%   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.core.management.colorr   Z	django.dbr   Zdjango_extensions.settingsr   r   r	   Z#django_extensions.utils.deprecationr
   r   r   r   r   r   <module>   s   