B
    \$                 @   s   d Z 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	 G d
d de
Zdd Zdd Zdd Zdd Zdd ZdS )a  Provides the :class:`~sqlalchemy.engine.url.URL` class which encapsulates
information about a database connection specification.

The URL object is created automatically when
:func:`~sqlalchemy.engine.create_engine` is called with a string
argument; alternatively, the URL is a public-facing construct which can
be used directly and is also accepted directly by ``create_engine()``.
    N   )Dialect   )exc)util)plugins)registryc               @   s   e Zd ZdZd!ddZd"ddZdd	 Zd
d Zdd Zdd Z	dd Z
edd Zejdd Zdd Zdd Zdd Zdd Zdd Zg fdd ZdS )#URLaX  
    Represent the components of a URL used to connect to a database.

    This object is suitable to be passed directly to a
    :func:`~sqlalchemy.create_engine` call.  The fields of the URL are parsed
    from a string by the :func:`.make_url` function.  the string
    format of the URL is an RFC-1738-style string.

    All initialization parameters are available as public attributes.

    :param drivername: the name of the database backend.
      This name will correspond to a module in sqlalchemy/databases
      or a third party plug-in.

    :param username: The user name.

    :param password: database password.

    :param host: The name of the host.

    :param port: The port number.

    :param database: The database name.

    :param query: A dictionary of options to be passed to the
      dialect and/or the DBAPI upon connect.

    Nc             C   sF   || _ || _|| _|| _|d k	r,t|| _nd | _|| _|p>i | _d S )N)
drivernameusernamepassword_originalhostintportdatabasequery)selfr
   r   passwordr   r   r   r    r   4lib/python3.7/site-packages/sqlalchemy/engine/url.py__init__8   s    
zURL.__init__Tc                s    j d } jd k	rN|t j7 } jd k	rF|d|r8dnt j 7 }|d7 } jd k	r|d jkrr|d j 7 }n
| j7 } jd k	r|dt j 7 } jd k	r|d j 7 } jrt	 j}|
  |dd fd	d
|D  7 }|S )Nz://:z***@z[%s]/?&c             3   s0   | ](}t  j| D ]}d ||f V  qqdS )z%s=%sN)r   to_listr   ).0kelement)r   r   r   	<genexpr>c   s   z$URL.__to_string__.<locals>.<genexpr>)r
   r   _rfc_1738_quoter   r   r   strr   r   listsortjoin)r   hide_passwordskeysr   )r   r   __to_string__M   s,    









zURL.__to_string__c             C   s   | j ddS )NF)r&   )r)   )r   r   r   r   __str__i   s    zURL.__str__c             C   s   |   S )N)r)   )r   r   r   r   __repr__l   s    zURL.__repr__c             C   s   t t| S )N)hashr"   )r   r   r   r   __hash__o   s    zURL.__hash__c             C   s^   t |to\| j|jko\| j|jko\| j|jko\| j|jko\| j|jko\| j|jko\| j|jkS )N)	
isinstancer	   r
   r   r   r   r   r   r   )r   otherr   r   r   __eq__r   s    
z
URL.__eq__c             C   s
   | |k S )Nr   )r   r/   r   r   r   __ne__~   s    z
URL.__ne__c             C   s   | j d krd S t| j S d S )N)r   r   Z	text_type)r   r   r   r   r      s    
zURL.passwordc             C   s
   || _ d S )N)r   )r   r   r   r   r   r      s    c             C   s$   d| j kr| j S | j dd S d S )N+r   )r
   split)r   r   r   r   get_backend_name   s    
zURL.get_backend_namec             C   s(   d| j kr|  jS | j dd S d S )Nr2   r   )r
   get_dialectZdriverr3   )r   r   r   r   get_driver_name   s    

zURL.get_driver_namec                s8   t jdd}| dg 7 } fdd|D S )NZpluginr   r   c                s   g | ]}t | qS r   )r   load)r   Zplugin_name)kwargsr   r   r   
<listcomp>   s   z,URL._instantiate_plugins.<locals>.<listcomp>)r   r   r   get)r   r8   Zplugin_namesr   )r8   r   r   _instantiate_plugins   s    zURL._instantiate_pluginsc             C   sZ   d| j kr| j }n| j dd}t|}t|drRt|jtrRt|jt	rR|jS |S dS )zReturn the "entry point" dialect class.

        This is normally the dialect itself except in the case when the
        returned class implements the get_dialect_cls() method.

        r2   .dialectN)
