o
    6	fx                     @  s   d Z ddlm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 dd
lmZ e
j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)Any   )pq)errors)_conninfo_utils)_conninfo_attempts)_conninfo_attempts_async    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/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg/conninfo.py
<dictcomp>8   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"   ConnDictc                 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   W   s   ( z$conninfo_to_dict.<locals>.<dictcomp>N)r   r   )r   r   optsrvr   r   r   r   r   r   G   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 d| } t| rd|  d } | S )z9
    Apply the escaping rule required by PQconnectdb
    z''z\\\1')	re_escapesubre_spacesearch)r2   r   r   r   r   p   s   
r   paramsintc                 C  sd   t | d}|du rt}zt|}W n ty!   td|w |dkr*t}|S |dk r0d}|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   
ValueErrorr.   r0   )r8   valuetimeoutr   r   r   timeout_from_conninfo~   s   rA   )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   retypingr   r   r   r   r.   r   r   r	   r#   conninfo_attemptsconninfo_attempts_asyncr=   r"   r   r   compiler4   r6   r   rA   r   r   r   r   <module>   s(    )



