o
    oĎi"                     @   s   U d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlZd dl	Z	d dl
m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 ddlmZ dd	lmZ d
dlmZ G dd deZg Zee ed< e j dd Z!dS )    N)md5)PathPurePosixPath)TemporaryDirectory)sleep)CallableClassVarDictIterableListOptionalTupleUnion   )Client)FileCacheMode   )	LocalPathc                
       s`  e Zd ZU dZdZeee  ed< ee	e
ejf  ed< dddejddee	e
ejf  dee	e
ef  dee	e
ejf  d	ee f fd
dZedefddZedefddZedefddZdddefddZde	e
ejf ddfddZddde	e
ejf defddZdddefddZdAdddefdd ZdAdddefd!d"Zdddee
 fd#d$Z	%dBddde e!def  fd&d'Z"ddde
fd(d)Z#	dAd*dd+dd,eddfd-d.Z$dAddd/eddfd0d1Z%dddej&fd2d3Z'dAddd4eddfd5d6Z(de	e
ejf ddddfd7d8Z)ddde*fd9d:Z+ddde
fd;d<Z,	=dCddd>e-de
fd?d@Z.  Z/S )DLocalClientzAbstract client for accessing objects the local filesystem. Subclasses are as a monkeypatch
    substitutes for normal Client subclasses when writing tests.N_default_storage_temp_dir_local_storage_dir)local_storage_dirfile_cache_modelocal_cache_dircontent_type_methodr   r   r   r   c                   s   || _ t j|||d d S )N)r   r   r   )r   super__init__)selfr   r   r   r   argskwargs	__class__ Z/home/jeff/fluffinator/venv/lib/python3.10/site-packages/cloudpathlib/local/localclient.pyr      s   	
zLocalClient.__init__returnc                 C   s*   | j du rt | _ t| j  t| j jS )a  Return the default storage directory for this client class. This is used if a client
        is instantiated without a storage directory being explicitly provided. In this usage,
        "storage" refers to the local storage that simulates the cloud.
        N)r   r   _temp_dirs_to_cleanappendr   nameclsr"   r"   r#   get_default_storage_dir-   s   
z#LocalClient.get_default_storage_dirc                 C   s   d| _ |  S )a3  Reset the default storage directly. This tears down and recreates the directory used by
        default for this client class when instantiating a client without explicitly providing
        a storage directory. In this usage, "storage" refers to the local storage that simulates
        the cloud.
        N)r   r*   r(   r"   r"   r#   reset_default_storage_dir8   s   z%LocalClient.reset_default_storage_dirc                 C   s   | j du r	|  S t| j S )a  The local directory where files are stored for this client. This storage directory is
        the one that simulates the cloud. If no storage directory was provided on instantiating the
        client, the default storage directory for this client class is used.
        N)r   r*   r   )r   r"   r"   r#   r   B   s   

zLocalClient.local_storage_dir
cloud_pathr   c                 C   s   | j |j S N)r   
_no_prefixr   r,   r"   r"   r#   _cloud_path_to_localN   s   z LocalClient._cloud_path_to_local
local_pathc                 C   s0   t |}| jjj}| | t|| j S r-   )r   _cloud_meta
path_classcloud_prefix	CloudPathr   relative_tor   )r   r1   r4   r"   r"   r#   _local_to_cloud_pathQ   s
   
z LocalClient._local_to_cloud_pathc                 C   sx   t |}|jjddd zt| || W |S  ty;   td |jjddd td t| || Y |S w )NTexist_okparentsg      ?)r   parentmkdirshutilcopyfiler0   FileNotFoundErrorr   )r   r,   r1   r"   r"   r#   _download_fileX   s   	zLocalClient._download_filec                 C   s   |  | S r-   )r0   existsr/   r"   r"   r#   _existsh   s   zLocalClient._existsTc                 C   4   t |d}tjdk r|d | |jdi |S N)follow_symlinks)      rE   r"   )dictsysversion_infopopr0   is_dirr   r,   rE   r   r"   r"   r#   _is_dirk      


zLocalClient._is_dirc                 C   rC   rD   )rH   rI   rJ   rK   r0   is_filerM   r"   r"   r#   _is_filer   rO   zLocalClient._is_filec                 C   s*   |  |rdS | |rdS td| )Ndirfilez-Path could not be identified as file or dir: )rN   rQ   r?   r/   r"   r"   r#   _is_file_or_diry   s
   

zLocalClient._is_file_or_dirFc                 c   s<    |rdnd}|  ||D ]}| || fV  qd S )Nz**/**)r0   globr7   rL   )r   r,   	recursivepatternobjr"   r"   r#   	_list_dir   s
   zLocalClient._list_dirc                 C   s   t | |  S r-   )r   r0   
read_bytes	hexdigestr/   r"   r"   r#   _md5   s   zLocalClient._md5srcdst
remove_srcc                 C   sP   |  |jjddd |r|  ||  | |S t|  ||  | |S NTr8   )r0   r;   r<   replacer=   copy)r   r^   r_   r`   r"   r"   r#   
_move_file   s   zLocalClient._move_file
missing_okc                 C   sR   |  |}|s| std| | r|  d S | r't| d S d S )NzFile does not exist: )r0   rA   r?   rP   unlinkrL   r=   rmtree)r   r,   re   local_storage_pathr"   r"   r#   _remove   s   
zLocalClient._removec                 C   s2   |  | }td d |jd d d |jd |jd f
S r-   )r0   statosstat_resultr4   st_sizest_mtime)r   r,   rl   r"   r"   r#   _stat   s   zLocalClient._statr9   c                 C   s@   |  |}| r|std| |jjddd |  d S )NzFile exists: Tr8   )r0   rA   FileExistsErrorr;   r<   touch)r   r,   r9   rh   r"   r"   r#   _touch   s
   
zLocalClient._touchc                 C   s*   |  |}|jjddd t|| |S ra   )r0   r;   r<   r=   rc   )r   r1   r,   r_   r"   r"   r#   _upload_file   s   
zLocalClient._upload_filec                 C   s4   | j d u r
dd }n| j }d|t| |d iS )Nc                 S   s   dS )N)NNr"   )xr"   r"   r#   <lambda>   s    z+LocalClient._get_metadata.<locals>.<lambda>content_typer   )r   strr0   )r   r,   r   r"   r"   r#   _get_metadata   s
   

zLocalClient._get_metadatac                 C   s   |  S r-   )as_urir/   r"   r"   r#   _get_public_url   s   zLocalClient._get_public_url  expire_secondsc                 C   s   t d)Nz1Cannot generate a presigned URL for a local path.)NotImplementedError)r   r,   r|   r"   r"   r#   _generate_presigned_url   s   z#LocalClient._generate_presigned_url)T)F)r{   )0__name__
__module____qualname____doc__r   r   r   r   __annotations__r   rw   rk   PathLike	mimetypes
guess_typer   r   r   classmethodr   r*   r+   propertyr   r0   r7   r@   boolrB   rN   rQ   rT   r
   r   rZ   r]   rd   ri   rl   ro   rr   rs   r	   rx   rz   intr~   __classcell__r"   r"   r    r#   r      s   
 
	 	



r   r%   c                  C   s   t D ]} |   qd S r-   )r%   cleanup)temp_dirr"   r"   r#   clean_temp_dirs   s   
r   )"atexithashlibr   r   rk   pathlibr   r   r=   rI   tempfiler   timer   typingr   r   r	   r
   r   r   r   r   clientr   enumsr   	localpathr   r   r%   r   registerr   r"   r"   r"   r#   <module>   s$   
 ( E