o
    oĎi+                     @   sR  U d dl Z d dlmZ d dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d dlZd dlmZmZmZmZ d dlZd dlZd dlmZ d dlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z* d d	l+m,Z, e%rd d
l-m.Z.m/Z/m0Z0m1Z1m2Z2 ej3dkrd dlm4Z4 nd dl5m4Z4 ej3dkrd dlm6Z6 nd dl5m6Z6 ej3dk rd dlm7Z7 d dlm8Z9 d dlm:Z: d6ddZ8nLej3dd dkrd dlm:Z: d dlm;Z7 d dlm8Z8 n0ej3dd dkrd dl<m:Z: d dl;Z7ddl=m8Z8 nej3dkrd dlm:Z: d dl;Z7ddl=m8Z8 d dl>m?Z? ddl@mAZA ddlBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ e%rIddlRmSZS ddlTmUZU G d d! d!ZVeeVZWeeXeVf eYd"< e&d#ZZe&d$d%d&Z[d'eXd(ee$e[ ge$e[ f fd)d*Z\G d+d, d,e j]Z^G d-d% d%e^d.Z_d/ed(eXfd0d1Z`G d2d3 d3ZaG d4d5 d5ZbdS )7    Ndefaultdict)contextmanager)BufferedRandomBufferedReaderBufferedWriterFileIOTextIOWrapper)Path	PosixPathPurePosixPathWindowsPath)
MethodType)BinaryIOLiteraloverloadAnyCallable	ContainerIterableIODict	GeneratorListOptionalTupleTypeTYPE_CHECKINGTypeVarUnioncast)urlparse)warn)OpenBinaryModeOpenBinaryModeReadingOpenBinaryModeUpdatingOpenBinaryModeWritingOpenTextMode)   
   )	TypeGuard)r(      )Selfr(      )_posix_flavour)_make_selector)_PathParentsTc                 C   s   t t| |S N)_make_selector_pathlibtuple)pattern_parts_flavourcase_sensitive r8   R/home/jeff/fluffinator/venv/lib/python3.10/site-packages/cloudpathlib/cloudpath.pyr0   A   s   r0      )	posixpathr(         )r(      )FileCacheMode)anypath)ClientMismatchErrorCloudPathFileExistsErrorCloudPathFileNotFoundErrorCloudPathIsADirectoryErrorCloudPathNotADirectoryErrorCloudPathNotExistsErrorCloudPathNotImplementedErrorDirectoryNotEmptyErrorIncompleteImplementationErrorInvalidPrefixErrorMissingDependenciesErrorNoStatErrorOverwriteDirtyFileErrorOverwriteNewerCloudErrorOverwriteNewerLocalError)ClientCloudPathInfoc                   @   sp   e Zd ZU eed< dZeed< ed ed< ed ed< dd
dZe	ded fddZ
e	ded fddZd	S )CloudImplementationnameTdependencies_loadedrQ   _client_class	CloudPath_path_classreturnNc                    sR   ddg} fdd|D }|rt d|  js'td jj d j dd S )	Nclient_class
path_classc                    s$   g | ]}t  d | du r|qS )_N)getattr).0clsselfr8   r9   
<listcomp>w   s   $ z=CloudImplementation.validate_completeness.<locals>.<listcomp>z1Implementation is missing registered components: zMissing dependencies for z6. You can install them with 'pip install cloudpathlib[z]'.)rJ   rV   rL   rW   __name__rU   )rb   expectedmissingr8   ra   r9   validate_completenessu   s   z)CloudImplementation.validate_completenessc                 C      |    | jS r2   )rg   rW   ra   r8   r8   r9   r[         z CloudImplementation.client_classc                 C   rh   r2   )rg   rY   ra   r8   r8   r9   r\      ri   zCloudImplementation.path_classrZ   N)rd   
__module____qualname__str__annotations__rV   boolr   rg   propertyr[   r\   r8   r8   r8   r9   rT   o   s   
 
rT   implementation_registryT
CloudPathTrX   )boundkeyrZ   c                    s"   dt t dt t f fdd}|S )Nr`   rZ   c                    s*   t | ts	td| t  _t  | _| S )Nz/Only subclasses of CloudPath can be registered.)
issubclassrX   	TypeErrorrq   rY   _cloud_metar`   ru   r8   r9   	decorator   s
   


z&register_path_class.<locals>.decorator)r   rs   )ru   r{   r8   rz   r9   register_path_class   s   r|   c                   @   s   e Zd Zedee dedededef
ddZedee de	e
df dededef
d	dZdee de	e
ef dedede	edef f
d
dZde
deedf dee
ef ddfddZdS )CloudPathMetar`   
cloud_pathargskwargsrZ   c                 O      d S r2   r8   r`   r~   r   r   r8   r8   r9   __call__      zCloudPathMeta.__call__rX   c                 O   r   r2   r8   r   r8   r8   r9   r      r   c                 O   s   t | tstdtj d| tu rQt D ]&}|j}|d ur<|j|ddr<t	|}|j
||g|R i | |  S qdd t D }td| d| d	t	| }| j
||g|R i | |S )
NzOnly subclasses of z) can be instantiated from its meta class.Fraise_on_errorc                 S   s   g | ]}|j d ur|j jqS r2   )rY   cloud_prefix)r_   implr8   r8   r9   rc      s
    
z*CloudPathMeta.__call__.<locals>.<listcomp>Path z$ does not begin with a known prefix .)rv   rX   rw   rd   rq   valuesrY   is_valid_cloudpathobject__new____init__rK   )r`   r~   r   r   implementationr\   new_objvalid_prefixesr8   r8   r9   r      s.   


