o
    h'                     @   s   d dl 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mZ d dl	m
Z
mZmZmZ d dlmZ d dlmZ edZG dd deZd	d
d
efddZdd Zedkred dlZeejdd  dS dS )    N)EIOENOENT)FUSEFuseOSErrorLoggingMixIn
Operations)__version__)	url_to_fsfsspec.fusec                   @   s|   e Zd ZdddZdddZdd Zd	d
 Zdd Zdd Zdd Z	dddZ
dd ZdddZdd Zdd Zdd ZdS )FUSErFc                 C   s8   || _ i | _|dd | _d| _td| || _d S )N/r   zStarting FUSE at %s)fscacherstriprootcounterloggerinfo_ready_file)selfr   path
ready_file r   _/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/fuse.py__init__   s   
zFUSEr.__init__Nc              
   C   sH  t d| | jr|dv rdddS d| j|dgd}z| j|}W n t	y9 } zt
t|d }~ww |dd	|d
d	d}|dd}|d dkrctj|B |d< d|d< d|d< ntj|B |d< |d |d< d|d< d|d< d|v r|d nt |d< d|v r|d nt |d< d|v r|d nt |d< |S )Nz
getattr %sz/.fuse_readyz.fuse_readyfile   )typest_size r   uidi  gid)st_uidst_gidmodei  r   st_moder   r   
st_blksizesizei  P    st_nlinkatimest_atimectimest_ctimemtimest_mtime)r   debugr   joinr   lstripr   r   r   FileNotFoundErrorr   r   getstatS_IFDIRS_IFREGtime)r   r   fhr   excdatapermr   r   r   getattr   s0   


zFUSEr.getattrc                 C   sJ   t d| d| j|dg}| j|d}dd |D }ddg| S )	Nz
readdir %sr    r   Fc                 S   s   g | ]}t j|d qS )r   )osr   basenamer   ).0fr   r   r   
<listcomp>:   s    z!FUSEr.readdir.<locals>.<listcomp>.z..)r   r1   r2   r   r3   r   ls)r   r   r:   filesr   r   r   readdir6   s
   zFUSEr.readdirc                 C   &   d | j|dg}| j| dS Nr    r   r   )r2   r   r3   r   mkdirr   r   r%   r   r   r   rJ   =      zFUSEr.mkdirc                 C   rH   rI   )r2   r   r3   r   rmdir)r   r   r   r   r   rM   B   rL   zFUSEr.rmdirc                 C   sF   t d|||f | jr|dv rdS | j| }|| ||}|S )Nzread %sr   s   ready)r   r1   r   r   seekread)r   r   r(   offsetr:   rB   outr   r   r   rO   G   s   


z
FUSEr.readc                 C   s6   t d||f | j| }|| || t|S )Nzwrite %s)r   r1   r   rN   writelen)r   r   r<   rP   r:   rB   r   r   r   rR   R   s
   


zFUSEr.writec                 C   sd   t d||f d| j|dg}| j| | j|d}|| j| j	< |  j	d7  _	| j	d S )Nz	create %sr    r   wbr)   )
r   r1   r2   r   r3   r   touchopenr   r   )r   r   flagsfifnrB   r   r   r   createY   s   
zFUSEr.createc                 C   sj   t d||f d| j|dg}|d dkrd}nd}| j||| j| j< |  jd7  _| jd S )	Nzopen %sr    r      r   rbrT   r)   )	r   r1   r2   r   r3   r   rV   r   r   )r   r   rW   rY   r%   r   r   r   rV   b   s   
z
FUSEr.openc                 C   s2   d | j|dg}|dkrt| j| d S rI   )r2   r   r3   NotImplementedErrorr   rU   )r   r   lengthr:   rY   r   r   r   truncateo   s   zFUSEr.truncatec              
   C   sR   d | j|dg}z
| j|d W d S  ttfy( } ztt|d }~ww )Nr    r   F)	r2   r   r3   r   rmOSErrorr4   r   r   )r   r   rY   r;   r   r   r   unlinkv   s   
zFUSEr.unlinkc              
   C   sd   z|| j v r| j | }|  | j | W dS W dS  ty1 } zt| W Y d }~dS d }~ww )Nr   )r   closepop	Exceptionprint)r   r   r:   rB   er   r   r   release}   s   

zFUSEr.releasec                 C   s4   t | jdrd| j|dg}| j||S t)Nchmodr    r   )hasattrr   r2   r   r3   ri   r]   rK   r   r   r   ri      s   zFUSEr.chmod)F)N)__name__
__module____qualname__r   r>   rG   rJ   rM   rO   rR   rZ   rV   r_   rb   rh   ri   r   r   r   r   r      s    


	

