o
    Êôj3:  ã                   @   s”  d Z ddlmZ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 eZg ZzddlmZ W n ey>   e d¡ Y nw zddlmZ W n eyU   e d¡ Y nw zddlmZ W n eyl   e d	¡ Y nw zdd
lmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ W n ey•   e d¡ Y n
w ddl%Z%e% &e"¡ dd„ Z'dd„ Z(ddœdd„Z)dddœdd„Z*	d dddœdd„Z+G dd„ deƒZ,dS )!zO
FileSystem abstraction to interact with various local and remote filesystems.
é    )Ú_is_path_likeÚ_stringify_path)ÚFileSelectorÚFileTypeÚFileInfoÚ
FileSystemÚLocalFileSystemÚSubTreeFileSystemÚ_MockFileSystemÚFileSystemHandlerÚPyFileSystemÚ_copy_filesÚ_copy_files_selector)ÚAzureFileSystemr   )ÚHadoopFileSystemr   )ÚGcsFileSystemr   )
ÚAwsDefaultS3RetryStrategyÚAwsStandardS3RetryStrategyÚS3FileSystemÚ
S3LogLevelÚS3RetryStrategyÚensure_s3_initializedÚfinalize_s3Úensure_s3_finalizedÚinitialize_s3Úresolve_s3_regionr   Nc                 C   s$   | t v rtd | ¡ƒ‚td | ¡ƒ‚)Nz<The pyarrow installation is not built with support for '{0}'z*module 'pyarrow.fs' has no attribute '{0}')Ú_not_importedÚImportErrorÚformatÚAttributeError)Úname© r!   ú]/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/pyarrow/fs.pyÚ__getattr__I   s   þÿr#   c                 C   s^   t  | ¡\}}| |¡}|r-| |g¡d }|jtjkr(td |jj	|j
| ¡ƒ‚t||ƒ}|S )Nr   z›The path component of the filesystem URI must point to a directory but it has a type: `{}`. The path component is `{}` and the given filesystem URI is `{}`)r   Úfrom_uriÚnormalize_pathÚget_file_infoÚtyper   Ú	DirectoryÚ
ValueErrorr   r    Úpathr	   )ÚuriÚ
filesystemÚprefixZprefix_infor!   r!   r"   Ú_filesystem_from_strU   s   
ÿý
r.   F©Úuse_mmapc                C   sŽ   t | tƒr| S t | tƒr|rtdƒ‚t| ƒS zdd l}W n	 ty%   Y nw t | |jƒr>t| ƒj	dkr8t
|d�S tt| ƒƒS td t| ƒ¡ƒ‚)NzXSpecifying to use memory mapping not supported for filesystem specified as an URI stringr   r   r/   zlUnrecognized filesystem: {}. `filesystem` argument must be a FileSystem instance or a valid file system URI')Ú
isinstancer   Ústrr)   r.   Úfsspecr   ÚAbstractFileSystemr'   Ú__name__r   r   ÚFSSpecHandlerÚ	TypeErrorr   )r,   r0   r3   r!   r!   r"   Ú_ensure_filesystemi   s.   

ÿÿ
ÿþr8   )Ú
memory_mapc             
   C   s:  t | ƒs|durtdƒ‚|| fS |dur6t||d�}t|tƒr$t| ƒ} n	t| tƒs-tdƒ‚| | ¡} || fS t| ƒ} t|d�}z| 	| ¡}W n tyS   d}d}Y nw |j
