o
    ADiA                     @  sn  d dl mZ d dlZd dlZd dl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 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r^d dlmZ g dZejdkroG dd dZndd ZG dd dZG dd deZejdkrejZ ej!Z"n
d,ddZ d-dd Z"G d!d" d"Z#ed#Z$ed$e$f Z%d.d(d)Z&G d*d+ d+Z'dS )/    )annotationsN)Sequence)wraps)Path)PurePath)TYPE_CHECKING)Any)Callable)TypeVar)SplitResult)get_filesystem_class)UPath)PathlibPathShimstr_remove_prefixstr_remove_suffixFSSpecAccessorShim
deprecated)      c                      s(   e Zd ZdZdZdZ fddZ  ZS )r   z,no need to shim pathlib.Path in Python 3.12+ c                   s   t  j|  d S N)super__init__)selfargs	__class__r   F/var/www/Datamplify/venv/lib/python3.10/site-packages/upath/_compat.pyr   (      PathlibPathShim.__init__)__name__
__module____qualname____doc__	__slots____missing_py312_slots__r   __classcell__r   r   r   r   r   "   s
    r   c                    s*   d} dd t jD  t fdd| D S )zReturn a tuple of slots that are present in Python 3.12's
        pathlib.Path but not in the current version of pathlib.Path
        )	
_raw_paths_drv_root_tail_cached_str_str_normcase_cached_parts_normcase_cached_lines_cached_hashc                 S  s"   g | ]}t |d g D ]}|q
qS )r$   )getattr).0clsslotr   r   r   
<listcomp><   s
    z4_get_missing_py312_pathlib_slots.<locals>.<listcomp>c                   s   g | ]}| vr|qS r   r   )r1   r3   current_slotsr   r   r4   ?   s    )r   __mro__tuple)py312_slotsr   r5   r    _get_missing_py312_pathlib_slots-   s
   r:   c                   @  s  e Zd ZdZdZe Zdd Zedd Z	dd Z
d	d
 Zedd Zdd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zdd  Zd!d" Zd#d$ Zd%d&d'd(Zd)d* Zed+d, Zed-d. Zed/d0 Zd1d2 Zd3d4 Z d5S )6r   zA compatibility shim for python < 3.12

        Basically vendoring the functionality of pathlib.Path from Python 3.12
        that's not overwritten in upath.core.UPath

        r   c              	   G  s   g }|D ]P}t |tr-t|dr-|jtu r&| jtu r&|dd |jD  q||j qzt	|}W n t
y?   |}Y nw t |tsOt
dt|j|| q|| _d S )Nr'   c                 s  s    | ]	}| d dV  qdS )\/N)replace)r1   pathr   r   r   	<genexpr>R   s    z+PathlibPathShim.__init__.<locals>.<genexpr>zVargument should be a str or an os.PathLike object where __fspath__ returns a str, not )
isinstancer   hasattr_flavourntpath	posixpathextendr'   osfspath	TypeErrorstrtyper    append)r   r   pathsargr>   r   r   r   r   L   s&   

r   c           	      C  s   |sddg fS | j j}| j j}|r|||}| j |\}}}|sH||rH||sH||}t|dkr@|d dvr@|}nt|dkrH|}dd ||D }|||fS )N       z?.   c                 S  s&   g | ]}|r|d krt t|qS .)sysinternrI   )r1   xr   r   r   r4   t   s   & z/PathlibPathShim._parse_path.<locals>.<listcomp>)	rB   sepaltsepr=   	splitroot
startswithendswithsplitlen)	r2   r>   rW   rX   drvrootrel	drv_partsparsedr   r   r   _parse_pathc   s   


