o
    ;Di_1                     @   s.  d dl mZmZmZ 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 d dlZd dlZd dlmZ d dlmZmZmZmZmZ eeZejZejZejZG dd deZG dd	 d	eZG d
d deZG dd deZ G dd deZ!dd Z"e" Z#dd Z$e$ Z%G dd deZ&G dd deZ'G dd deZ(G dd deZ)e)Z*G dd deZ+G dd deZ,d Z-i Z.i Z/i Z0d!d" Z1d#d$ Z2d%d& Z3d'd( Z4d)d* Z5d+d, Z6d-d. Z7d/d0 Z8e4d1e e4d2e e4d3e e5d4e( e5d5e( e5d6e( d7d8 Z9d9d: Z:d;d< Z;d=d> Z<dBd@dAZ=dS )C    )absolute_importprint_functiondivisionN)contextmanager)ArgumentError)urlopenStringIOBytesIOstring_typesPY2c                   @   s   e Zd Zdd ZdddZdS )
FileSourcec                 K      || _ || _d S N)filenamekwargs)selfr   r    r   H/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/sources.py__init__      
zFileSource.__init__rc                 C   s   t j| j|fi | jS r   )ioopenr   r   r   moder   r   r   r       s   zFileSource.openNr   )__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   $   e Zd ZdddZed	ddZdS )

GzipSourceFc                 K      || _ || _|| _d S r   r   remoter   r   r   r#   r   r   r   r   r   &      
zGzipSource.__init__r   c                 c   b    | j r|dstdt| j}n| j}tj||fi | j}z
|V  W |  d S |  w Nr   source is read-only)	r#   
startswithr   r   r   gzipr   r   closer   r   
filehandlesourcer   r   r   r   +      
zGzipSource.openNFr   r   r   r   r   r   r   r   r   r   r   r    $       
r    c                   @   r   )
	BZ2SourceFc                 K   r!   r   r"   r$   r   r   r   r   <   r%   zBZ2Source.__init__r   c                 c   r&   r'   )	r#   r)   r   r   r   bz2BZ2Filer   r+   r,   r   r   r   r   A   r/   zBZ2Source.openNr0   r   r1   r   r   r   r   r3   :   r2   r3   c                   @   s"   e Zd ZdddZedd ZdS )	ZipSourceNc                 K   s   || _ || _|| _|| _d S r   )r   
membernamepwdr   )r   r   r7   r8   r   r   r   r   r   R   s   
zZipSource.__init__c                 c   s    t r
|d d}n|tdd tdd i}tj| j|fi | j}z&| jd ur4|| j	|| jV  n|| j	|V  W |
  d S W |
  d S |
  w )NbUbU)r   	translateordzipfileZipFiler   r   r8   r   r7   r+   )r   r   zfr   r   r   r   X   s   
zZipSource.openr   r1   r   r   r   r   r6   P   s    
r6   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Uncloseablec                 C   s   t | d| d S )N_inner)object__setattr__)r   innerr   r   r   r   j      zUncloseable.__init__c                 C   s   t | j|S r   )getattrrB   )r   itemr   r   r   __getattr__m   s   zUncloseable.__getattr__c                 C   s   t | j|| d S r   )setattrrB   )r   keyvaluer   r   r   rD   p   rF   zUncloseable.__setattr__c                 C   s   t d| j  d S )NzUncloseable: close called (%r))debugrB   r   r   r   r   r+   s   s   zUncloseable.closeN)r   r   r   r   rI   rD   r+   r   r   r   r   rA   h   s
    rA   c                  C      zt jjW S  ty   Y nw zt j } t| ddW S  ty%   Y nw zt jjW S  ty4   Y nw zt j } t| ddW S  tyM   Y t jS w )Nabr   )	sysstdoutbufferAttributeErrorfilenoosfdopen	Exception
__stdout__fdr   r   r   _get_stdout_binaryx   .   