tjk}|s”zt | ¡\}} W || fS  ty“ } zdt|ƒvr}dt|ƒvr†‚ W Y d}~|| fS W Y d}~|| fS d}~ww | | ¡} || fS )z`
    Return filesystem/path from path which could be an URI or a plain
    filesystem path.
    Nzg'filesystem' passed but the specified path is file-like, so there is nothing to open with 'filesystem'.r/   zPExpected string path; path-like objects are only allowed with a local filesystemFzempty schemezCannot parse URI)r   r)   r8   r1   r   r   r2   r7   r%   r&   r'   r   ÚNotFoundr   r$   )r*   r,   r9   Ú	file_infoZexists_locallyÚer!   r!   r"   Ú_resolve_filesystem_and_path‡   sR   ÿ


ÿ

þö
þ
û€ö
r=   i   T)Ú
chunk_sizeÚuse_threadsc                C   sj   t | |ƒ\}}t ||ƒ\}}	| |¡}
|
jtjkr*t|dd�}t||||	||ƒ dS t||||	||ƒ dS )a¦  
    Copy files between FileSystems.

    This functions allows you to recursively copy directories of files from
    one file system to another, such as from S3 to your local machine.

    Parameters
    ----------
    source : string
        Source file path or URI to a single file or directory.
        If a directory, files will be copied recursively from this path.
    destination : string
        Destination file path or URI. If `source` is a file, `destination`
        is also interpreted as the destination file (not directory).
        Directories will be created as necessary.
    source_filesystem : FileSystem, optional
        Source filesystem, needs to be specified if `source` is not a URI,
        otherwise inferred.
    destination_filesystem : FileSystem, optional
        Destination filesystem, needs to be specified if `destination` is not
        a URI, otherwise inferred.
    chunk_size : int, default 1MB
        The maximum size of block to read before flushing to the
        destination file. A larger chunk_size will use more memory while
        copying but may help accommodate high latency FileSystems.
    use_threads : bool, default True
        Whether to use multiple threads to accelerate copying.

    Examples
    --------
    Inspect an S3 bucket's files:

    >>> s3, path = fs.FileSystem.from_uri(
    ...            "s3://registry.opendata.aws/roda/ndjson/")
    >>> selector = fs.FileSelector(path)
    >>> s3.get_file_info(selector)
    [<FileInfo for 'registry.opendata.aws/roda/ndjson/index.ndjson':...]

    Copy one file from S3 bucket to a local directory:

    >>> fs.copy_files("s3://registry.opendata.aws/roda/ndjson/index.ndjson",
    ...               "file:///{}/index_copy.ndjson".format(local_path))

    >>> fs.LocalFileSystem().get_file_info(str(local_path)+
    ...                                    '/index_copy.ndjson')
    <FileInfo for '.../index_copy.ndjson': type=FileType.File, size=...>

    Copy file using a FileSystem object:

    >>> fs.copy_files("registry.opendata.aws/roda/ndjson/index.ndjson",
    ...               "file:///{}/index_copy.ndjson".format(local_path),
    ...               source_filesystem=fs.S3FileSystem())
    T©Ú	recursiveN)r=   r&   r'   r   r(   r   r   r   )ÚsourceÚdestinationZsource_filesystemZdestination_filesystemr>   r?   Z	source_fsÚsource_pathZdestination_fsÚdestination_pathr;   Z
source_selr!   r!   r"   Ú
copy_filesÁ   s"   8ÿÿ
þþrF   c                   @   s´   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	dd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*S )+r6   a  
    Handler for fsspec-based Python filesystems.

    https://filesystem-spec.readthedocs.io/en/latest/index.html

    Parameters
    ----------
    fs : FSSpec-compliant filesystem instance

    Examples
    --------
    >>> PyFileSystem(FSSpecHandler(fsspec_fs)) # doctest: +SKIP
    c                 C   s
   || _ d S ©N)Úfs)ÚselfrH   r!   r!   r"   Ú__init__  s   
zFSSpecHandler.__init__c                 C   s   t |tƒr| j|jkS tS rG   ©r1   r6   rH   ÚNotImplemented©rI   Úotherr!   r!   r"   Ú__eq__  ó   
zFSSpecHandler.__eq__c                 C   s   t |tƒr| j|jkS tS rG   rK   rM   r!   r!   r"   Ú__ne__#  rP   zFSSpecHandler.__ne__c                 C   s$   | j j}t|tƒr|d }d |¡S )Nr   z
fsspec+{0})rH   Úprotocolr1   Úlistr   )rI   rR   r!   r!   r"   Úget_type_name(  s   

zFSSpecHandler.get_type_namec                 C   s   |S rG   r!   ©rI   r*   r!   r!   r"   r%   .  s   zFSSpecHandler.normalize_pathc                 C   sR   |d }|d dkrt j}n|d dkrt j}d }nt j}t| ||| dd ¡d�S )NÚsizer'   ÚfileÚ	directoryÚmtime)rV   rY   )r   ÚFiler(   ÚUnknownr   Úget)r*   ÚinforV   Úftyper!   r!   r"   Ú_create_file_info1  s   zFSSpecHandler._create_file_infoc              	   C   sZ   g }|D ]&}z| j  |¡}W n ty    | t|tjƒ¡ Y qw | |  ||¡¡ q|S rG   )rH   r]   ÚFileNotFoundErrorÚappendr   r   r:   r_   )rI   ÚpathsÚinfosr*   r]   r!   r!   r"   r&   >  s   ÿzFSSpecHandler.get_file_infoc           	      C   sª   | j  |j¡s| j  |j¡rt|jƒ‚|jrg S t|jƒ‚|jr#d }nd}g }| j j|j|ddd�}| 	¡ D ]\}}| 
d¡}|j 
d¡}||krR| |  ||¡¡ q6|S )Né   T)ÚmaxdepthÚwithdirsÚdetailú/)rH   ÚisdirÚbase_dirÚexistsÚNotADirectoryErrorZallow_not_foundr`   rA   ÚfindÚitemsÚstripra   r_   )	rI   Úselectorre   rc   Zselected_filesr*   r]   Ú_pathrj   r!   r!   r"   Úget_file_info_selectorI  s(   