zPathlibPathShim._parse_pathc                 C  s`   | j }t|dkrd}nt|dkr|d }n| jj| }| |\}}}|| _|| _|| _d S )Nr   rN      )r'   r]   rB   joinrc   r(   r)   r*   )r   rL   r>   r^   r_   tailr   r   r   _load_partsw   s   

zPathlibPathShim._load_partsc                 C  s8   |  |||}| |}|pd|_||_||_||_|S NrS   )_format_parsed_partswith_segmentsr+   r(   r)   r*   )r   r^   r_   rf   path_strr>   r   r   r   _from_parsed_parts   s   

z"PathlibPathShim._from_parsed_partsc                 C  sN   |s|r|| | j j| S |r | j |d d r dg| }| j j|S )Nr   rS   )rB   rW   re   
splitdrive)r2   r^   r_   rf   r   r   r   ri      s
   
z$PathlibPathShim._format_parsed_partsc                 C  s<   z| j W S  ty   | | j| j| jpd| _ | j  Y S w rh   )r+   AttributeErrorri   driver_   _tailr   r   r   r   __str__   s   
zPathlibPathShim.__str__c                 C  *   z| j W S  ty   |   | j  Y S w r   )r(   rn   rg   rq   r   r   r   ro         
zPathlibPathShim.drivec                 C  rs   r   )r)   rn   rg   rq   r   r   r   r_      rt   zPathlibPathShim.rootc                 C  rs   r   )r*   rn   rg   rq   r   r   r   rp      rt   zPathlibPathShim._tailc                 C  s   | j | j }|S r   )ro   r_   )r   anchorr   r   r   ru      s   zPathlibPathShim.anchorc                 C  s   | j }|sdS |d S )NrN   )rp   )r   rf   r   r   r   name   s   zPathlibPathShim.namec                 C  sB   | j }|d}d|  k rt|d k rn dS ||d  S dS )NrS   r   rd   rN   rw   rfindr]   r   rw   ir   r   r   suffix      
zPathlibPathShim.suffixc                 C  s:   | j }|dr
g S |d}dd |ddd  D S )NrS   c                 S  s   g | ]}d | qS rR   r   )r1   r|   r   r   r   r4      s    z,PathlibPathShim.suffixes.<locals>.<listcomp>rd   )rw   r[   lstripr\   )r   rw   r   r   r   suffixes   s
   

zPathlibPathShim.suffixesc                 C  sB   | j }|d}d|  k rt|d k rn |S |d | S |S )NrS   r   rd   rx   rz   r   r   r   stem   r}   zPathlibPathShim.stemc                 C  sn   | j s
t| d| j}|r |j|v s |jr|j|v s |dkr&td| | | j| j| jd d |g S )N has an empty namerS   zInvalid name %rrv   )	rw   
ValueErrorrB   rW   rX   rl   ro   r_   rp   )r   rw   fr   r   r   	with_name   s   

zPathlibPathShim.with_namec                 C  s   |  || j S r   )r   r|   )r   r   r   r   r   	with_stem   r   zPathlibPathShim.with_stemc                 C  s   | j }|j|v s|jr|j|v rtd||r|dr"|dkr(td| | j}|s4t| d| j}|s>|| }n|d t|  | }| | j	| j
| jd d |g S )NzInvalid suffix rS   zInvalid suffix %rr   rv   )rB   rW   rX   r   rZ   rw   r|   r]   rl   ro   r_   rp   )r   r|   r   rw   
old_suffixr   r   r   with_suffix   s   
zPathlibPathShim.with_suffixF)walk_upc               G  s   |rd}t jd| tdd | j|g|R  }t|gt|j D ])\}}| |r- n-|s<tt	| dt	||j
dkrKtdt	|dq"tt	| d	t	|d
dg| | jt|jd   }| j| S )Nzsupport for supplying more than one positional argument to pathlib.PurePath.relative_to() is deprecated and scheduled for removal in Python 3.14z$pathlib.PurePath.relative_to(*args) rP   
stacklevelz is not in the subpath of z..z'..' segment in z cannot be walkedz and z have different anchors)warningswarnDeprecationWarningrj   	enumeratelistparentsis_relative_tor   rI   rw   rp   r]   )r   otherr   _deprecatedmsgstepr>   partsr   r   r   relative_to   s0   


zPathlibPathShim.relative_toc                G  sB   |rd}t jd| tdd | j|g|R  }|| kp || jv S )Nzsupport for supplying more than one argument to pathlib.PurePath.is_relative_to() is deprecated and scheduled for removal in Python 3.14z'pathlib.PurePath.is_relative_to(*args) rP   r   )r   r   r   rj   r   )r   r   r   r   r   r   r   r     s   zPathlibPathShim.is_relative_toc                 C  s.   | j s| jr| j | j ft| j S t| jS r   )ro   r_   r8   rp   rq   r   r   r   r   ,  s   
zPathlibPathShim.partsc                 C  s0   | j }| j}| j}|s| S | |||d d S )Nrv   )ro   r_   rp   rl   )r   r^   r_   rf   r   r   r   parent3  s   zPathlibPathShim.parentc                 C  s   t | S r   )_PathParentsrq   r   r   r   r   <  s   zPathlibPathShim.parentsc                 C  sr   t | }| j}|r| | jj | }n|dkr| | }n|}| |}||_| j|_| j|_	||g |_
|S rh   )rI   rp   rB   rW   rj   r+   ro   r(   r_   r)   r*   )r   rw   rk   rf   r>   r   r   r   _make_child_relpath@  s   
z#PathlibPathShim._make_child_relpathc                 C  s   | j |dd dS )z
            Like chmod(), except if the path points to a symlink, the symlink's
            permissions are changed, rather than its target's.
            F)follow_symlinksN)chmod)r   moder   r   r   lchmodP  s   zPathlibPathShim.lchmodN)!r    r!   r"   r#   r$   r:   r%   r   classmethodrc   rg   rl   ri   rr   propertyro   r_   rp   ru   rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   A   sR    
	
	










c                   @  s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   )_pathr(   r)   rp   c                 C  s"   || _ |j| _|j| _|j| _d S r   )r   ro   r(   r_   r)   rp   r   r>   r   r   r   r   Z  s   z_PathParents.__init__c                 C  s
   t | jS r   )r]   rp   rq   r   r   r   __len__`     
