o
    DDiw                     @  s   d Z ddlmZ ddlZddlmZmZmZ ddlmZ	 ddlm
Z
 ddlmZmZ ejZejZd	Zd!d"ddZd!d#ddZd$ddZedZedZd%ddZd&dd ZdS )'z*
Functions to manipulate conninfo strings
    )annotationsN   )_conninfo_attempts_conninfo_attempts_async_conninfo_utils)errors)pq)ConnDict	ConnParam    conninfostrkwargsr
   returnc                 K  sp   | s|sdS |st |  t| S dd | D }| r&t| }|| |}ddd | D } t |  | S )a  
    Merge a string and keyword params into a single conninfo string.

    :param conninfo: A `connection string`__ as accepted by PostgreSQL.
    :param kwargs: Parameters overriding the ones specified in `!conninfo`.
    :return: A connection string valid for PostgreSQL, with the `!kwargs`
        parameters merged.

    Raise `~psycopg.ProgrammingError` if the input doesn't make a valid
    conninfo string.

    .. __: https://www.postgresql.org/docs/current/libpq-connect.html
           #LIBPQ-CONNSTRING
    r   c                 S  s   i | ]\}}|d ur||qS N .0kvr   r   I/var/www/Datamplify/venv/lib/python3.10/site-packages/psycopg/conninfo.py
<dictcomp>4   s    z!make_conninfo.<locals>.<dictcomp> c                 s  s*    | ]\}}| d t t| V  qdS )=N)_param_escaper   r   r   r   r   	<genexpr>;   s   ( z make_conninfo.<locals>.<genexpr>)_parse_conninfor   itemsconninfo_to_dictupdatejoin)r   r   tmpr   r   r   make_conninfo   s   
r#   r	   c                 K  s<   t | }dd |D }| D ]\}}|dur|||< q|S )a  
    Convert the `!conninfo` string into a dictionary of parameters.

    :param conninfo: A `connection string`__ as accepted by PostgreSQL.
    :param kwargs: Parameters overriding the ones specified in `!conninfo`.
    :return: Dictionary with the parameters parsed from `!conninfo` and
        `!kwargs`.

    Raise `~psycopg.ProgrammingError` if `!conninfo` is not a a valid connection
    string.

    .. __: https://www.postgresql.org/docs/current/libpq-connect.html
           #LIBPQ-CONNSTRING
    c                 S  s(   i | ]}|j d ur|j |j  qS r   )valkeyworddecode)r   optr   r   r   r   S   s    "z$conninfo_to_dict.<locals>.<dictcomp>N)r   r   )r   r   optsrvr   r   r   r   r   r   C   s   r   list[pq.ConninfoOption]c              
   C  s>   z	t j|  W S  tjy } ztt|dd}~ww )z
    Verify that `!conninfo` is a valid connection string.

    Raise ProgrammingError if the string is not valid.

    Return the result of pq.Conninfo.parse() on success.
    N)r   ConninfoparseencodeeOperationalErrorProgrammingErrorr   )r   exr   r   r   r   \   s   r   z([\\'])z\ssc                 C  s.   | sdS t td|  } rd|  d } | S )z9
    Apply the escaping rule required by PQconnectdb
    z''z\\\1')re_spacesearch	re_escapesub)r2   r   r   r   r   n   s
   r   paramsintc                 C  sj   t | d}|du rt}ztt|}W n ty$   td|dw |dkr-t}|S |dk r3d}|S )zG
    Return the timeout in seconds from the connection parameters.
    connect_timeoutNzbad value for connect_timeout: r      )r   	get_param_DEFAULT_CONNECT_TIMEOUTr9   float
ValueErrorr.   r0   )r8   valuetimeoutr   r   r   timeout_from_conninfo{   s   rB   )r   )r   r   r   r
   r   r   )r   r   r   r
   r   r	   )r   r   r   r*   )r2   r   r   r   )r8   r	   r   r9   )__doc__
__future__r   rer   r   r   r   r   r.   r   abcr	   r
   conninfo_attemptsconninfo_attempts_asyncr=   r#   r   r   compiler6   r4   r   rB   r   r   r   r   <module>   s"    )