rU   bases.dicNc                 C   s   t | D ]B}|dsFtt|rFttt|dd rFtt|jd }tt| |tr1|t| |j_n|t| |_tt| |t	rF|t| |j
_qd S )Nr]   __doc__z' _(Docstring copied from pathlib.Path)_)dir
startswithhasattrr
   r^   r   
isinstancer   __func__rp   fget)r`   rU   r   r   attr	docstringr8   r8   r9   r      s   zCloudPathMeta.__init__)rd   rk   rl   r   r   rr   rs   r   r   r   rm   r   typer   r   r8   r8   r8   r9   r}      sJ    


.!r}   c                   @   sF  e Zd ZU dZeed< eed< dddeeed f dede	d	 d
dfddZ
edd ZdddZd
eeef fddZdeeef d
dfddZed
efddZed
efddZee	ddd ded
ee fddZeeddeded
efddZe	 ddeed f ded
eeee f fd!dZd
efd"d#Zd
efd$d%Zd
efd&d'Zd(ed
efd)d*Zd
efd+d,Zd(ed
efd-d.Zd(ed
efd/d0Z d(ed
efd1d2Z!d(ed
efd3d4Z"ee#j$d
efd5d6Z%e#j$	d d7ed8ed9e	e d
dfd:d;Z&e#j$d!d8ed9e	e d
dfd=d>Z'd"d@edAed
efdBdCZ(d(ed
dfdDdEZ)ed
efdFdGZ*d
efdHdIZ+d#d
efdJdKZ,d#d
efdLdMZ-d#d
efdNdOZ.ed
efdPdQZ/edRed
efdSdTZ0dUeee1j2f d
efdVdWZ3dXdY Z4dZed
e5eddf fd[d\Z6		<d$dUeee1j2f d]e	e d^ed
e5eddf fd_d`Z7		<d$dUeee1j2f d]e	e d^ed
e5eddf fdadbZ8d
e5eddf fdcddZ9e:d#dedfZ;	<		 d%dgedhe	e< died
e5e=ee>e e>e f ddf fdjdkZ?e	l	m					d&d9dndoedpe	e dqe	e dre	e dse	e dte	e d
dufdvdwZ@e					d'd9dxdoeAdy dpddqddrddse	e dte	e d
dzfd{dwZ@e	m					d(d9d|doeAd} dpddqddrddse	e dte	e d
d~fddwZ@e	m					d(d9ddoeAd} dpddqddrddse	e dte	e d
dfddwZ@e	m					d(d9ddoeAd} dpddqddrddse	e dte	e d
dfddwZ@e	m					d(d9dxdoedpddqddrddse	e dte	e d
dfddwZ@e	m					d(d9edoedpe	e dqe	e dre	e dse	e dte	e d
dfddwZ@	l	m					d&d9edoedpe	e dqe	e dre	e dse	e dte	e d
dfddwZ@ded
efddZBded
efddZCdddZDdeee1j2f d
efddZEd#ded
dfddZFdeGd
efddZH			d)dedpe	e dqe	e dre	e d
ef
ddZId
eGfddZJ			d)dpe	e dqe	e dre	e d
efddZKdd ZLded
efddZMd(eeeNf d
efddZOdeee1j2f d
efddZPd
efddZQd
efddZRdded
efddZSdd(eded
eNfddZTd(ed
efddZUed
efddZVd*dUed]e	e d
efddZWd*ded]e	e d
efddZXed
efddZYed
efddZZed
e=edf fddÄZ[ed
e=edf fddńZ\ed
efddǄZ]ed
efddɄZ^ed
e>e fdd˄Z_ded
efdd΄Z`ded
efddфZad
efddӄZbded
efddքZcded
efdd؄Zdd#died
e1jefddڄZfd+dd݄Zgdeee1j2f d
ehfddZidddZj	d*deee1j2f dte	e d
efddZk	<	 		 d,deee1j2d f diededte	e ded
eehef fddZle	<	 	d-dedte	e d
efddZme	<	 	d-dehdte	e d
ehfddZme	<	 	d-dedte	e d
eehd f fddZm	<	 	d-deee1j2ef diededte	e d
eehef f
ddZme	<	 	d-dediededte	e d
ef
ddZne	<	 	d-dehdiededte	e d
ehf
ddZne	<	 	d-dediededte	e d
eehd f f
ddZn	<	 	d-deee1j2ef diededte	e d
eehef f
ddZne		d.dedte	e de	e<eeoe gepe f  d
efddZqe		d.dehdte	e de	e<eeoe gepe f  d
ehfddZqe		d.deee1j2ef dte	e de	e<eeoe gepe f  d
eehd f fddZqd.ddZqe	<	 	d-dediededte	e d
ef
ddZre	<	 	d-dehdiededte	e d
ehf
ddZre	<	 	d-dediededte	e d
eehd f f
ddZr	<	 	d-deee1j2ef diededte	e d
eehef f
ddZre	<	 	d-dediededte	e d
ef
d dZse	<	 	d-dehdiededte	e d
ehf
ddZse	<	 	d-dediededte	e d
eehd f f
ddZs	<	 	d-deee1j2ef diededte	e d
eehef f
ddZsdd Zted
ehfddZudeee1j2f ded
efd	d
Zvd*dse	e d
dfddZw	d*dte	e d
efddZx	d*dehdte	e d
efddZyedefddZzeded
efddZ{ed
e5e<egef ddf fddZ|eded
efddZ}dS (/  rX   a  Base class for cloud storage file URIs, in the style of the Python standard library's
    [`pathlib` module](https://docs.python.org/3/library/pathlib.html). Instances represent a path
    in cloud storage with filesystem path semantics, and convenient methods allow for basic
    operations like joining, reading, writing, iterating over contents, etc. `CloudPath` almost
    entirely mimics the [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)
    interface, so most familiar properties and methods should be available and behave in the
    expected way.

    Analogous to the way `pathlib.Path` works, instantiating `CloudPath` will instead create an
    instance of an appropriate subclass that implements a particular cloud storage service, such as
    [`S3Path`](../s3path). This dispatching behavior is based on the URI scheme part of a cloud
    storage URI (e.g., `"s3://"`).
    rx   r   N)clientr~   partsr   rQ   rZ   c                G   s   d | _ d | _|r#t|dst|d }t|ddd |D  }| j|dd | j  t|| _t	| j| _
td| j | _|d u rQt|trP|j| _n|| _|d urqt|| jjsqtd|j d| j d| jj d	d
| _d S )N/c                 s   s    | ]}| d V  qdS )r   N)stripr_   pr8   r8   r9   	<genexpr>  s    z%CloudPath.__init__.<locals>.<genexpr>Tr   zClient of type [z'] is not valid for cloud path of type [z]; must be instance of [z;], or None to use default client for this cloud path class.F)_handle_clientrm   endswithjoinr   rx   rg   _strr!   _urlr   
_no_prefix_pathr   rX   r   r[   rB   	__class___dirty)rb   r~   r   r   r8   r8   r9   r      s2   




zCloudPath.__init__c                 C   s$   t | dd d u r| jj | _| jS Nr   )r^   rx   r[   get_default_clientr   ra   r8   r8   r9   r     s   zCloudPath.clientc                 C   sL   | j d ur| j r| j   t| dd }t|dd tjkr$|   d S d S )Nr   file_cache_mode)r   _localexistscloser^   r@   cloudpath_objectclear_cache)rb   r   r8   r8   r9   __del__%  s   
zCloudPath.__del__c                 C   s   | j  }d|v r|d= |S r   )__dict__copyrb   stater8   r8   r9   __getstate__/  s   
zCloudPath.__getstate__r   c                 C   s   | j | d S r2   )r   updater   r8   r8   r9   __setstate__8     zCloudPath.__setstate__c                 C   s   | j t| jd  S r2   )r   lenanchorra   r8   r8   r9   r   ;  s   zCloudPath._no_prefixc                 C   s   | j t| jt| j d  S r2   )r   r   r   drivera   r8   r8   r9   _no_prefix_no_drive?  s   zCloudPath._no_prefix_no_drive.pathr   c                 C   r   r2   r8   r`   r   r   r8   r8   r9   r   C     zCloudPath.is_valid_cloudpathc                 C   r   r2   r8   r   r8   r8   r9   r   I  s   Fc                 C   s<   t | | j }|r|std| d| j d|S )N'z4' is not a valid path since it does not start with ')rm   lowerr   r   rK   )r`   r   r   validr8   r8   r9   r   M  s   c                 C   s   | j j d|  dS )Nz('z'))r   rd   ra   r8   r8   r9   __repr__Z     zCloudPath.__repr__c                 C      | j S r2   )r   ra   r8   r8   r9   __str__]     zCloudPath.__str__c                 C   s   t t| jt| fS r2   )hashr   rd   rm   ra   r8   r8   r9   __hash__`  s   zCloudPath.__hash__otherc                 C   s   t |t| ot| t|kS r2   )r   r   rm   rb   r   r8   r8   r9   __eq__c  s   zCloudPath.__eq__c                 C   s   |   r|   t| jS r2   )is_file_refresh_cacherm   r   ra   r8   r8   r9   
__fspath__f  s   
zCloudPath.__fspath__c                 C   s   t |t| s	tS | j|jk S r2   r   r   NotImplementedr   r   r8   r8   r9   __lt__k     zCloudPath.__lt__c                 C   s   t |t| s	tS | j|jkS r2   r   r   r8   r8   r9   __le__p  r   zCloudPath.__le__c                 C   s   t |t| s	tS | j|jkS r2   r   r   r8   r8   r9   __gt__u  r   zCloudPath.__gt__c                 C   s   t |t| s	tS | j|jkS r2   r   r   r8   r8   r9   __ge__z  r   zCloudPath.__ge__c                 C      dS )zVFor example "bucket" on S3 or "container" on Azure; needs to be defined for each classNr8   ra   r8   r8   r9   r     r   zCloudPath.driveparentsexist_okmodec                 C   r   )zPShould be implemented using the client API without requiring a dir is downloadedNr8   )rb   r   r   r   r8   r8   r9   mkdir  s   zCloudPath.mkdirTc                 C   r   )zMShould be implemented using the client API to create and update modified timeNr8   )rb   r   r   r8   r8   r9   touch  r   zCloudPath.touch  presignexpire_secondsc                 C   s(   |r| j j| |d}|S | j | }|S )N)r   )r   _generate_presigned_url_get_public_url)rb   r   r   urlr8   r8   r9   as_url  s
   zCloudPath.as_urlc                 C   s   t d)NzZCannot change a cloud path's root since all paths are absolute; create a new path instead.)