z_PathParents.__len__c                   s   t |trt fddt|t  D S |t ks$|t  k r(t||dk r2|t 7 } j j	 j
 jd | d  S )Nc                 3  s    | ]} | V  qd S r   r   )r1   r{   rq   r   r   r?   e  s    z+_PathParents.__getitem__.<locals>.<genexpr>r   rd   )r@   slicer8   rangeindicesr]   
IndexErrorr   rl   r(   r)   rp   )r   idxr   rq   r   __getitem__c  s   
$z_PathParents.__getitem__c                 C  s   dt | jj dS )N<z	.parents>)rJ   r   r    rq   r   r   r   __repr__o  s   z_PathParents.__repr__N)r    r!   r"   r$   r   r   r   r   r   r   r   r   r   W  s    r   )r   	   srI   r|   returnc                 C  s    |  |r| d t|  S | S r   )r[   r]   )r   r|   r   r   r   r   y  s   
r   prefixc                 C  s   |  |r| t|d  S | S r   )rZ   r]   )r   r   r   r   r   r     s   
r   c                   @  s   e Zd ZdZd-dd	Zd
d Zed.ddZd/ddZd0ddZ	dd Z
dd Zdd Zdd Zdd Zd d! Zd1d#d$Zd2d&d'Zd(d) Zd3d+d,Zd*S )4r   z0this is a compatibility shim and will be removed
parsed_urlSplitResult | Nonekwargsr   r   Nonec                 K  sN   |r|j rt|j }|| }ntd }i }|| |di || _d S )Nr   )schemer   _get_kwargs_from_urlsgeturlupdate_fs)r   r   r   r2   
url_kwargsr   r   r   r     s   


zFSSpecAccessorShim.__init__c                 K  s   t jdtdd d S )NzAll _FSSpecAccessor subclasses have been deprecated.  Please follow the universal_pathlib==0.2.0 migration guide at https://github.com/fsspec/universal_pathlib for more information.rP   r   )r   r   r   )r2   r   r   r   r   __init_subclass__  s
   