r\   c                  C   rO   )Nrbr   )	rQ   stdinrS   rT   rU   rV   rW   rX   	__stdin__rZ   r   r   r   _get_stdin_binary   r]   ra   c                   @   s   e Zd Zedd ZdS )StdoutSourcec                 c   s:    | dr
tdd|v rttV  d S ttjV  d S )Nr   zsource is write-onlyr:   )r)   r   rA   stdout_binaryrQ   rR   r   r   r   r   r         
zStdoutSource.openNr   r   r   r   r   r   r   r   r   rb      s    rb   c                   @   s   e Zd ZedddZdS )StdinSourcer   c                 c   s:    | ds
tdd|v rttV  d S ttjV  d S )Nr   r(   r:   )r)   r   rA   stdin_binaryrQ   r_   r   r   r   r   r      rd   zStdinSource.openNr   re   r   r   r   r   rf      s    rf   c                   @   "   e Zd Zdd ZedddZdS )	URLSourcec                 O   r   r   argsr   r   rk   r   r   r   r   r      r   zURLSource.__init__r   c                 c   sF    | ds
tdt| ji | j}z
|V  W |  d S |  w r'   )r)   r   r   rk   r   r+   )r   r   fr   r   r   r      s   
zURLSource.openNr   r1   r   r   r   r   ri          ri   c                   @   s0   e Zd ZdZd
ddZedddZdd	 ZdS )MemorySourceao  Memory data source. E.g.::

        >>> import petl as etl
        >>> data = b'foo,bar\na,1\nb,2\nc,2\n'
        >>> source = etl.MemorySource(data)
        >>> tbl = etl.fromcsv(source)
        >>> tbl
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' | '1' |
        +-----+-----+
        | 'b' | '2' |
        +-----+-----+
        | 'c' | '2' |
        +-----+-----+

        >>> sink = etl.MemorySource()
        >>> tbl.tojson(sink)
        >>> sink.getvalue()
        b'[{"foo": "a", "bar": "1"}, {"foo": "b", "bar": "2"}, {"foo": "c", "bar": "2"}]'

    Also supports appending.

    Nc                 C   s   || _ d | _d S r   )srS   )r   rp   r   r   r   r      r   zMemorySource.__init__r^   c                 c   s    z`z[d|v r"| j d urd|v rt| j | _n=t| j | _n6tdd|v r>| jd ur0| j  d|v r9t | _nt | _nd|v rT| jd u rTd|v rPt | _nt | _t| jV  W W d S     w )Nr   r:   zno string data suppliedwa)rp   r	   rS   r   r   r+   rA   r   r   r   r   r      s0   






zMemorySource.openc                 C   s   | j r| j  S d S r   )rS   getvaluerN   r   r   r   rs     s   
zMemorySource.getvaluer   r^   )r   r   r   __doc__r   r   r   rs   r   r   r   r   ro      s    
ro   c                   @   rh   )PopenSourcec                 O   r   r   rj   rl   r   r   r   r     r   zPopenSource.__init__r   c                 c   sF    | ds
tdtj| jd< tj| ji | j}z|jV  W d S w )Nr   r(   rR   )r)   r   
subprocessPIPEr   Popenrk   rR   )r   r   procr   r   r   r   !  s   

zPopenSource.openNr   r1   r   r   r   r   rv     rn   rv   c                   @   s&   e Zd ZdZdd ZedddZdS )	CompressedSourcea  Handle IO from a file-like object and (de)compress with a codec
    
    The `source` argument (source class) is the source class that will handle
    the actual input/output stream. E.g: :class:`petl.io.sources.URLSource`.
    
    The `codec` argument (source class) is the source class that will handle
    the (de)compression of the stream. E.g: :class:`petl.io.sources.GzipSource`.
    c                 C   r   r   )r.   codec)r   r.   r|   r   r   r   r   7  r   zCompressedSource.__init__r^   c              	   c   s    | j j|d/}| |}|j|d}|V  W d    n1 s"w   Y  W d    d S W d    d S 1 s:w   Y  d S )N)r   )r.   r   r|   )r   r   r-   
transcoderstreamr   r   r   r   ;  s   
"zCompressedSource.openNrt   )r   r   r   ru   r   r   r   r   r   r   r   r{   -  s
    	r{   z]invalid source argument, expected None or a string or an object implementing open(), found %rc                 C   s0   | d}t |drtt|dsJ t| d S )Ntestr   )hasattrcallablerG   _invalid_source_msg)source_classr.   r   r   r   _assert_source_has_openL  s   
r   c                 C   s@   t | tsJ t|  t |tsJ t|  t| ||| < d S r   )
isinstancer
   r   typer   )handler_typehandler_classhandler_listr   r   r   _register_handlerS  s   r   c                 C   s   t | tr| |v r||  S d S r   )r   r
   )r   r   r   r   r   _get_handler[  s   
r   c                 C      t | |t dS )z
    Register handler for automatic compression and decompression for file I/O

    Use of the handler is determined matching the file `extension` with the
    source specified in ``from...()`` and ``to...()`` functions.

    .. versionadded:: 1.5.0
    N)r   _CODECS)	extensionr   r   r   r   register_codecc     	r   c                 C   r   )z
    Register handler for automatic reading using a remote protocol.

    Use of the handler is determined matching the `protocol` with the scheme
    part of the url in ``from...()`` function (e.g: `http://`).

    .. versionadded:: 1.5.0
    N)r   _READERSprotocolr   r   r   r   register_readero  r   r   c                 C   r   )z
    Register handler for automatic writing using a remote protocol.

    Use of the handler is determined matching the `protocol` with the scheme
    part of the url in ``to...()`` function (e.g: `smb://`).

    .. versionadded:: 1.5.0
    N)r   _WRITERSr   r   r   r   register_writer{  r   r   c                 C   
   t | tS )zk
    Retrieve the handler responsible for reading from a remote protocol.

    .. versionadded:: 1.6.0
    )r   r   r   r   r   r   
