U
    9%e)                     @   s~   d Z ddlZddlZddlmZmZ ddlZddlmZ ddl	m
Z
 G dd deZd	d
 Zdd Zdd ZG dd dZdS )z
This module contains SMBFileSystem class responsible for handling access to
Windows Samba network shares by using package smbprotocol
    N)S_ISDIRS_ISLNK   )AbstractFileSysteminfer_storage_optionsc                       s   e Zd ZdZdZd) fdd	Zedd Zd	d
 Ze	dd Z
edd Zd*ddZd+ddZdd Zdd Zdd Zdd Zd,ddZd-d!d"Zd#d$ Zd%d& Zd'd( Z  ZS ).SMBFileSystemaa  Allow reading and writing to Windows and Samba network shares.

    When using `fsspec.open()` for getting a file-like object the URI
    should be specified as this format:
    ``smb://workgroup;user:password@server:port/share/folder/file.csv``.

    Example::

        >>> import fsspec
        >>> with fsspec.open(
        ...     'smb://myuser:mypassword@myserver.com/' 'share/folder/file.csv'
        ... ) as smbfile:
        ...     df = pd.read_csv(smbfile, sep='|', header=None)

    Note that you need to pass in a valid hostname or IP address for the host
    component of the URL. Do not use the Windows/NetBIOS machine name for the
    host component.

    The first component of the path in the URL points to the name of the shared
    folder. Subsequent path components will point to the directory/folder/file.

    The URL components ``workgroup`` , ``user``, ``password`` and ``port`` may be
    optional.

    .. note::

        For working this source require `smbprotocol`_ to be installed, e.g.::

            $ pip install smbprotocol
            # or
            # pip install smbprotocol[kerberos]

    .. _smbprotocol: https://github.com/jborean93/smbprotocol#requirements

    Note: if using this with the ``open`` or ``open_files``, with full URLs,
    there is no way to tell if a path is relative, so all paths are assumed
    to be absolute.
    smbN<   c           	         sV   t t| jf | || _|| _|| _|| _|| _|| _|	dd| _
|| _|   dS )ai  
        You can use _get_kwargs_from_urls to get some kwargs from
        a reasonable SMB url.

        Authentication will be anonymous or integrated if username/password are not
        given.

        Parameters
        ----------
        host: str
            The remote server name/ip to connect to
        port: int or None
            Port to connect with. Usually 445, sometimes 139.
        username: str or None
            Username to connect with. Required if Kerberos auth is not being used.
        password: str or None
            User's password on the server, if using username
        timeout: int
            Connection timeout in seconds
        encrypt: bool
            Whether to force encryption or not, once this has been set to True
            the session cannot be changed back to False.
        share_access: str or None
            Specifies the default access applied to file open operations
            performed with this file system object.
            This affects whether other processes can concurrently open a handle
            to the same file.

            - None (the default): exclusively locks the file until closed.
            - 'r': Allow other handles to be opened with read access.
            - 'w': Allow other handles to be opened with write access.
            - 'd': Allow other handles to be opened with delete access.
        temppath N)superr   __init__hostportusernamepasswordtimeoutencryptpopr   share_access_connect)	selfr   r   r   r   r   r   r   kwargs	__class__ Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/fsspec/implementations/smb.pyr   >   s    ,zSMBFileSystem.__init__c                 C   s   | j d krdS | j S )N  r   r   r   r   r   _portu   s    zSMBFileSystem._portc                 C   s&   t j| j| j| j| j| j| jd d S )N)r   r   r   r   Zconnection_timeout)	smbclientZregister_sessionr   r   r   r!   r   r   r    r   r   r   r   y   s    zSMBFileSystem._connectc                 C   s   t |d S )Npathr   )clsr#   r   r   r   _strip_protocol   s    zSMBFileSystem._strip_protocolc                 C   s$   t | }|dd  |dd  |S )Nr#   protocol)r   r   )r#   outr   r   r   _get_kwargs_from_urls   s    z#SMBFileSystem._get_kwargs_from_urlsTc                 K   sH   t | j|}|r,tj|fd| jd| ntj|fd| ji| d S )NFexist_okr   r   )_as_unc_pathr   r"   makedirsr!   mkdir)r   r#   Zcreate_parentsr   wpathr   r   r   r-      s    zSMBFileSystem.mkdirFc                 C   s*   t |r&t| j|}tj||| jd d S )Nr)   )_share_has_pathr+   r   r"   r,   r!   )r   r#   r*   r.   r   r   r   r,      s    zSMBFileSystem.makedirsc                 C   s(   t |r$t| j|}tj|| jd d S Nr   )r/   r+   r   r"   rmdirr!   )r   r#   r.   r   r   r   r1      s    zSMBFileSystem.rmdirc                 K   sz   t | j|}tj|fd| ji|}t|jr4d}nt|jrDd}nd}|dkrX|d n||j||j	|j
|j|jd}|S )Nr   	directorylinkfile/)namesizetypeuidgidtimemtime)r+   r   r"   statr!   r   st_moder   st_sizest_uidst_gidst_atimest_mtime)r   r#   r   r.   statsstyperesr   r   r   info   s     

	zSMBFileSystem.infoc                 C   s2   t | j|}tj|| jd}tjj|jtjj	dS )z=Return the created timestamp of a file as a datetime.datetimer   tz)