ValueErrorr   r8   r8   r9   __rtruediv__  s   zCloudPath.__rtruediv__c                 C   r   r2   )r   ra   r8   r8   r9   r        zCloudPath.anchorc                 C      t | S r2   )rm   ra   r8   r8   r9   as_uri  s   zCloudPath.as_uric                 C   s   | j | S r2   )r   _existsrb   follow_symlinksr8   r8   r9   r        zCloudPath.existsc                 C      | j | dkS )Nr   r   _is_file_or_dirr   r8   r8   r9   is_dir  r   zCloudPath.is_dirc                 C   r   )Nfiler   r   r8   r8   r9   r     r   zCloudPath.is_filec                 C   s   |   S r2   )r   ra   r8   r8   r9   fspath     zCloudPath.fspathuric                 C      | |S r2   r8   )r`   r   r8   r8   r9   from_uri  r   zCloudPath.from_uripatternc                 C   s~   t |tjrt |trt|| }n
t|}nt|}d|v r%td|| j	s0|dr4td| j
dkr=td|S )N..z8Relative paths with '..' not supported in glob patterns.r   z%Non-relative patterns are unsupported z.glob is only supported within a bucket or container; you can use `.iterdir` to list buckets; for example, CloudPath('s3://').iterdir())r   osPathLikerX   rm   relative_tor   rH   r   r   r   )rb   r   str_patternr8   r8   r9   _glob_checks  s    

zCloudPath._glob_checksc                    s    fdd  fdd  }| j j| |dD ],\}}t|| d}t|dkr3|d d	kr3qd
d |D }|t|d || qt|S )Nc                      s   t  S r2   r   r8   )Treer8   r9   <lambda>  s    z*CloudPath._build_subtree.<locals>.<lambda>c                    s>   t |d}|du r|r  nd| |< dS | | ||| dS )ziUtility to build a tree from nested defaultdicts with a generator
            of nodes (parts) of a path.N)next)trunkbranchnodesr   next_branchr  _build_treer8   r9   r    s   
z-CloudPath._build_subtree.<locals>._build_tree	recursiver   r>   r   r   c                 s   s    | ]}|V  qd S r2   r8   r   r8   r8   r9   r     s    z+CloudPath._build_subtree.<locals>.<genexpr>)r   	_list_dirrm   r  splitr   r	  dict)rb   r  	file_treefr   r   r  r8   r  r9   _build_subtree  s   zCloudPath._build_subtreer  c                 c   sN    |  |}t| jg |}||D ]}| t|t| jd d   V  qd S )Nr>   )r  _CloudPathSelectablerU   select_fromrm   r   )rb   selectorr  r  rootr   r8   r8   r9   _glob  s   
"zCloudPath._globr7   recurse_symlinksc                 c   sJ    |  |}t|j}tt|t|d}| |d|v pd|v E d H  d S )Nr7   r   **r  r   r   r0   r4   r/   r  rb   r   r7   r  r5   r  r8   r8   r9   glob  s   


zCloudPath.globc                 c   sB    |  |}t|j}tdt| t|d}| |dE d H  d S )N)r  r  Tr   r!  r8   r8   r9   rglob'  s   

zCloudPath.rglobc                 c   s.    | j j| ddD ]\}}|| kr|V  q	d S )NFr  )r   r  )rb   r  r]   r8   r8   r9   iterdir6  s   zCloudPath.iterdirc                 c   s    g }g }|  D ]\}}|du r||n|| q	|r$| ||fV  |D ]}tj| | || |dE dH  q&|sB| ||fV  dS dS )z}Utility to yield tuples in the form expected by `.walk` from the file
        tree constructed by `_build_substree`.
        Ntop_down)itemsappendrX   _walk_results_from_tree)r  treer&  dirsfilesitemr  r   r8   r8   r9   r)  ;  s    z!CloudPath._walk_results_from_treer&  on_errorr   c              
   c   sf    z| j dd}| j| ||dE d H  W d S  ty2 } z|d ur&|| n W Y d }~d S d }~ww )NTr  r%  )r  r)  	Exception)rb   r&  r.  r   r  er8   r8   r9   walkN  s   
zCloudPath.walkrr'   	bufferingencodingerrorsnewlineforce_overwrite_from_cloudforce_overwrite_to_cloudr	   c                 C   r   r2   r8   rb   r   r4  r5  r6  r7  r8  r9  r8   r8   r9   open^     
zCloudPath.openr#   r   r   c                 C   r   r2   r8   r:  r8   r8   r9   r;  j  r<  r%   )r3  r>   r   c                 C   r   r2   r8   r:  r8   r8   r9   r;  v  r<  r&   r   c                 C   r   r2   r8   r:  r8   r8   r9   r;    r<  r$   r   c                 C   r   r2   r8   r:  r8   r8   r9   r;    r<  r   c                 C   r   r2   r8   r:  r8   r8   r9   r;    r<  zIO[Any]c                 C   r   r2   r8   r:  r8   r8   r9   r;    r<  c                    s*    }|r std d|s&tfdddD r&td dkr6  r6td d	j|d
 j  sMjjj	ddd dnj
 jjj||||d}	tfdddD r|	jd fdd}
|
|	_|	_d_jjtjkr|	jfdd}||	_|	S )Nz2Cannot open directory, only files. Tried to open ()c                 3       | ]}| v V  qd S r2   r8   r_   mr   r8   r9   r         z!CloudPath.open.<locals>.<genexpr>)r2  az@File opened for read or append, but it does not exist on cloud: xzCannot open existing file (z) for creation.)r8  Tr   r   g        )r   r4  r5  r6  r7  c                 3   r>  r2   r8   r?  rA  r8   r9   r     rB  )w+rD  rC  rZ   c                     sZ   | i | j sd S j jk r"d }tjj||fd j d d_ d S )Nr>   timesr9  F)r   r   statst_mtimer  utime_upload_local_to_cloud)r   r   	new_mtime)r9  original_mtimerb   wrapped_closer8   r9   _patched_close_upload  s   
z-CloudPath.open.<locals>._patched_close_uploadc                     s   | i |     d S r2   )r   )r   r   )rb   wrapped_close_for_cacher8   r9   _patched_close_empty_cache  s   z2CloudPath.open.<locals>._patched_close_empty_cacherj   )r   r   rE   anyrD   rC   r   r   parentr   rK  rL  r;  r   r   r   r   r   r@   
close_file)rb   r   r4  r5  r6  r7  r8  r9  exists_on_cloudbufferrR  rT  r8   )r9  r   rP  rb   rQ  rS  r9   r;    sD   

	targetc                 C   sh   t | t |urtdt |  |  rtd|  d|| kr#| S | r+|  | j| | |S )NzBThe target based to rename must be an instantiated class of type: r   z6 is a directory; rename/replace the files recursively.)r   rw   r   rE   r   unlinkr   
_move_filerb   rZ  r8   r8   r9   replace  s   
zCloudPath.replacec                 C   s
   |  |S r2   )r^  r]  r8   r8   r9   rename%  s   
zCloudPath.renamec                 C   sX   |   rtd|  dzt|   td|  d ty#   Y nw | j|  d S )Nr   z) is a file; call unlink instead of rmdir.zDirectory not empty: 'z$'. Use rmtree to delete recursively.)r   rF   r	  r$  rI   StopIterationr   _removera   r8   r8   r9   rmdir*  s   

zCloudPath.rmdir
other_pathc                 C   s   | |kS r2   r8   )rb   rc  r8   r8   r9   samefile8  r   zCloudPath.samefile
missing_okc                 C   s*   |   rtd|  d| j| | d S )Nr   z. is a directory; call rmdir instead of unlink.)r   rE   r   ra  )rb   re  r8   r8   r9   r[  <  s
   
zCloudPath.unlinkdatac                 C   sB   t |}| jdd}||W  d   S 1 sw   Y  dS )zOpen the file in bytes mode, write to it, and close the file.

        NOTE: vendored from pathlib since we override open
        https://github.com/python/cpython/blob/3.8/Lib/pathlib.py#L1235-L1242
        wbrA  N)
memoryviewr;  write)rb   rf  viewr  r8   r8   r9   write_bytesD  s   $zCloudPath.write_bytesc                 C   sZ   t |tstd|jj | jd|||d}||W  d   S 1 s&w   Y  dS )zOpen the file in text mode, write to it, and close the file.

        NOTE: vendored from pathlib since we override open
        https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L1146-L1155
        zdata must be str, not %srF  r   r5  r6  r7  N)r   rm   rw   r   rd   r;  ri  )rb   rf  r5  r6  r7  r  r8   r8   r9   
write_textO  s
   
$zCloudPath.write_textc                 C   s8   | j dd}| W  d    S 1 sw   Y  d S )NrbrA  r;  read)rb   r  r8   r8   r9   
read_bytesa  s   $zCloudPath.read_bytesc                 C   s>   | j d|||d}| W  d    S 1 sw   Y  d S )Nr2  rl  ro  )rb   r5  r6  r7  r  r8   r8   r9   	read_texte  s   $zCloudPath.read_textc                 C   r   NFr8   ra   r8   r8   r9   is_junctionn     zCloudPath.is_junctionfuncc                    s    j |}t|r||i |}t|trt|} |S t|tjj	rIt
|dkrIt|d trIt|ts<t|nt}| fdd|D S |S )zSome functions we can just dispatch to the pathlib version
        We want to do this explicitly so we don't have to support all
        of pathlib and subclasses can override individually if necessary.
        r   c                 3   s,    | ]}t ||jkr t |V  qd S r2   )_resolver  _new_cloudpathr   ra   r8   r9   r     s    "z.CloudPath._dispatch_to_path.<locals>.<genexpr>)r   __getattribute__callabler   r   rw  rx  collectionsabcSequencer   r1   r   r4   )rb   rv  r   r   path_versionsequence_classr8   ra   r9   _dispatch_to_pathu  s"   

zCloudPath._dispatch_to_pathc                 C   s   t |ttfs	tS | d|S )N__truediv__)r   rm   r   r   r  r   r8   r8   r9   r    s   zCloudPath.__truediv__pathsegmentsc                 G   s   | j dg|R  S )Njoinpathr  rb   r  r8   r8   r9   r    s   zCloudPath.joinpathc                 C      | S r2   r8   ra   r8   r8   r9   absolute  ru  zCloudPath.absolutec                 C   r   NTr8   ra   r8   r8   r9   is_absolute  ru  zCloudPath.is_absolutestrictc                 C   r  r2   r8   )rb   r  r8   r8   r9   resolve  ru  zCloudPath.resolvewalk_upc              	   C   s   t |tst|  d| d| j|jkr't|  d| j d| d|j dt|d}tjdk r6|d | jj	|jfi |S )	Nz is a cloud path, but z is notz is a z path, but z path)r  r-   r  )
r   rX   r   r   r  sysversion_infopopr   r  )rb   r   r  r   r8   r8   r9   r    s   



zCloudPath.relative_toc                 C   s&   z|  | W dS  ty   Y dS w )NTF)r  r   r   r8   r8   r9   is_relative_to  s   
zCloudPath.is_relative_toc                 C   
   |  dS )NrU   r  ra   r8   r8   r9   rU        
zCloudPath.namec                 C   st   t jdk r	td|| j| j r|t| j| j d  }n|| jr0|t| jd d  }t| jj	||dS )Nr<   z)full_match requires Python 3.13 or higherr>   r  )
r  r  NotImplementedErrorr   r   r   r   r   r   
full_match)rb   r   r7   r8   r8   r9   r    s   

zCloudPath.full_matchpath_patternc                 C   sd   | | j| j d r|t| j| j d d  }t|d}tjdk r(|d | jd|fi |S )Nr   r  r-   r7   match)	r   r   r   r   r  r  r  r  r  )rb   r  r7   r   r8   r8   r9   r    s   


zCloudPath.matchc                 C   s   t jdk r	td| dS )Nr<   z%parser requires Python 3.13 or higherparser)r  r  r  r  ra   r8   r8   r9   r    s   

zCloudPath.parserc                 C   r  )NrV  r  ra   r8   r8   r9   rV    r  zCloudPath.parentc                 C   r  )Nr   r  ra   r8   r8   r9   r     r  zCloudPath.parentsc                 C   s0   |  d}|d dkr|dd  }| jg|R S )Nr   r   r   r>   )r  r   )rb   r   r8   r8   r9   r     s   
zCloudPath.partsc                 C   r  )Nstemr  ra   r8   r8   r9   r    r  zCloudPath.stemc                 C   r  )Nsuffixr  ra   r8   r8   r9   r    r  zCloudPath.suffixc                 C   r  )Nsuffixesr  ra   r8   r8   r9   r    r  zCloudPath.suffixesr  c                 C   s2   z|  d|W S  ty   | || j  Y S w )N	with_stem)r  AttributeError	with_namer  )rb   r  r8   r8   r9   r    s
   zCloudPath.with_stemrU   c                 C      |  d|S )Nr  r  )rb   rU   r8   r8   r9   r    r   zCloudPath.with_namec                 G   s   |  d|S )zCreate a new CloudPath with the same client out of the given segments.
        The first segment will be interpreted as the bucket/container name.
        r   )rx  r   r  r8   r8   r9   with_segments  s   zCloudPath.with_segmentsr  c                 C   r  )Nwith_suffixr  )rb   r  r8   r8   r9   r    r   zCloudPath.with_suffixc                 O   sN   |    | j|}t|r||i |}t|ttfr%| }| |S |S r2   )	r   r   ry  rz  r   r   r   r  rx  )rb   rv  r   r   r~  r8   r8   r9   _dispatch_to_local_cache_path  s   
z'CloudPath._dispatch_to_local_cache_pathc                 C   s    t d| j d | jd|dS )zNote: for many clients, we may want to override so we don't incur
        network costs since many of these properties are available as
        API calls.
        z%stat not implemented as API call for z_ so file must be downloaded to calculate stats; this may take a long time depending on filesizerK  )r   )r"   r   r  r   r8   r8   r9   rK  -  s   zCloudPath.statrS   c                 C   r   )z,Return a CloudPathInfo object for this path.rR   ra   r8   r8   r9   info8  r   zCloudPath.infodestinationc                 C   s   t |}|  std|  |  r#| r|| j }| j| |S |jdd | 	 D ] }t
| }|ds<|d }t
|t|d  }|||  q-|S )Nz-Cannot download because path does not exist: T)r   r   )r
   r   rG   r   r   rU   r   _download_filer   r$  rm   r   r   download_to)rb   r  r  relrel_destr8   r8   r9   r  =  s   

zCloudPath.download_toc                 C   s(   |   rtd|  d| j|  dS )z Delete an entire directory tree.r   z* is a file; call unlink instead of rmtree.N)r   rF   r   ra  ra   r8   r8   r9   rmtreeS  s
   
zCloudPath.rmtreesourcec                 C   sh   t |}| r| D ]}| |j j||d q| S |  r)|  r)| |j }n| }|j||d |S )z-Upload a file or directory to the cloud path.rJ  )r
   r   r$  rU   upload_fromr   _upload_file_to_cloud)rb   r  r9  r   dstr8   r8   r9   r  [  s   zCloudPath.upload_frompreserve_metadata
remove_srcc                 C   sB  |   std|  dt|}|  r%| j||d}tttt	f |S t
|ts/| |S | j|ju rx|  rB| rB|| j }|d u rQtjdd dv }|sl|  rl| j|  jkrltd| d|  d	tt	| jj| ||d
S |  r| rtttt	f |j| j|dS tttt	f || j j| j|dS )Nr   z must exist to copy.rJ  %CLOUDPATHLIB_FORCE_OVERWRITE_TO_CLOUDFalse1truezFile (z) is newer than (z5). To overwrite pass `force_overwrite_to_cloud=True`.)r  )r   r   rA   
to_anypathr   copytreer    r   r
   r,   r   rX   r  r   rU   r  environgetr   rK  rL  rO   r\  r   r  r   )rb   rZ  r   r  r9  r  r  resultr8   r8   r9   _copys  sT   






zCloudPath._copyc                 C   r   r2   r8   rb   rZ  r   r  r9  r8   r8   r9   r        zCloudPath.copyc                 C   r   r2   r8   r  r8   r8   r9   r     r  c                 C   r   r2   r8   r  r8   r8   r9   r     r  c                 C      | j ||||ddS )z6Copy self to target folder or file, if self is a file.Fr   r  r9  r  r  r  r8   r8   r9   r        
target_dirc                 C   r   r2   r8   rb   r  r   r  r9  r8   r8   r9   	copy_into  r  zCloudPath.copy_intoc                 C   r   r2   r8   r  r8   r8   r9   r    r  c                 C   r   r2   r8   r  r8   r8   r9   r    r  c                 C   6   t || j }| j||||dd}ttttf |S )z9Copy self into target directory, preserving the filename.Fr  rA   r  rU   r  r    r   r
   r,   rb   r  r   r  r9  target_pathr  r8   r8   r9   r       ignorec                 C   r   r2   r8   rb   r  r9  r  r8   r8   r9   r  
     zCloudPath.copytreec                 C   r   r2   r8   r  r8   r8   r9   r    r  c                 C   r   r2   r8   r  r8   r8   r9   r    r  c                 C   s   |   std|  dt|}| r!| r!td| dt|  }|dur7|| j	dd |D }nt
 }|jddd	 |D ]0}|j|v rKqC| rZ|j||j |d
 qC|  rs|j||j|jdrkdnd  ||d qC|S )z1Copy self to a directory, if self is a directory.zOrigin path z@ must be a directory. To copy a single file use the method copy.zDestination path z! of copytree must be a directory.Nc                 S      g | ]}|j qS r8   rU   )r_   rD  r8   r8   r9   rc   3      z&CloudPath.copytree.<locals>.<listcomp>TrE  rJ  r   r  )r9  r  )r   rF   rA   r  r   r   rC   listr$  r   setr   rU   r   r  r   )rb   r  r9  r  contentsignored_namessubpathr8   r8   r9   r  "  s:   




c                 C   r   r2   r8   r  r8   r8   r9   moveI  r  zCloudPath.movec                 C   r   r2   r8   r  r8   r8   r9   r  R  r  c                 C   r   r2   r8   r  r8   r8   r9   r  [  r  c                 C   r  )z2Move self to target location, removing the source.Tr  r  r  r8   r8   r9   r  d  r  c                 C   r   r2   r8   r  r8   r8   r9   	move_intot  r  zCloudPath.move_intoc                 C   r   r2   r8   r  r8   r8   r9   r  }  r  c                 C   r   r2   r8   r  r8   r8   r9   r    r  c                 C   r  )zQMove self into target directory, preserving the filename and removing the source.Tr  r  r  r8   r8   r9   r    r  c                 C   s6   | j  r| j  r| j   dS t| j  dS dS )zRemoves cache if it existsN)r   r   r   r[  shutilr  ra   r8   r8   r9   r     s
   

zCloudPath.clear_cachec                 C   s   | j j| j S )z!Cached local version of the file.)r   _local_cache_dirr   ra   r8   r8   r9   r     s   zCloudPath._localc                 G   sL   t |}|dr|dd }|| js| j | }| jj|g|R  S )zUse the scheme, client, cache dir of this cloudpath to instantiate
        a new cloudpath of the same type with the path passed.

        Used to make results of iterdir and joins have a unified client + cache.
        r   r>   N)rm   r   r   r   rX   )rb   r   r   r8   r8   r9   rx    s   
zCloudPath._new_cloudpathc                 C   s   z|   }W n
 ty   Y d S w |d u r tjdd dv }|s0| j r0| j  j|jk rK| jj	j
ddd | | j tj| j|j|jfd | jrZtd| j d|  d	| j  j|jkrotd| j d|  d
d S )N'CLOUDPATHLIB_FORCE_OVERWRITE_FROM_CLOUDr  r  TrE  rH  Local file () for cloud path (a  ) has been changed by your code, but is being requested for download from cloud. Either (1) push your changes to the cloud, (2) remove the local file, or (3) pass `force_overwrite_from_cloud=True` to overwrite; or set env var CLOUDPATHLIB_FORCE_OVERWRITE_FROM_CLOUD=1.z) is newer on disk, but is being requested for download from cloud. Either (1) push your changes to the cloud, (2) remove the local file, or (3) pass `force_overwrite_from_cloud=True` to overwrite; or set env var CLOUDPATHLIB_FORCE_OVERWRITE_FROM_CLOUD=1.)rK  rM   r  r  r  r   r   r   rL  rV  r   r  rM  r   rN   rP   )rb   r8  statsr8   r8   r9   r     s8   	zCloudPath._refresh_cachec                 C   sR   | j  r	td| j| j |d}|  }tj| j |j|jfd d| _d| _	|S )z.Uploads cache file at self._local to the cloudz2Only individual files can be uploaded to the cloudrJ  rH  FN)
r   r   r   r  rK  r  rM  rL  r   r   )rb   r9  uploadedr  r8   r8   r9   rN    s   
z CloudPath._upload_local_to_cloud
local_pathc                 C   s   |du rt jdd dv }|r| j||  | S z|  }W n ty+   d}Y nw |r6| j|jkr?| j||  | S t	d| j
 d|  d)zfUploads file at `local_path` to the cloud if there is not a newer file
        already there.
        Nr  r  r  r  r  z) is newer in the cloud disk, but is being requested to be uploaded to the cloud. Either (1) redownload changes from the cloud or (2) pass `force_overwrite_to_cloud=True` to overwrite; or set env var CLOUDPATHLIB_FORCE_OVERWRITE_TO_CLOUD=1.)r  r  r  r   r   _upload_filerK  rM   rL  rO   r   )rb   r  r9  r  r8   r8   r9   r    s4   zCloudPath._upload_file_to_cloud_source_typec                 C   s6   zddl m} || j| W S  ty   Y dS w )zVPydantic special method. See
        https://docs.pydantic.dev/2.0/usage/types/custom/r   )core_schemaN)pydantic_corer   no_info_after_validator_functionvalidate
any_schemaImportError)r`   r  _handlerr  r8   r8   r9   __get_pydantic_core_schema__2  s   z&CloudPath.__get_pydantic_core_schema__vc                 C   r   )z[Used as a Pydantic validator. See
        https://docs.pydantic.dev/2.0/usage/types/custom/r8   )r`   r  r8   r8   r9   r  @     zCloudPath.validatec                 c   s    | j V  dS )zgPydantic special method. See
        https://pydantic-docs.helpmanual.io/usage/types/#custom-data-typesN)	_validatery   r8   r8   r9   __get_validators__F  s   zCloudPath.__get_validators__valuec                 C   r   )zlUsed as a Pydantic validator. See
        https://pydantic-docs.helpmanual.io/usage/types/#custom-data-typesr8   )r`   r  r8   r8   r9   r  L  r  zCloudPath._validaterj   ).F)FFN)TN)Fr   Tr  )TNF)r2  r3  NNNNN)NNNNN)r3  NNNNN)NNNr2   )rZ   rS   )TFNF)TFN)NN)~rd   rk   rl   r   rT   rn   rm   r   r,   r   r   rp   r   r   r   r   r   r   r   r   r   classmethodro   r*   r   r   r   intr   r   r   r   r   r   r   r|  abstractmethodr   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r  r"  r#  r$  staticmethodr)  r   r   r   r1  r;  r   r^  r_  rb  rd  r[  bytesrk  rm  rq  rr  rt  r  r   r  r  r  r  r  r  r  rU   r  r  r  rV  r   r   r  r  r  r  r  r  r  r  stat_resultrK  r  r
   r  r  r  r  r   r  r   r   r  r  r  r   r   rx  r   rN  r  r  r  r  r  r8   r8   r8   r9   rX      sp  
 
+

	
 	


								
^

	#


>






'




	$.,()	metaclassr   c                 C   sX   d}d}t | |D ]}|r|dkrq|dkr!||\}}}q|| | }q|p+|S )Nr   r  r   r   )rm   r  
