a
    Z%b!                     @   sx   d Z ddlmZ ddlmZ ddlZddlmZ ddlZddl	m
Z
mZmZmZmZmZmZ dd ZG d	d
 d
eZdS )z0.4    )contextmanager)partialN)copyfileobj)	text_typePY2reraiseStringIOBytesIOIteratorwrapsc                    s    fdd}|S )Nc                    s   t   fdd}|S )Nc                    sN   | g|R i |} r0t |ts0|| jS  sJt |trJ|| jS |S N)
isinstancer   decodeencodingencode)selfargskwargsrv)enabledf ]/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/data/__init__.py_   s    z*enable_unicode.<locals>.wrapper.<locals>._)r   )r   r   r   )r   r   wrapper   s    zenable_unicode.<locals>.wrapperr   )r   r   r   r   r   enable_unicode   s    
r   c                   @   s   e Zd ZdZdZd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d Zedd Zeddd Zeddd Zeddd  Zd!d" Zd#d$ Zed*d'd(ZdS )+Dataa  Dynamically converts between various forms of passed in input data.

    Exactly one of ``arg``, ``data`` or ``file`` must be not-``None``.

    :param arg: Dynamic argument. If a bytestring, will be interpreted as raw
                bytes. A unicode string will be interpreted as text and any
                object that has a ``read()`` method as a file-like.
                Any instance of ``Data`` will be passed through and rendered
                unusable.
    :param encoding: The data's encoding. Will be used for every conversion
                     from bytestrings to text (unicode) if necessary and the
                     other way around.
    :param data: Buffer argument. If unicode string, will be interpreted as
                 text, otherwise as bytestring.
    :param file: File argument. Any object with a ``read()`` method will be
                 treated as file-like. Everything else is considered a
                 filename.Nc                 C   s   ||||f| _ |||gd dkr*td|d urt|| jrv|jpH|j}|jpT|j}|j	}d  |_ |_ |_|_nt
|dr|}n|}|d urt|tr|| _q|| _n2|d urt
|dr|| _t|dd r|j	}n|| _|pd| _	d S )N   z'Must supply exactly one of data or filereadr   utf8)	orig_argscount
ValueErrorr   	__class__datatextfilefilenamer   hasattrr   getattr)r   argr   r%   r'   r   r   r   __init__1   s.    


zData.__init__c                 C   s   | j dur| j S | jdur(| j| jS | jdur:|  S | jdurxt| jd}| W  d   S 1 sn0    Y  t	ddS )z>Returns the data as bytes (on Python3) or string (on Python2).NrbBroken Data, all None.)
r%   r&   r   r   r'   readbr(   openr   r#   )r   r   r   r   r   	__bytes__R   s    



&zData.__bytes__c                 C   s   | S )zhContext manager support. If data is a file-like, will close it upon
        exiting the context manager.r   r   r   r   r   	__enter__c   s    zData.__enter__c                 C   s   |    d S r   )close)r   Zexception_typeZexception_value	tracebackr   r   r   __exit__h   s    zData.__exit__c                 C   s   | S )zdIterator support. Returns lines (similar to file objects) using
        :meth:`~data.Data.readline`.r   r2   r   r   r   __iter__k   s    zData.__iter__c                 C   s   |   }|st|S r   )readlineStopIteration)r   chunkr   r   r   __next__p   s    zData.__next__c                 C   s   t r|  S |  S )z@Returns the data as unicode (on Python3) or string (on Python2).)r   r1   __unicode__r2   r   r   r   __str__v   s    zData.__str__c                 C   s2   | j dur| j S | jdur"|  S |  | jS )zReturns the data as unicode.N)r&   r'   r   r1   r   r   r2   r   r   r   r<   |   s
    

zData.__unicode__c                 C   sh   dd }| j j}| jd ur0d||| j| jS | jd urPd||| j| jS d|| jp`| j| jS )Nc                 S   s(   t | dk rt| S t| d d d S )N   z...)lenrepr)bufr   r   r   head   s    zData.__repr__.<locals>.headz{}(data={}, encoding={!r})z{}(file={!r}, encoding={!r}))r$   __name__r%   formatr   r&   r'   r(   )r   rB   cnamer   r   r   __repr__   s    

zData.__repr__c                 C   s,   t | dddu r| jdu rdS | j  dS )z?Closes input if based on open filelike. Otherwise does nothing._streamN)r*   r'   streamr4   r2   r   r   r   r4      s    z
Data.closec                 C   sv   t | dsp| jdur| j| _nR| jdur8t| jd| _n8| jdurPt| j| _n | jdurht| j| _nt	d| jS )zpReturns a stream object (:func:`file`, :class:`~io.BytesIO` or
        :class:`~StringIO.StringIO`) on the data.rG   Nr-   r.   )
r)   r'   rG   r(   r0   r&   r   r%   r	   r#   r2   r   r   r   rH      s    





zData.streamTc                 O   s   | j j|i |S )z`Read method, implements same interface as :func:`file.read`. Always
        returns ``unicode``.rH   r   r   r   r   r   r   r   r      s    z	Data.readFc                 O   s   | j j|i |S )z>Like :meth:`~data.Data.read`, but returns bytestrings instead.rI   rJ   r   r   r   r/      s    z
Data.readbc                 O   s   | j j| S )z4Return one line from stream. Always returns unicode.)rH   r8   rJ   r   r   r   r8      s    zData.readlinec                 O   s"   t tt| jg|R i |dS )z9Return list of all lines. Always returns list of unicode. )listiterr   r8   rJ   r   r   r   	readlines   s    zData.readlinesc                 C   s   |}t |dr| jdur8t| jdddu r8t| j| q| jdurzt| jd}t|| W d   q1 sn0    Y  q||   n4t|d}| |W  d   S 1 s0    Y  dS )zSave data to file.

        Will copy by either writing out the data or using
        :func:`shutil.copyfileobj`.

        :param file: A file-like object (with a ``write`` method) or a
                     filename.writeNr   r-   wb)	r)   r'   r*   r   r(   r0   rO   r1   save_to)r   r'   destinpoutr   r   r   rQ      s    


*zData.save_torK   tmpc                 c   s   t j|||dd}zn| | |  |d |V  W zt|j W q ty~ } z|j	dkrjt
| W Y d}~qd}~0 0 nJzt|j W n6 ty } z|j	dkrt
| W Y d}~n
d}~0 0 0 dS )a$  Saves data to temporary file and returns the relevant instance of
        :func:`~tempfile.NamedTemporaryFile`. The resulting file is not
        deleted upon closing, but when the context manager exits.

        Other arguments are passed on to :func:`~tempfile.NamedTemporaryFile`.
        F)suffixprefixdirdeleter   r   N)tempfileNamedTemporaryFilerQ   flushseekosunlinknameOSErrorerrnor   )r   rV   rW   rX   rU   er   r   r   
temp_saved   s*    


 
zData.temp_saved)NNNN)rK   rU   N)rC   
__module____qualname____doc__r%   r&   r'   r(   r,   r1   r3   r6   r7   r;   r=   r<   rF   r4   propertyrH   r   r   r/   r8   rN   rQ   r   rd   r   r   r   r   r      s6   
!




r   )__version__
contextlibr   	functoolsr   r^   shutilr   rZ   sixr   r   r   r   r	   r
   r   r   r   r   r   r   r   <module>   s   $