get_reader     
r   c                 C   r   )zk
    Retrieve the handler responsible for writing from a remote protocol.

    .. versionadded:: 1.6.0
    )r   r   r   r   r   r   
get_writer  r   r   z.gzz.bgzz.bz2ftphttphttpsc                 C   s(   t  D ]\}}| |r|  S qd S r   )r   itemsendswith)r.   extcodec_classr   r   r   _get_codec_for  s
   
r   c                 C   sH   |  d}|dkrd S | d | }| D ]\}}||kr!|  S qd S )N://r   )findr   )r.   handlersprotocol_indexr   prefixr   r   r   r   _get_handler_from  s   
r   c                 C   s   t | tr,t| |}t| }|d u r(|d ur|| S d| vs$J t|  t| S || S t| dr8tt| ds>J t|  | S )Nr   r   )	r   r
   r   r   r   r   r   r   rG   )r.   r   handlerr|   r   r   r   _resolve_source_from_arg  s   


r   c                 C      | du rt  S t| tS )a9  
    Retrieve a open stream for reading from the source provided.

    The result stream will be open by a handler that would return raw bytes and
    transparently take care of the decompression, remote authentication,
    network transfer, format decoding, and data extraction.

    .. versionadded:: 1.4.0
    N)rf   r   r   )r.   r   r   r   read_source_from_arg     

r   wbc                 C   r   )a1  
    Retrieve a open stream for writing to the source provided.

    The result stream will be open by a handler that would write raw bytes and
    transparently take care of the compression, remote authentication,
    network transfer, format encoding, and data writing.

    .. versionadded:: 1.4.0
    N)rb   r   r   )r.   r   r   r   r   write_source_from_arg  r   r   )r   )>
__future__r   r   r   rV   r   r*   rQ   r4   r>   
contextlibr   rw   loggingpetl.errorsr   petl.compatr   r   r	   r
   r   	getLoggerr   loggerwarninginforM   rC   r   r    r3   r6   rA   r\   rc   ra   rg   rb   rf   ri   ro   StringSourcerv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sn   

C	