rpartition)r   sepnewpathrU   r]   r8   r8   r9   rw  Z  s   rw  c                   @   s   e Zd ZdeddfddZdS )_CloudPathSelectableAccessorscandir_funcrZ   Nc                 C   s
   || _ d S r2   )scandir)rb   r  r8   r8   r9   r   v     
z%_CloudPathSelectableAccessor.__init__)rd   rk   rl   r   r   r8   r8   r8   r9   r  u  s    r  c                   @   s   e Zd Z	ddedee dededdf
dd	Zdefd
dZddedefddZ	defddZ
defddZedefddZdd Zeedd deed ddf fddZeZdd ZdS ) r  TrU   r   childrenr   rZ   Nc                 C   s(   || _ || _|| _|| _t| j| _d S r2   )_name_all_children_parentsr   r  r  	_accessor)rb   rU   r   r  r   r8   r8   r9   r   {  s
   z_CloudPathSelectable.__init__c                 C   s   d | j| jg S )Nr   )r   r  rU   ra   r8   r8   r9   r     r   z_CloudPathSelectable.__repr__Fr   c                 C   s
   | j d uS r2   )r  r   r8   r8   r9   r     r  z_CloudPathSelectable.is_dirc                 C   r   r2   )r   ra   r8   r8   r9   r     r   z_CloudPathSelectable.existsc                 C   r   rs  r8   ra   r8   r8   r9   
