
    rbi                        d Z ddl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 Zd	eez  d
efdZdddddddddddddddZd e                                D             Zd	efdZd
efdZd Zd Zd	eez  fdZdS )zUtility functions.    N)Version)Path)urlparse)MULTI_EXTENSIONS)vsimem_rmtree_toplevelc                 n   t          | t                    rt          |           S t          | t                    r| S t	          | d          rO|                                 }t	          | d          r)|                                 r|                     d           |S t          t          |                     S )a  Get VSI-prefixed path or bytes buffer depending on type of path_or_buffer.

    If path_or_buffer is a bytes object, it will be returned directly and will
    be read into an in-memory dataset when passed to one of the Cython functions.

    If path_or_buffer is a file-like object with a read method, bytes will be
    read from the file-like object and returned.

    Otherwise, it will be converted to a string, and parsed to prefix with
    appropriate GDAL /vsi*/ prefixes.

    Parameters
    ----------
    path_or_buffer : str, pathlib.Path, bytes, or file-like
        A dataset path or URI, raw buffer, or file-like object with a read method.

    Returns
    -------
    str or bytes

    readseekabler   )	
isinstancer   vsi_pathbyteshasattrr	   r
   seekstr)path_or_bufferbytes_buffers     \/var/www/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/pyogrio/util.pyget_vsi_path_or_bufferr      s    0 .$'' ('''.%(( ~v&& %**,, >:.. 	#>3J3J3L3L 	#"""C''(((    pathreturnc                    t          | t                    r[t          j        dk    r<|                                                     d          r|                                 } nt          |           } |                     d          r| S t          j        dk    rt          j        d|           rj| 	                    d          d         
                    d          s| S | 	                    d          d         
                    t                    r| S d|  } t          |           \  } }}|s1|s/| 
                    d          r+| 
                    t                    st          | ||          S | S )z:Ensure path is a local path or a GDAL-compatible VSI path.win32z/vsiz^[a-zA-Z]\:!r   .zipzzip://)r   r   sysplatformas_posix
startswithr   rematchsplitendswithr   
_parse_uri_construct_vsi_path)r   archiveschemes      r   r   r   7   sc    $ <7""t}}'A'A&'I'I"==??DDt99D v  |w28ND#A#A zz#q!**622 	K::c??1&&'788 	K &t,,D'6 	:: MM&!!: +/--8H*I*I:
 #4&999Kr   fileziptargzipcurls3gsazadlshdfswebhdfs)r(   r)   r*   r+   httphttpsftpr-   r.   r/   r0   adlr1   r2   c                 $    h | ]\  }}|d k    |S )r,    ).0kvs      r   	<setcomp>r<   q   s!    <<<TQVqr   c                    t          | d          }|j        r6t          d |j                            d          D                       s| ddfS |j        } |j        pd}|j        r| d|j        z   z  } |j        r|j        r
|j        | z   } |                     d          }|r|                                nd} |r|                                nd}| ||fS )a  Parse a URI.

    Returns a tuples of (path, archive, scheme)

    path : str
        Parsed path. Includes the hostname and query string in the case
        of a URI.
    archive : str
        Parsed archive path.
    scheme : str
        URI scheme such as "https" or "zip+s3".
    F)allow_fragmentsc              3   (   K   | ]}|t           v V  d S )NSCHEMESr9   ps     r   	<genexpr>z_parse_uri.<locals>.<genexpr>   s&      NNWNNNNNNr   + ?r   )r   r'   allr"   r   querynetlocpop)r   partsr'   r&   s       r   r$   r$   t   s     T5111E | CNNel6H6H6M6MNNNNN R| :D\RF{ "ek!!| # #|d"JJsOOE'599;;;RD"*eiikkkG'6""r   c                    d}d}|                     d          }d|vrZ|                    d          s/|                     d          r0|                     t                    s|                    dd           |r9d                    d |D                       }|d         t
          v r|d          d	}|r7|r+d
                    ||||                     d                    S d| d| |  S | S )z)Convert a parsed path to a GDAL VSI path.rF   rE   r)   r   r   /c              3   F   K   | ]}||d k    
dt           |          V  dS )r(   vsiNr@   rB   s     r   rD   z&_construct_vsi_path.<locals>.<genexpr>   s8      SSSqF{{,
,,{{{{SSr   z://z/{}/{}{}/{})r"   r#   r   insertjoinCURLSCHEMESformatlstrip)r   r&   r'   prefixsuffixschemess         r   r%   r%      s   FFll3GG   MM&!! *.--8H*I*I  	q%    )SSgSSSSS2;+%%(((F / 	/ ''SAQAQRRR.v......Kr   c                 4   t          | t                    st          dt          |                      i }|                                 D ]L\  }}||                                }t          |t                    r|rdnd}nt          |          }|||<   M|S )zhPreprocess options.

    For example, `spatial_index=True` gets converted to `SPATIAL_INDEX="YES"`.
    z#Expected options to be a dict, got NONOFF)r   dict	TypeErrortypeitemsupperboolr   )optionsresultr:   r;   s       r   _preprocess_options_key_valuere      s    
 gt$$ OMd7mmMMNNNF  19GGIIa 	$uAAAAq		Mr   c                    | | S 	 ddl }t          |j                  t          d          k     rd}n# t          $ r d}Y nw xY w|st	          d          t          | |j                  st	          d          |                    |           S )aH  Convert a Shapely mask geometry to WKB.

    Parameters
    ----------
    mask : Shapely geometry
        The geometry to convert to WKB.

    Returns
    -------
    WKB bytes or None

    Raises
    ------
    ValueError
        raised if Shapely >= 2.0 is not available or mask is not a Shapely
        Geometry object

    Nr   z2.0.0z('mask' parameter requires Shapely >= 2.0z+'mask' parameter must be a Shapely geometry)shapelyr   __version__ImportError
ValueErrorr   Geometryto_wkb)maskrg   s     r   _mask_to_wkbrn      s    & |7&''''*:*:::G     ECDDDdG,-- HFGGG>>$s   +2 A Ac                 v    t          | t                    r|                                 } t          |            dS )a  Remove the parent directory of the file path recursively.

    This is used for final cleanup of an in-memory dataset, which may have been
    created within a directory to contain sibling files.

    Additional VSI handlers may be chained to the left of /vsimem/ in path and
    will be ignored.

    Remark: function is defined here to be able to run tests on it.

    Parameters
    ----------
    path : str or pathlib.Path
        path to in-memory file

    N)r   r   r   _vsimem_rmtree_toplevel)r   s    r   r   r      s7    " $ }}D!!!!!r   )__doc__r    r   packaging.versionr   pathlibr   urllib.parser   pyogrio._ogrr   pyogrio._vsir   rp   r   r   r   rA   r`   rT   r$   r%   re   rn   r8   r   r   <module>rw      s     				 



 % % % % % %       ! ! ! ! ! ! ) ) ) ) ) ) J J J J J J') ') ')T#3: ## # # # #P 


 & =<W]]__<<< #S  #  #  #  #F#    6  *$  $  $ N"t " " " " " "r   