
    h                     2   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	 Zd
eeef   defdZdddddddddddddddZej'                         D  ch c]  \  } }|dk(  s|  c}} Zd
efdZdefdZd Zd Zd
eeef   fdZyc c}} w )zUtility functions.    N)Version)Path)Union)urlparse)vsimem_rmtree_toplevel)z	.gpkg.zipz.shp.zipc                    t        | t              rt        |       S t        | t              r| S t	        | d      r?| j                         }t	        | d      r!| j                         r| j                  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/immo/lib/python3.12/site-packages/pyogrio/util.pyget_vsi_path_or_bufferr      s|    0 .$'''.%(~v&%**, >:.>3J3J3L"C'((    pathreturnc                 l   t        | t              rNt        j                  dk(  r0| j	                         j                  d      r| j	                         } nt        |       } | j                  d      r| S t        j                  dk(  rit        j                  d|       rS| j                  d      d   j                  d      s| S | j                  d      d   j                  t              r| S d|  } t        |       \  } }}|s(|s&| j                  d      r"| j                  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endswithMULTI_EXTENSIONS
_parse_uri_construct_vsi_path)r   archiveschemes      r   r   r   9   s    $<<7"t}}'A'A&'I==?Dt9D v ||w288ND#A zz#q!**62K::c?1&&'78K v&t,D'6 	MM&!$--8H*I"4&99Kr   fileziptargzipcurls3gsazadlshdfswebhdfs)r)   r*   r+   r,   httphttpsftpr.   r/   r0   r1   adlr2   r3   c                    t        | d      }|j                  r0t        d |j                  j                  d      D              s| ddfS |j                  } |j                  xs d}|j
                  r| d|j
                  z   z  } |j                  r|j                  r|j                  | z   } | j                  d      }|r|j                         nd} |r|j                         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   y w)NSCHEMES.0ps     r   	<genexpr>z_parse_uri.<locals>.<genexpr>   s     NWNs   + ?r   )r   r(   allr"   r   querynetlocpop)r   partsr(   r'   s       r   r%   r%   v   s     T51E ||CNell6H6H6MNNR| ::D\\RF{{ekk!!||||d"JJsOE599;RD"eiikG'6""r   c                    d}d}|j                  d      }d|vrI|j                  d      s&| j                  d      r'| j                  t              s|j                  dd       |r+dj	                  d |D              }|d   t
        v r|d    d	}|r/|r#d
j                  |||| j                  d            S d| d| |  S | S )z)Convert a parsed path to a GDAL VSI path.rB   rA   r*   r   r   /c              3   F   K   | ]  }|s|d k7  sdt         |      yw)r)   vsiNr;   r=   s     r   r@   z&_construct_vsi_path.<locals>.<genexpr>   s#     SqF{C
|,Ss   !!!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q% SgSS2;+%}C(F ''SAQRRvhaxv..Kr   c                     t        | t              st        dt        |              i }| j	                         D ]?  \  }}|	|j                         }t        |t              r|rdnd}nt        |      }|||<   A |S )zhPreprocess options.

    For example, `spatial_index=True` gets converted to `SPATIAL_INDEX="YES"`.
    z#Expected options to be a dict, got ONOFF)r   dict	TypeErrortypeitemsupperboolr   )optionsresultkvs       r   _preprocess_options_key_valuerc      s    
 gt$=d7m_MNNF 19GGIauAAAq	 Mr   c                     | | S 	 ddl }t        |j                        t        d      k  rd}|st	        d      t        | |j                        st	        d      |j                  |       S # t        $ r d}Y Lw xY w)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)maskre   s     r   _mask_to_wkbrl      s    & |7&&'''*::G CDDdG,,-FGG>>$  s   'A, ,A:9A:c                 Z    t        | t              r| j                         } t        |        y)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      s!    " $}}D!r   )__doc__r    r   packaging.versionr   pathlibr   typingr   urllib.parser   pyogrio._vsir   rn   r$   r   r   r   r<   r\   rP   r%   r&   rc   rl   )ra   rb   s   00r   <module>ru      s     	 
 %   ! J, ')T#5d# # #P 


& %]]_<TQVq< #S  #F# 6*$ N"sDy!1 "{ =s   B-B