is_symlink  ru  z_CloudPathSelectable.is_symlinkc                 C   r   r2   )r  ra   r8   r8   r9   rU     r   z_CloudPathSelectable.namec                 C   s*   t || j| jg | j|d || jv dS )N)r   )r  r  rU   r  r  )rb   partr8   r8   r9   _make_child_relpath  s   z(_CloudPathSelectable._make_child_relpathr  )r  NNc                 #   s      fdd j  D V  d S )Nc                 3   s*    | ]\}}t | j jg |V  qd S r2   )r  r  r  )r_   childgrand_childrenr  r8   r9   r     s
    
z/_CloudPathSelectable.scandir.<locals>.<genexpr>)r  r'  r  r8   r  r9   r    s   
z_CloudPathSelectable.scandirc                 c   s    t t}| | 9}|D ]}||  | q| dd |d D dd |d D fV  |d D ]	}| E d H  q1W d    d S 1 sFw   Y  d S )Nc                 S   r  r8   r  )r_   r  r8   r8   r9   rc     r  z-_CloudPathSelectable.walk.<locals>.<listcomp>TF)r   r  r  r   r(  r1  )rb   
dirs_filesr'  r  	child_dirr8   r8   r9   r1    s   ("z_CloudPathSelectable.walkr  r  )rd   rk   rl   rm   r   r   ro   r   r   r   r   r  rp   rU   r  r  r   r   r  _scandirr1  r8   r8   r8   r9   r  z  s:    
	r  r  )cr|  r{  r   collections.abc
contextlibr   ior   r   r   r   r	   r  pathlibr
   r   r   r   r  r  typesr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    urllib.parser!   warningsr"   	_typeshedr#   r$   r%   r&   r'   r  r*   typing_extensionsr,   r/   r0   r3   r1   r;   pathlib._locallegacy.globcloudpathlib.enumsr@   r  rA   
exceptionsrB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r   rQ   cloudpath_inforS   rT   rq   rm   rn   rr   rs   r|   ABCMetar}   rX   rw  r  r  r8   r8   r8   r9   <module>   s   
 P


D$B           