r   TFc           	         s\    fdd} st j|d}d|_|  |S z|  W dS  ty-   Y dS w )am  Mount stuff in a local directory

    This uses fusepy to make it appear as if a given path on an fsspec
    instance is in fact resident within the local file-system.

    This requires that fusepy by installed, and that FUSE be available on
    the system (typically requiring a package to be installed with
    apt, yum, brew, etc.).

    Parameters
    ----------
    fs: file-system instance
        From one of the compatible implementations
    path: str
        Location on that file-system to regard as the root directory to
        mount. Note that you typically should include the terminating "/"
        character.
    mount_point: str
        An empty directory on the local file-system where the contents of
        the remote path will appear.
    foreground: bool
        Whether or not calling this function will block. Operation will
        typically be more stable if True.
    threads: bool
        Whether or not to create threads when responding to file operations
        within the mounter directory. Operation will typically be more
        stable if False.
    ready_file: bool
        Whether the FUSE process is ready. The ``.fuse_ready`` file will
        exist in the ``mount_point`` directory if True. Debugging purpose.
    ops_class: FUSEr or Subclass of FUSEr
        To override the default behavior of FUSEr. For Example, logging
        to file.

    c                      s   t d  dS )N)r   )	nothreads
foreground)r   r   ro   r   mount_point	ops_classr   r   threadsr   r   <lambda>   s    zrun.<locals>.<lambda>)targetTN)	threadingThreaddaemonstartKeyboardInterrupt)	r   r   rq   ro   rs   r   rr   functhr   rp   r   run   s   ,r}   c              	   C   s  G dd dt j}|dtjd}|jddtd |jdtd	d
 |jdtdd
 |jdtdd
 |jddddd |jddtdd
 |jddddd |jddddd |jdddd d || } i }| jphg D ]\}|	d!\}}}|s~|j
d"|d# | }|d$rt|d%td$  }n|d&r|d%td&  d'v }d(|v r|d(d)\}	}
|	|v r|||	 |
< qi|
|i||	< qi|||< qi| jrtjtj| jd*d+ G d,d- d-tt}|}nt}t| jfi |\}}td.|t| j t|| j| j| j| j| j|d/ d%S )0a  Mount filesystem from chained URL to MOUNT_POINT.

    Examples:

    python3 -m fsspec.fuse memory /usr/share /tmp/mem

    python3 -m fsspec.fuse local /tmp/source /tmp/local \
            -l /tmp/fsspecfuse.log

    You can also mount chained-URLs and use special settings:

    python3 -m fsspec.fuse 'filecache::zip::file://data.zip' \
            / /tmp/zip \
            -o 'filecache-cache_storage=/tmp/simplecache'

    You can specify the type of the setting by using `[int]` or `[bool]`,
    (`true`, `yes`, `1` represents the Boolean value `True`):

    python3 -m fsspec.fuse 'simplecache::ftp://ftp1.at.proftpd.org' \
            /historic/packages/RPMS /tmp/ftp \
            -o 'simplecache-cache_storage=/tmp/simplecache' \
            -o 'simplecache-check_files=false[bool]' \
            -o 'ftp-listings_expiry_time=60[int]' \
            -o 'ftp-username=anonymous' \
            -o 'ftp-password=xieyanbo'
    c                       s   e Zd Z fddZ  ZS )z*main.<locals>.RawDescriptionArgumentParserc                    s,   t   }|d}| j |d< d|S )Nz

r)   )superformat_helpsplitdescriptionr   r2   )r   usageparts	__class__r   r   r      s   


z6main.<locals>.RawDescriptionArgumentParser.format_help)rk   rl   rm   r   __classcell__r   r   r   r   RawDescriptionArgumentParser   s    r   r
   )progr   z	--versionversion)actionr   urlzfs url)r   helpsource_pathzsource directory in fsrq   zlocal directoryz-oz--optionappendz3Any options of protocol included in the chained URL)r   r   z-lz
--log-filez%Logging FUSE debug info (Default: '')z-fz--foregroundstore_falsez-Running in foreground or not (Default: False)z-tz	--threadsz-Running with threads support (Default: False)z-rz--ready-filezZThe `.fuse_ready` file will exist after FUSE is ready. (Debugging purpose, Default: False)=zWrong option: )messagez[int]Nz[bool])1yestrue-r)   z%(asctime)s %(message)s)levelfilenameformatc                   @   s   e Zd ZdS )zmain.<locals>.LoggingFUSErN)rk   rl   rm   r   r   r   r   LoggingFUSEr-  s    r   zMounting %s to %s)ro   rs   r   rr   ) argparseArgumentParsermain__doc__add_argumentr   str
parse_argsoption	partitionerrorlowerendswithintrS   r   log_fileloggingbasicConfigDEBUGr   r   r	   r   r   r1   rq   r}   r   ro   rs   r   )argsr   parserkwargsitemkeysepvaluevalfs_namesetting_namer   fuserr   url_pathr   r   r   r      s   




r   __main__r)   )r   r   r?   r6   rv   r9   errnor   r   fuser   r   r   r   fsspecr   fsspec.corer	   	getLoggerr   r   r}   r   rk   sysargvr   r   r   r   <module>   s.    
 
>u