r
   replacer   r7   hasattrr.   r=   type
issubclassr   )r   nameclsr   r   r   _get_entrypoint   s    


zURL._get_entrypointc             C   s   |   }|| }|S )zfReturn the SQLAlchemy database dialect class corresponding
        to this URL's driver name.
        )rD   Zget_dialect_cls)r   Z
entrypointZdialect_clsr   r   r   r5      s    
zURL.get_dialectc             K   sl   i }dddddg}xT|D ]L}|r,| d}n||kr>|| }n|}|dk	rt| |drt| |||< qW |S )	a2  Translate url attributes into a dictionary of connection arguments.

        Returns attributes of this url (`host`, `database`, `username`,
        `password`, `port`) as a plain dictionary.  The attribute names are
        used as the keys by default.  Unset or false attributes are omitted
        from the final dictionary.

        :param \**kw: Optional, alternate key names for url attributes.

        :param names: Deprecated.  Same purpose as the keyword-based alternate
            names, but correlates the name to the original positionally.
        r   r   r   r   r   r   NF)popgetattr)r   nameskwZ
translatedZattribute_namesZsnamerB   r   r   r   translate_connect_args   s    

zURL.translate_connect_args)NNNNNN)T)__name__
__module____qualname____doc__r   r)   r*   r+   r-   r0   r1   propertyr   setterr4   r6   r;   rD   r5   rI   r   r   r   r   r	      s*        

	r	   c             C   s   t | tjrt| S | S dS )zGiven a string or unicode instance, produce a new URL instance.

    The given string is parsed according to the RFC 1738 spec.  If an
    existing URL object is passed, just returns the object.
    N)r.   r   Zstring_types_parse_rfc1738_args)Zname_or_urlr   r   r   make_url   s    rQ   c       
      C   sX  t dt j}|| }|d k	rF| }|d d k	r|d dd}|d |d< t|dkri }xbt|d D ]J\}}tj	r|
d}||krt|| ||< || | qr|||< qrW qd }nd }||d< |d	 d k	rt|d	 |d	< |d
 d k	rt|d
 |d
< |d}|d}	|p*|	|d< |d} t| f|S td|  d S )Na  
            (?P<name>[\w\+]+)://
            (?:
                (?P<username>[^:/]*)
                (?::(?P<password>.*))?
            @)?
            (?:
                (?:
                    \[(?P<ipv6host>[^/]+)\] |
                    (?P<ipv4host>[^/:]+)
                )?
                (?::(?P<port>[^/]*))?
            )?
            (?:/(?P<database>.*))?
            r   r   r   r   r   asciir   r   r   ipv4hostipv6hostr   rB   z,Could not parse rfc1738 URL from string '%s')recompileXmatch	groupdictr3   lenr   	parse_qslZpy2kencoder   append_rfc_1738_unquoterE   r	   r   ZArgumentError)
rB   patternmZ
componentstokensr   keyvaluerS   rT   r   r   r   rP      s@    





rP   c             C   s   t ddd | S )Nz[:@/]c             S   s   dt | d S )Nz%%%Xr   )ordgroup)r`   r   r   r   <lambda>(  s    z!_rfc_1738_quote.<locals>.<lambda>)rU   sub)textr   r   r   r!   '  s    r!   c             C   s
   t | S )N)r   Zunquote)rh   r   r   r   r^   +  s    r^   c             C   sH   t d| }|d k	r@|dd\} }tt|}t| f| S d S d S )Nz(\w+)://(.*)r   r   )rU   rX   re   dictr   r[   r	   )rB   r`   argsZoptsr   r   r   _parse_keyvalue_args/  s    rk   )rM   rU   Z
interfacesr    r   r   Zdialectsr   r   objectr	   rQ   rP   r!   r^   rk   r   r   r   r   <module>   s    D=