o
    ;Di!                     @   s   d dl mZ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 eeZG dd deZdd	d
Zdd Zdd Ze	rIe  G dd deZdddZdd Zede ede dS )    )absolute_importprint_functiondivisionN)contextmanager)PY3)register_readerregister_writer
get_reader
get_writerc                   @   s0   e Zd ZdZdd Zd
ddZed
ddZd	S )RemoteSourcea  Read or write directly from files in remote filesystems.

    This source handles many filesystems that are selected based on the
    protocol passed in the `url` argument.

    The url should be specified in `to..()` and `from...()` functions. E.g.::

        >>> import petl as etl
        >>>
        >>> def example_s3():
        ...     url = 's3://mybucket/prefix/to/myfilename.csv'
        ...     data = b'foo,bar\na,1\nb,2\nc,2\n'
        ...
        ...     etl.tocsv(data, url)
        ...     tbl = etl.fromcsv(url)
        ...
        >>> example_s3() # doctest: +SKIP
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' | '1' |
        +-----+-----+
        | 'b' | '2' |
        +-----+-----+
        | 'c' | '2' |
        +-----+-----+

    This source uses `fsspec`_ to provide the data transfer with the remote
    filesystem. Check the `Built-in Implementations <fs_builtin>`_ for available
    remote implementations.

    Some filesystem can use `URL chaining <fs_chain>`_ for compound I/O.

    .. note::

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

            $ pip install fsspec

        Some remote filesystems require aditional packages to be installed.
        Check  `Known Implementations <fs_known>`_ for checking what packages
        need to be installed, e.g.::

            $ pip install s3fs     # AWS S3
            $ pip install gcsfs    # Google Cloud Storage
            $ pip install adlfs    # Azure Blob service
            $ pip install paramiko # SFTP
            $ pip install requests # HTTP, github

    .. versionadded:: 1.6.0

    .. _fsspec: https://filesystem-spec.readthedocs.io/en/latest/
    .. _fs_builtin: https://filesystem-spec.readthedocs.io/en/latest/api.html#built-in-implementations
    .. _fs_known: https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations
    .. _fs_chain: https://filesystem-spec.readthedocs.io/en/latest/features.html#url-chaining
    c                 K      || _ || _d S Nurlkwargsselfr   r    r   H/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/remotes.py__init__J      
zRemoteSource.__init__rbc                 C   s*   dd l }|j| jf|ddd| j}|S )Nr   inferF)modecompression
auto_mkdir)fsspecopenr   r   )r   r   r   fsr   r   r   	open_fileN   s   zRemoteSource.open_filec                 c   sN    |d d d }| j |d}|}|V  W d    d S 1 s w   Y  d S )N   b)r   )r   )r   r   mode2r   sourcer   r   r   r   U   s   "zRemoteSource.openNr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s    9
r   Fc                 C   s(   ddl m}m} t||  t||  dS )z;Register all known fsspec implementations as remote source.r   )known_implementationsregistryN)fsspec.registryr)   r*   _register_filesystems_from)only_availabler)   r*   r   r   r   _register_filesystems`   s   
r.   c                 C   sn   |   D ]0\}}t|tod|v }|r|rqt|}|r |du r%t|t t|}|r/|du r4t|t qdS )zBRegister each fsspec provider from this registry as remote source.errN)items
isinstancedictr	   r   r   r
   r   )fsspec_registryr-   protocolspecmissing_deps
has_reader
has_writerr   r   r   r,   j   s   

r,   c               
   C   s\   zdd l } W n ty   td Y d S w zt  W d S  ty- } ztd|d }~ww )Nr   z:# Missing fsspec package. Install with: pip install fsspecz.# ERROR: failed to register fsspec filesystems)r   ImportErrorloggerdebugr.   	Exception)r   exr   r   r   _try_register_filesystems~   s   
r>   c                   @   s&   e Zd ZdZdd ZedddZdS )		SMBSourcea  Downloads or uploads to Windows and Samba network drives. E.g.::

        >>> def example_smb():
        ...     import petl as etl
        ...     url = 'smb://user:password@server/share/folder/file.csv'
        ...     data = b'foo,bar\na,1\nb,2\nc,2\n'
        ...     etl.tocsv(data, url)
        ...     tbl = etl.fromcsv(url)
        ...
        >>> example_smb() # doctest: +SKIP
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' | '1' |
        +-----+-----+
        | 'b' | '2' |
        +-----+-----+
        | 'c' | '2' |
        +-----+-----+

    The argument `url` (str) must have a URI with format:
    `smb://workgroup;user:password@server:port/share/folder/file.csv`.

    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.

    .. note::

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

            $ pip install smbprotocol[kerberos]

    .. versionadded:: 1.5.0

    .. _smbprotocol: https://github.com/jborean93/smbprotocol#requirements
    c                 K   r   r   r   r   r   r   r   r      r   zSMBSource.__init__r   c                 c   sJ    |d d d }t | jfd|i| j}z
|V  W |  d S |  w )Nr    r!   r   )_open_file_smbprotocolr   r   close)r   r   r"   r#   r   r   r   r      s   zSMBSource.openNr$   )r%   r&   r'   r(   r   r   r   r   r   r   r   r?      s
    )r?   r   c              
   K   s   t | \}}}}}}dd l}	z!|r|	j||||d |d d d }
|	j|fd|
i|}|W S  tyH } ztd| t d d }~ww )Nr   )usernamepasswordportr    r!   r   zSMB error: %s   )	_parse_smb_url	smbclientregister_sessionr   r<   ConnectionErrorwith_tracebacksysexc_info)r   r   r   _domainhostrD   userpasswdserver_pathrG   r"   
filehandler=   r   r   r   r@      s   r@   c           	      C   s   d}| st d| dst ||  trddlm} nddlm} || }|js/t ||  |jdd}d|j|}|j	sEd }d }nd	|j	v rS|j	
d	\}}nd |j	}}|js^d
nt|j}||j|||j|fS )NzQSMB url must be smb://workgroup;user:password@server:port/share/folder/file.txt: zSMB error: no host givenzsmb://r   )urlparse/\z\\{}{};i  )
ValueError
startswithr   urllib.parserS   pathreplaceformathostnamerB   splitrD   intrC   )	r   erS   parsedunc_pathrQ   domainrB   rD   r   r   r   rF      s*   

rF   smb)Fr$   )
__future__r   r   r   loggingrK   
contextlibr   petl.compatr   petl.io.sourcesr   r   r	   r
   	getLoggerr%   r:   objectr   r.   r,   r>   r?   r@   rF   r   r   r   r   <module>   s$   

P

8
