
    #j                    L    d Z ddlmZ ddlmZmZmZmZ dZdez   Z	dd
Z
ddZdS )a(  Database URL normalization.

Hindsight accepts SQLAlchemy-style URLs like ``postgresql+asyncpg://...?ssl=require``
for its async engine, but the same string cannot be handed directly to synchronous
SQLAlchemy (psycopg2) or to :func:`asyncpg.create_pool`, which both expect a
libpq-compatible URL (``postgresql://...?sslmode=require``).

:func:`to_libpq_url` performs that translation. It is idempotent and safe to
apply to URLs that are already libpq-compatible, to the ``pg0`` embedded-PG
marker, or to any non-PostgreSQL string (returned unchanged).
    )annotations)	parse_qsl	urlencodeurlsplit
urlunsplit)zpostgresql+asyncpgzpostgres+asyncpg)
postgresqlpostgresurlstrreturnboolc                `    | rd| vrdS t          |           j                            d          S )zPTrue if ``url`` is an Oracle SQLAlchemy URL (``oracle`` or ``oracle+oracledb``).://Foracle)r   scheme
startswith)r
   s    \/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/db_url.pyis_oracle_urlr      s6     %s""uC==**8444    c                (   | rd| vr| S t          |           }|j        t          vr| S d}d t          |j        d          D             }t          |          }||j        k    r||j        k    r| S t          ||j        |j        ||j	        f          S )u  Normalize a PostgreSQL URL for libpq-style consumers.

    Accepts a SQLAlchemy URL (``postgresql+asyncpg://...``) or a plain libpq
    URL and returns a form suitable for:

    - :func:`sqlalchemy.create_engine` (sync / psycopg2)
    - :func:`asyncpg.create_pool`

    Transformations:

    - ``postgresql+asyncpg`` / ``postgres+asyncpg`` / ``postgres`` → ``postgresql``
    - Query param ``ssl=<mode>`` → ``sslmode=<mode>`` (SQLAlchemy's asyncpg
      dialect uses ``ssl=``; libpq uses ``sslmode=``)

    Any non-PostgreSQL input (e.g. the ``pg0`` embedded-PG marker, a sqlite
    URL, an empty string) is returned unchanged. Already-normalized URLs are
    returned unchanged.
    r   r   c                0    g | ]\  }}|d k    rd|fn||fS )sslsslmode ).0kvs      r   
<listcomp>z to_libpq_url.<locals>.<listcomp>8   s=       59Q!u**A1a&  r   T)keep_blank_values)
r   r   _POSTGRES_SCHEMESr   queryr   r   netlocpathfragment)r
   parts
new_schemenew_query_pairs	new_querys        r   to_libpq_urlr)      s    &  %s""
SMME|,,,
J =Fu{fj=k=k=k  O /**IU\!!i5;&>&>
z5<YWXXXr   N)r
   r   r   r   )r
   r   r   r   )__doc__
__future__r   urllib.parser   r   r   r   _ASYNCPG_SCHEMESr    r   r)   r   r   r   <module>r.      s   
 
 # " " " " " C C C C C C C C C C C C= .1AA 5 5 5 5$Y $Y $Y $Y $Y $Yr   