z$FSSpecAccessorShim.__init_subclass__r>   r   c                 C  s2   |j j|jd}| |fi |j}|j|jd< |S )z-internal accessor for backwards compatibility)r   r   )_url_replaceprotocolstorage_optionsfs__dict__)r2   r>   urlobjr   r   r   	from_path  s   zFSSpecAccessorShim.from_pathrI   c                 C  s   |j S r   )r>   r   r   r   r   _format_path  s   zFSSpecAccessorShim._format_pathrc                 O  s"   |j j| ||g|R i |S r   )r   openr   )r   r>   r   r   r   r   r   r   r     s   "zFSSpecAccessorShim.openc                 K     |j j| |fi |S r   )r   statr   r   r>   r   r   r   r   r        zFSSpecAccessorShim.statc                 K  s~   |  |}|jj|fi |}t|dkr!|j|s!tt| t|dkr=|d d |kr=|d d dkr=tt| |S )Nr   rd   rw   rJ   file)r   r   listdirr]   isdirNotADirectoryErrorrI   )r   r>   r   p_fmtcontentsr   r   r   r     s   
zFSSpecAccessorShim.listdirc                 K  s   |j j| |fi |S r   )r   globr   )r   r   path_patternr   r   r   r   r     r   zFSSpecAccessorShim.globc                 K  r   r   )r   existsr   r   r   r   r   r     r   zFSSpecAccessorShim.existsc                 K  r   r   )r   infor   r   r   r   r   r     r   zFSSpecAccessorShim.infoc                 K     |j j| |fd|i|S )N	recursive)r   rmr   )r   r>   r   r   r   r   r   r        zFSSpecAccessorShim.rmTc                 K  r   )Ncreate_parents)r   mkdirr   )r   r>   r   r   r   r   r   r     s   zFSSpecAccessorShim.mkdirFc                 K  r   )Nexist_ok)r   makedirsr   )r   r>   r   r   r   r   r   r     r   zFSSpecAccessorShim.makedirsc                 K  r   r   )r   touchr   r   r   r   r   r     r   zFSSpecAccessorShim.touchNc                 K  s8   t |dr|j|}|jj| ||f||d|S )N	_accessor)r   maxdepth)rA   r   r   r   mv)r   r>   targetr   r   r   r   r   r   r     s   
zFSSpecAccessorShim.mv)r   r   r   r   r   r   )r>   r   r   r   )r>   r   r   rI   )r   )T)F)FN)r    r!   r"   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s$    






r   RT.python_versiontuple[int, ...]Callable[[F], F]c                   s$   d tt d fdd}|S )	zmarks function as deprecatedrS   funcFr   c                   s(   t jkrt  fdd}|S  S )Nc                    s*   t j j d tdd  | i |S )Nz is deprecated on py>=rP   r   )r   r   r    r   )r   r   )r   	pyver_strr   r   wrapper  s   z9deprecated.<locals>.deprecated_decorator.<locals>.wrapper)rT   version_infor   )r   r   r   r   )r   r   deprecated_decorator  s
   
z(deprecated.<locals>.deprecated_decoratorN)r   r   r   r   )re   maprI   )r   r   r   r   r   r     s   r   c                   @  s    e Zd ZdZdd Zdd ZdS )method_and_classmethodz<Allow a method to be used as both a method and a classmethodc                 C  s
   || _ d S r   )method)r   r   r   r   r   r     r   zmethod_and_classmethod.__init__c                 C  s    |d u r
| j |S | j |S r   )r   __get__)r   instanceownerr   r   r   r      s   zmethod_and_classmethod.__get__N)r    r!   r"   r#   r   r   r   r   r   r   r     s    r   )r   rI   r|   rI   r   rI   )r   rI   r   rI   r   rI   )r   r   r   r   )(
__future__r   rC   rF   rD   rT   r   collections.abcr   	functoolsr   pathlibr   r   typingr   r   r	   r
   urllib.parser   fsspecr   upathr   __all__r   r   r:   r   rI   removesuffixr   removeprefixr   r   r   r   r   r   r   r   r   r   <module>   sH    
	  


X