r+   r   r"   r=   r!   datetimefromtimestampst_ctimetimezoneutcr   r#   r.   rD   r   r   r   created   s    zSMBFileSystem.createdc                 C   s2   t | j|}tj|| jd}tjj|jtjj	dS )z>Return the modified timestamp of a file as a datetime.datetimer   rH   )
r+   r   r"   r=   r!   rJ   rK   rC   rM   rN   rO   r   r   r   modified   s    zSMBFileSystem.modifiedc                    sP   t j }tj|fdji|} fdd|D }|rLfdd|D }|S )Nr   c                    s    g | ]}d   d |gqS )r5   )joinrstrip).0pr#   r   r   
<listcomp>   s     z$SMBFileSystem.ls.<locals>.<listcomp>c                    s   g | ]}  |qS r   )rG   )rT   dr    r   r   rW      s     )r+   r   r"   listdirr!   )r   r#   detailr   uncZlisteddirsr   )r#   r   r   ls   s    zSMBFileSystem.lsrbc                 K   s   |dk	r|dkr|nd}t | j|}|d| j}	d|krn|dkrnt| j|| j}
t||
|f| j|d|S tj	||f||	| jd|S )	a5  
        block_size: int or None
            If 0, no buffering, 1, line buffering, >1, buffer that many bytes

        Notes
        -----
        By specifying 'share_access' in 'kwargs' it is possible to override the
        default shared access setting applied in the constructor of this object.
        Nr   r_   r   wF)r   
block_size)	bufferingr   r   )
r+   r   r   r   _as_temp_pathr   SMBFileOpenerr!   r"   	open_file)r   r#   modera   Z
autocommitZcache_optionsr   Zblsr.   r   tempr   r   r   _open   s2       zSMBFileSystem._openc                 K   s6   t | j|}t | j|}tj||fd| ji| dS )z0Copy within two locations in the same filesystemr   N)r+   r   r"   copyfiler!   r   Zpath1Zpath2r   Zwpath1Zwpath2r   r   r   copy   s    zSMBFileSystem.copyc                 C   sT   t |rPt| j|}tj|| jd}t|jr@tj|| jd ntj	|| jd d S r0   )
r/   r+   r   r"   r=   r!   r   r>   r1   removerO   r   r   r   _rm   s    
zSMBFileSystem._rmc                 K   s6   t | j|}t | j|}tj||fd| ji| d S )Nr   )r+   r   r"   renamer!   rj   r   r   r   mv   s    zSMBFileSystem.mv)NNNr
   NN)T)F)T)r^   r_   TN)__name__
__module____qualname____doc__r&   r   propertyr!   r   classmethodr%   staticmethodr(   r-   r,   r1   rG   rP   rQ   r]   rh   rk   rm   ro   __classcell__r   r   r   r   r      s>   '      7






    
#	r   c                 C   s   | dd}d| |}|S )Nr5   \z\\{}{})replaceformat)r   r#   Zrpathr[   r   r   r   r+     s    r+   c                 C   s.   | dd }d||t }t| |}|S )Nr5      z/{}{}/{})splitrz   uuiduuid4r+   )r   r#   r   share	temp_filer[   r   r   r   rc     s    
rc   c                 C   s$   |  d}| dr|dkS |dkS )Nr5   r   r{   )countendswith)r#   partsr   r   r   r/     s    

r/   c                   @   sZ   e Zd ZdZd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 )rd   z/writes to remote temporary file, move on commitr   r_   c                 K   s<   || _ || _|| _|| _|| _d | _d| _|| _|   d S NF)	r#   rg   rf   ra   r   smbfile
_incontextr   rh   )r   r#   rg   rf   r   ra   r   r   r   r   r     s    zSMBFileOpener.__init__c                 C   s<   | j d ks| j jr8tj| j| jf| j| jd| j| _ d S )N)r   rb   )	r   closedr"   re   rg   rf   r   ra   r   r    r   r   r   rh   #  s    zSMBFileOpener._openc                 C   s   t j| j| j| jd dS )z(Move temp file to definitive on success.r   N)r"   ry   rg   r#   r   r    r   r   r   commit-  s    zSMBFileOpener.commitc                 C   s   t j| j| jd dS )z Remove the temp file on failure.r   N)r"   rl   rg   r   r    r   r   r   discard2  s    zSMBFileOpener.discardc                 C   s   | j S NrV   r    r   r   r   
__fspath__6  s    zSMBFileOpener.__fspath__c                 C   s
   | j  S r   )r   __iter__r    r   r   r   r   9  s    zSMBFileOpener.__iter__c                 C   s   t | j|S r   )getattrr   )r   itemr   r   r   __getattr__<  s    zSMBFileOpener.__getattr__c                 C   s   d| _ | j S )NT)r   r   	__enter__r    r   r   r   r   ?  s    zSMBFileOpener.__enter__c                 C   s   d| _ | j||| d S r   )r   r   __exit__)r   exc_type	exc_value	tracebackr   r   r   r   C  s    zSMBFileOpener.__exit__N)r   r_   )rp   rq   rr   rs   r   rh   r   r   r   r   r   r   r   r   r   r   r   rd     s   

rd   )rs   rJ   r}   r=   r   r   r"   r   r   utilsr   r   r+   rc   r/   rd   r   r   r   r   <module>   s    o