ÿ
€z$FSSpecHandler.get_file_info_selectorc                 C   s,   z| j j||d� W d S  ty   Y d S w )N)Úcreate_parents)rH   ÚmkdirÚFileExistsError)rI   r*   rA   r!   r!   r"   Ú
create_dirf  s
   ÿzFSSpecHandler.create_dirc                 C   s   | j j|dd� d S ©NTr@   )rH   ÚrmrU   r!   r!   r"   Ú
delete_dirm  s   zFSSpecHandler.delete_dirc                 C   st   z
| j j|dd�}W n ty   |rY d S ‚ w |D ]}| j  |¡r+| j j|dd� q| j  |¡r7| j  |¡ qd S )NF)rg   Tr@   )rH   Úlistdirr`   ri   rx   Úisfile)rI   r*   Úmissing_dir_okÚsubpathsÚsubpathr!   r!   r"   Ú_delete_dir_contentsp  s   ý€üz"FSSpecHandler._delete_dir_contentsc                 C   s*   |  d¡dkrtd|dƒ‚|  ||¡ d S )Nrh   Ú z$delete_dir_contents called on path 'ú')ro   r)   r   )rI   r*   r|   r!   r!   r"   Údelete_dir_contents}  s
   ÿz!FSSpecHandler.delete_dir_contentsc                 C   s   |   d¡ d S )Nrh   )r   )rI   r!   r!   r"   Údelete_root_dir_contentsƒ  s   z&FSSpecHandler.delete_root_dir_contentsc                 C   s$   | j  |¡s
t|ƒ‚| j  |¡ d S rG   )rH   rk   r`   rx   rU   r!   r!   r"   Údelete_file†  s   zFSSpecHandler.delete_filec                 C   s   | j j||dd� d S rw   )rH   Úmv©rI   ÚsrcÚdestr!   r!   r"   Úmove�  s   zFSSpecHandler.movec                 C   s   | j  ||¡ d S rG   )rH   Úcopyr†   r!   r!   r"   Ú	copy_file�  s   zFSSpecHandler.copy_filec                 C   ó8   ddl m} | j |¡st|ƒ‚|| jj|dd�dd�S ©Nr   ©Ú
PythonFileÚrb©ÚmodeÚr©Úpyarrowr�   rH   r{   r`   Úopen©rI   r*   r�   r!   r!   r"   Úopen_input_stream—  ó   zFSSpecHandler.open_input_streamc                 C   rŒ   r�   r”   r—   r!   r!   r"   Úopen_input_fileŸ  r™   zFSSpecHandler.open_input_filec                 C   ó$   ddl m} || jj|dd�dd�S )Nr   rŽ   Úwbr‘   Úw©r•   r�   rH   r–   ©rI   r*   Úmetadatar�   r!   r!   r"   Úopen_output_stream§  ó   z FSSpecHandler.open_output_streamc                 C   r›   )Nr   rŽ   Úabr‘   r�   rž   rŸ   r!   r!   r"   Úopen_append_stream¬  r¢   z FSSpecHandler.open_append_streamN)r5   Ú
__module__Ú__qualname__Ú__doc__rJ   rO   rQ   rT   r%   Ústaticmethodr_   r&   rr   rv   ry   r   r‚   rƒ   r„   r‰   r‹   r˜   rš   r¡   r¤   r!   r!   r!   r"   r6     s.    
r6   rG   )NN)-r§   Úpyarrow.utilr   r   Zpyarrow._fsr   r   r   r   r   r	   r
   r   r   r   r   Z	FileStatsr   Zpyarrow._azurefsr   r   ra   Zpyarrow._hdfsr   Zpyarrow._gcsfsr   Zpyarrow._s3fsr   r   r   r   r   r   r   r   r   r   ÚatexitÚregisterr#   r.   r8   r=   rF   r6   r!   r!   r!   r"   Ú<module>   sH   4ÿÿÿ4ÿ
;ÿþK