U
    di                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZ dZd	Zd
ZdZdZG dd dZG dd dZG dd dZdd ZdddZdddZe  dS )z
    tablib.core
    ~~~~~~~~~~~

    This module implements the central Tablib objects.

    :copyright: (c) 2016 by Kenneth Reitz. 2019 Jazzband.
    :license: MIT, see LICENSE for more details.
    )OrderedDict)copy)
itemgetter)HeadersNeededInvalidDatasetIndexInvalidDatasetTypeInvalidDimensionsUnsupportedFormat)registry)normalize_inputZtablibzKenneth ReitzMITz,Copyright 2017 Kenneth Reitz. 2019 Jazzband.Zrestructuredtextc                   @   s   e Zd ZdZddg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dd Zdd Zdd Zdd  Zed!d" Zed#d$ Zd%d& Zd'S ))Rowz/Internal Row object. Mainly used for filtering._rowtags c                 C   s   t || _t || _d S N)listr   r   selfrowr   r   r   //tmp/pip-unpacked-wheel-_3ph8cfy/tablib/core.py__init__%   s    
zRow.__init__c                 C   s   dd | j D S )Nc                 s   s   | ]
}|V  qd S r   r   ).0colr   r   r   	<genexpr>*   s     zRow.__iter__.<locals>.<genexpr>r   r   r   r   r   __iter__)   s    zRow.__iter__c                 C   s
   t | jS r   )lenr   r   r   r   r   __len__,   s    zRow.__len__c                 C   s
   t | jS r   )reprr   r   r   r   r   __repr__/   s    zRow.__repr__c                 C   s
   | j | S r   r   r   ir   r   r   __getitem__2   s    zRow.__getitem__c                 C   s   || j |< d S r   r   )r   r#   valuer   r   r   __setitem__5   s    zRow.__setitem__c                 C   s   | j |= d S r   r   r"   r   r   r   __delitem__8   s    zRow.__delitem__c                 C   s   | j | jfS r   r   r   r   r   r   r   __getstate__;   s    zRow.__getstate__c                 C   s   |\| _ | _d S r   r(   )r   stater   r   r   __setstate__>   s    zRow.__setstate__c                 C   s   |  t| j| d S r   )insertr   r   r   r%   r   r   r   rpushA   s    z	Row.rpushc                 C   s   |  d| d S )Nr   r,   r-   r   r   r   lpushD   s    z	Row.lpushc                 C   s   |  | d S r   r.   r-   r   r   r   appendG   s    z
Row.appendc                 C   s   | j || d S r   )r   r,   )r   indexr%   r   r   r   r,   J   s    z
Row.insertc                 C   s
   || j kS r   r   )r   itemr   r   r   __contains__M   s    zRow.__contains__c                 C   s
   t | jS )z%Tuple representation of :class:`Row`.)tupler   r   r   r   r   r6   P   s    z	Row.tuplec                 C   s
   t | jS )z$List representation of :class:`Row`.)r   r   r   r   r   r   r   U   s    zRow.listc                 C   s>   |dkrdS t |tr || jkS ttt|t| j@ S dS )z)Returns true if current row contains tag.NF)
isinstancestrr   boolr   set)r   tagr   r   r   has_tagZ   s
    

zRow.has_tagN)r   r   )__name__
__module____qualname____doc__	__slots__r   r   r   r!   r$   r&   r'   r)   r+   r.   r0   r2   r,   r5   propertyr6   r   r<   r   r   r   r   r       s*   


r   c                   @   s  e Zd Z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d_ddZd`ddZdd Zdd ZeeeZdd  Zd!d" ZeeeZd#d$ Zed%d& Zed'd( Zdad)d*Zd+d, Zdbd.d/Zdcd0d1Zddd2d3Zded4d5Zdfd6d7Zd8d9 Z d:d; Z!d<d= Z"dgd>d?Z#dhd@dAZ$didBdCZ%djdEdFZ&dkdGdHZ'dldIdJZ(dKdL Z)dMdN Z*dOdP Z+dmdQdRZ,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1dnd]d^Z2dS )oDataseta  The :class:`Dataset` object is the heart of Tablib. It provides all core
    functionality.

    Usually you create a :class:`Dataset` instance in your main module, and append
    rows as you collect data. ::

        data = tablib.Dataset()
        data.headers = ('name', 'age')

        for (name, age) in some_collector():
            data.append((name, age))


    Setting columns is similar. The column data length must equal the
    current height of the data and headers must be set. ::

        data = tablib.Dataset()
        data.headers = ('first_name', 'last_name')

        data.append(('John', 'Adams'))
        data.append(('George', 'Washington'))

        data.append_col((90, 67), header='age')


    You can also set rows and headers upon instantiation. This is useful if
    dealing with dozens or hundreds of :class:`Dataset` objects. ::

        headers = ('first_name', 'last_name')
        data = [('John', 'Adams'), ('George', 'Washington')]

        data = tablib.Dataset(*data, headers=headers)

    :param \*args: (optional) list of rows to populate Dataset
    :param headers: (optional) list strings for Dataset header row
    :param title: (optional) string to use as title of the Dataset


    .. admonition:: Format Attributes Definition

    If you look at the code, the various output/import formats are not
    defined within the :class:`Dataset` object. To add support for a new format, see
    :ref:`Adding New Formats <newformats>`.

    c                 O   sB   t dd |D | _d | _g | _g | _|d| _|d| _d S )Nc                 s   s   | ]}t |V  qd S r   r   )r   argr   r   r   r      s     z#Dataset.__init__.<locals>.<genexpr>headerstitle)r   _data_Dataset__headers_separators_formattersgetrF   rG   )r   argskwargsr   r   r   r      s    zDataset.__init__c                 C   s   | j S r   )heightr   r   r   r   r      s    zDataset.__len__c                    sf   t |tr:|| jkr4| j|  fdd| jD S tn(| j| }t |trT|jS dd |D S d S )Nc                    s   g | ]}|  qS r   r   r   r   posr   r   
<listcomp>   s     z'Dataset.__getitem__.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )r6   )r   resultr   r   r   rS      s     )r7   r8   rF   r3   rH   KeyErrorr   r6   )r   keyZ_resultsr   rQ   r   r$      s    



zDataset.__getitem__c                 C   s   |  | t|| j|< d S r   )	_validater   rH   )r   rV   r%   r   r   r   r&      s    
zDataset.__setitem__c                 C   s`   t |trT|| jkrN| j|}| j|= t| jD ]\}}||= || j|< q2q\tn| j|= d S r   )r7   r8   rF   r3   	enumeraterH   rU   )r   rV   rR   r#   r   r   r   r   r'      s    

zDataset.__delitem__c                 C   s,   zd| j   W S  tk
r&   Y dS X d S )Nz<%s dataset>z<dataset object>rG   lowerAttributeErrorr   r   r   r   r!      s    zDataset.__repr__c                    s   g }| j r |dd | j D  |dd | jD  dd |D }tttt| }| j rr|ddd |D  d	d	d t
|D  d
	 fdd|D S )Nc                 S   s   g | ]}t |qS r   )r8   )r   hr   r   r   rS      s     z#Dataset.__str__.<locals>.<listcomp>c                 s   s   | ]}t tt|V  qd S r   )r   mapr8   rP   r   r   r   r      s     z"Dataset.__str__.<locals>.<genexpr>c                 S   s   g | ]}t tt|qS r   )r   r]   r   rP   r   r   r   rS      s        c                 S   s   g | ]}d | qS )-r   )r   lengthr   r   r   rS      s     |c                 s   s   | ]}d | V  qdS )z{%s:%s}Nr   )r   r4   r   r   r   r      s     
c                 3   s   | ]} j | V  qd S r   )formatrP   format_stringr   r   r      s     )rI   r2   extendrH   r   r]   maxzipr,   joinrX   )r   rT   ZlensZ
field_lensr   rd   r   __str__   s    zDataset.__str__c                 K   s   t |j| f|S r   )r
   
get_format
export_set)r   fmt_keyrN   r   r   r   _get_in_format   s    zDataset._get_in_formatc                 K   s   t |}t|j| |f|S r   )r   r
   rk   
import_set)r   rm   	in_streamrN   r   r   r   _set_in_format   s    zDataset._set_in_formatNFc                    s~   |r j rt| j knd}nH|rNt|dk r4d}qf jrHt| jknd}nt fdd jD }|rndS |svtdS dS )z>Assures size of every row in dataset is of proper proportions.Tr^   c                 3   s   | ]}t | jkV  qd S r   )r   width)r   xr   r   r   r      s     z$Dataset._validate.<locals>.<genexpr>FN)rr   r   rO   allrH   r   )r   r   r   ZsafetyZis_validr   r   r   rW      s    zDataset._validateTc              
      s   t j}|rt nt jrt|D ]v\}}jD ]f\}}zD|dkrjt|D ]\}}	||	|| |< qNn||| || |< W q4 tk
r   tY q4X q4q&jr|r fdd|D }
qt jgt | }
ndd |D }
|
S )z=Packages Dataset into lists of dictionaries for transmission.Nc                    s    g | ]} t tj|qS r   )r   rh   rF   )r   data_row	dict_packr   r   r   rS     s     z$Dataset._package.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   rP   r   r   r   rS     s     )	r   rH   r   dictrK   rX   
IndexErrorr   rF   )r   ZdictsorderedrH   Zrow_ir   r   callbackjcdatar   rv   r   _package  s(    
zDataset._packagec                 C   s   | j S )zAn *optional* list of strings to be used for header rows and attribute names.

        This must be set manually. The given list length must equal :attr:`Dataset.width`.

        )rI   r   r   r   r   _get_headers#  s    zDataset._get_headersc                 C   sB   |  | |r8zt|| _W q> tk
r4   tY q>X nd| _dS )zValidating headers setter.N)rW   r   rI   	TypeError)r   Z
collectionr   r   r   _set_headers+  s    
zDataset._set_headersc                 C   s   |   S )a  A native Python representation of the :class:`Dataset` object. If headers have
        been set, a list of Python dictionaries will be returned. If no headers have been set,
        a list of tuples (rows) will be returned instead.

        A dataset object can also be imported by setting the `Dataset.dict` attribute: ::

            data = tablib.Dataset()
            data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]

        )r   r   r   r   r   	_get_dict8  s    zDataset._get_dictc                 C   s   t |sdS t|d tr<|   |D ]}| t| q&nNt|d tr|   t|d  | _|D ]}| tt|	  qhnt
dS )a  A native Python representation of the Dataset object. If headers have been
        set, a list of Python dictionaries will be returned. If no headers have been
        set, a list of tuples (rows) will be returned instead.

        A dataset object can also be imported by setting the :attr:`Dataset.dict` attribute. ::

            data = tablib.Dataset()
            data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]

        Nr   )r   r7   r   wiper2   r   rx   keysrF   valuesr	   )r   pickler   r   r   r   	_set_dictE  s    zDataset._set_dictc                 C   s^   t |}| jr|dg}ng }t|dkrNt|d drNt t|d | j}t|| }|S )z,Prepares the given column for insert/append.r   r^   __call__)r   rF   popr   hasattrr]   rH   r6   r   r   headerr   r   r   
_clean_cole  s    zDataset._clean_colc                 C   s
   t | jS )zfThe number of rows currently in the :class:`Dataset`.
           Cannot be directly modified.
        )r   rH   r   r   r   r   rO   v  s    zDataset.heightc                 C   sT   zt | jd W S  tk
rN   zt | jW  Y S  tk
rH   Y Y dS X Y nX dS )ziThe number of columns currently in the :class:`Dataset`.
           Cannot be directly modified.
        r   N)r   rH   ry   rF   r   r   r   r   r   rr   }  s    zDataset.widthc                 K   s`   t |}|st|}t|}t|ds8td| dtsLtd| d|j| |f| | S )z
        Import `in_stream` to the :class:`Dataset` object using the `format`.
        `in_stream` can be a file-like object, a string, or a bytestring.

        :param \*\*kwargs: (optional) custom configuration to the format `import_set`.
        ro   Format z cannot be imported.)r   detect_formatr
   rk   r   r	   ro   r   rp   rc   rN   streamfmtr   r   r   load  s    

zDataset.loadc                 K   s2   t |}t|ds$td| d|j| f|S )z
        Export :class:`Dataset` object to `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `export_set`.
        rl   r    cannot be exported.)r
   rk   r   r	   rl   r   rc   rN   r   r   r   r   export  s    

zDataset.exportr   c                 C   s$   |  | | j|t||d dS )zInserts a row to the :class:`Dataset` at the given index.

        Rows inserted must be the correct size (height or width).

        The default behaviour is to insert the given row to the :class:`Dataset`
        object at the given index.
       )r   N)rW   rH   r,   r   )r   r3   r   r   r   r   r   r,     s    	
zDataset.insertc                 C   s   | j | j||d dS )zzAdds a row to the end of the :class:`Dataset`.
        See :method:`Dataset.insert` for additional documentation.
        r   r   N)r,   rO   r   r   r   r   r.     s    zDataset.rpushc                 C   s   | j d||d dS )zzAdds a row to the top of the :class:`Dataset`.
        See :method:`Dataset.insert` for additional documentation.
        r   r   Nr/   r   r   r   r   r0     s    zDataset.lpushc                 C   s   |  || dS )zoAdds a row to the :class:`Dataset`.
        See :method:`Dataset.insert` for additional documentation.
        Nr1   r   r   r   r   r2     s    zDataset.appendc                 C   s   |D ]}|  || qdS )z[Adds a list of rows to the :class:`Dataset` using
        :method:`Dataset.append`
        N)r2   )r   rowsr   r   r   r   r   rf     s    zDataset.extendc                 C   s   | d }| d= |S )z:Removes and returns the first row of the :class:`Dataset`.r   r   r   cacher   r   r   lpop  s    zDataset.lpopc                 C   s   | d }| d= |S )9Removes and returns the last row of the :class:`Dataset`.r   r   r   r   r   rpop  s    zDataset.rpopc                 C   s   |   S )r   )r   r   r   r   r   r     s    zDataset.popc                 C   s   |dkrg }t |dr&tt|| j}| |}| j|d | jrv|sNt n|rh| jdkrht	|rht
| j|| | jr| jrt| jD ]"\}}||||  || j|< qndd |D | _dS )a  Inserts a column to the :class:`Dataset` at the given index.

        Columns inserted must be the correct height.

        You can also insert a column of a single callable object, which will
        add a new column with the return values of the callable each as an
        item in the column. ::

            data.append_col(col=random.randint)

        If inserting a column, and :attr:`Dataset.headers` is set, the
        header attribute must be set, and will be considered the header for
        that row.

        See :ref:`dyncols` for an in-depth example.

        .. versionchanged:: 0.9.0
           If inserting a column, and :attr:`Dataset.headers` is set, the
           header attribute must be set, and will be considered the header for
           that row.

        .. versionadded:: 0.9.0
           If inserting a row, you can add :ref:`tags <tags>` to the row you are inserting.
           This gives you the ability to :method:`filter <Dataset.filter>` your
           :class:`Dataset` later.

        Nr   r   r   c                 S   s   g | ]}t |gqS r   rD   rP   r   r   r   rS   ,  s     z&Dataset.insert_col.<locals>.<listcomp>)r   r   r]   rH   r   rW   rF   r   rO   r   r   r,   rr   rX   )r   r3   r   r   r#   r   r   r   r   
insert_col  s"    

zDataset.insert_colc                 C   s   | j | j||d dS )z}Adds a column to the end of the :class:`Dataset`.
        See :method:`Dataset.insert` for additional documentation.
        r   N)r   rr   r   r   r   r   	rpush_col.  s    zDataset.rpush_colc                 C   s   | j d||d dS )z}Adds a column to the top of the :class:`Dataset`.
        See :method:`Dataset.insert` for additional documentation.
        r   r   N)r   r   r   r   r   	lpush_col5  s    zDataset.lpush_colr_   c                 C   s   ||f}| j | dS )z4Adds a separator to :class:`Dataset` at given index.N)rJ   r2   )r   r3   textsepr   r   r   insert_separator<  s    zDataset.insert_separatorc                 C   s<   | j s| jr| jnd}n| jr(| jd nd}| || dS )z=Adds a :ref:`separator <separators>` to the :class:`Dataset`.r   r^   N)rF   rO   r   )r   r   r3   r   r   r   append_separatorB  s    zDataset.append_separatorc                 C   s   |  || dS )zvAdds a column to the :class:`Dataset`.
        See :method:`Dataset.insert_col` for additional documentation.
        N)r   r   r   r   r   
append_colM  s    zDataset.append_colc                    s    fdd| j D S )z@Returns the column from the :class:`Dataset` at the given index.c                    s   g | ]}|  qS r   r   rP   r3   r   r   rS   W  s     z#Dataset.get_col.<locals>.<listcomp>)rH   )r   r3   r   r   r   get_colT  s    zDataset.get_colc                 C   sJ   t |tr&|| jkr"| j|}nt|| jksB| j||f ntdS )a  Adds a formatter to the :class:`Dataset`.

        .. versionadded:: 0.9.5

        :param col: column to. Accepts index int or header str.
        :param handler: reference to callback function to execute against
                        each cell value.
        T)	r7   r8   rF   r3   rU   rr   rK   r2   r   )r   r   handlerr   r   r   add_formatter]  s    



zDataset.add_formatterc                    s"   t | } fdd|jD |_|S )zReturns a new instance of the :class:`Dataset`, excluding any rows
        that do not contain the given :ref:`tags <tags>`.
        c                    s   g | ]}|  r|qS r   )r<   rP   r;   r   r   rS   y  s     
 z"Dataset.filter.<locals>.<listcomp>)r   rH   )r   r;   _dsetr   r   r   filtert  s    zDataset.filterc                    s   t |trd| jstt| jt||d}t| j| jd}|D ]$  fdd| jD }|j	|d q<nj| jrt| j| }t| jt||d}t| j| jd}|D ]0 | jr fdd| jD }n }|j	|d q|S )a  Sort a :class:`Dataset` by a specific column, given string (for
        header) or integer (for column index). The order can be reversed by
        setting ``reverse`` to ``True``.

        Returns a new :class:`Dataset` instance where columns have been
        sorted.
        )rV   reverse)rF   rG   c                    s   g | ]} | qS r   r   r   rV   r4   r   r   rS     s     z Dataset.sort.<locals>.<listcomp>r   c                    s   g | ]} | qS r   r   r   r   r   r   rS     s     )
r7   r8   rF   r   sortedrx   r   rC   rG   r2   )r   r   r   Z_sortedr   r   r   r   r   sort}  s$    	

zDataset.sortc                 C   sz   | sdS t  }| jd g| | jd   }||_t| jD ]<\}}|| jd krPq8|g| | }t|}|j|d q8|S )zTranspose a :class:`Dataset`, turning rows into columns and vice
        versa, returning a new ``Dataset`` instance. The first row of the
        original instance becomes the new header row.Nr   r   )rC   rF   rX   r   r   r2   )r   r   new_headersr3   columnZrow_datar   r   r   	transpose  s    zDataset.transposec                 C   sZ   t |tsdS | j|jkrtt| }dd |jD }dd |jD }|| ||_|S )zStack two :class:`Dataset` instances together by
        joining at the row level, and return new combined
        ``Dataset`` instance.Nc                 S   s   g | ]}|qS r   r   rP   r   r   r   rS     s     z!Dataset.stack.<locals>.<listcomp>c                 S   s   g | ]}|qS r   r   rP   r   r   r   rS     s     )r7   rC   rr   r   r   rH   rf   )r   otherr   Zrows_to_stackZ
other_rowsr   r   r   stack  s    

zDataset.stackc                 C   s   t |tsdS | js|jr*| jr&|js*t| j|jkr:tz| j|j }W n tk
rb   d}Y nX t }| jD ]}|j| | d qp|jD ]}|j|| d q||_|S )zStack two :class:`Dataset` instances together by
        joining at the column level, and return a new
        combined ``Dataset`` instance. If either ``Dataset``
        has headers set, than the other must as well.Nr   )r7   rC   rF   r   rO   r   r   r   )r   r   r   r   r   r   r   r   
stack_cols  s$    



zDataset.stack_colsc                    s(   t    fdd| jD | jdd< dS )ziRemoves all duplicate rows from the :class:`Dataset` object
        while maintaining the original order.c                    s*   g | ]"}t | ks t |s|qS r   )r6   addrP   seenr   r   rS     s       z-Dataset.remove_duplicates.<locals>.<listcomp>N)r:   rH   r   r   r   r   remove_duplicates  s    zDataset.remove_duplicatesc                 C   s   t  | _d| _dS )zARemoves all content and headers from the :class:`Dataset` object.N)r   rH   rI   r   r   r   r   r     s    zDataset.wipec           	         s    sdS |dkrt t j}|dkr0t  j} fdd|D } fdd|D }t }t ||_g |_t jD ]Z\}}g }|jD ].}| jkr j|}|||  qt	q||krt|jt
|d qt|S )zkReturns a new instance of the :class:`Dataset`,
        including only specified rows and columns.
        Nc                    s   g | ]}|t  jkr|qS r   )rangerO   rP   r   r   r   rS     s      z"Dataset.subset.<locals>.<listcomp>c                    s   g | ]}| j kr|qS r   )rF   )r   r   r   r   r   rS     s     
 r   )r   r   rO   rF   rC   rH   rX   r3   r2   rU   r   )	r   r   colsr   Zrow_nor   ru   rV   rR   r   r   r   subset   s*    



zDataset.subset)NNF)TT)N)r   )r   )r   )r   )r   )NN)N)N)r_   )r_   )N)F)NN)3r=   r>   r?   r@   r   r   r$   r&   r'   r!   rj   rn   rq   rW   r   r   r   rB   rF   r   r   rx   r   rO   rr   r   r   r,   r.   r0   r2   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rC   e   sb   .

"









	
;




		
%!rC   c                   @   s`   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdddZ	e
dd Zdd Zdd ZdS )Databookz(A book of :class:`Dataset` objects.
    Nc                 C   s   |pg | _ d S r   	_datasets)r   Zsetsr   r   r   r   ,  s    zDatabook.__init__c                 C   s,   zd| j   W S  tk
r&   Y dS X d S )Nz<%s databook>z<databook object>rY   r   r   r   r   r!   /  s    zDatabook.__repr__c                 C   s
   g | _ dS )z@Removes all :class:`Dataset` objects from the :class:`Databook`.Nr   r   r   r   r   r   5  s    zDatabook.wipec                 C   s   | j S r   r   r   r   r   r   sheets9  s    zDatabook.sheetsc                 C   s    t |tr| j| ntdS )z5Adds given :class:`Dataset` to the :class:`Databook`.N)r7   rC   r   r2   r   )r   Zdatasetr   r   r   	add_sheet<  s    
zDatabook.add_sheetTc                 C   s>   g }|rt }nt}| jD ] }|||j|j|dd q|S )z(Packages :class:`Databook` for delivery.)rz   )rG   r~   )r   rx   r   r2   rG   r   )r   rz   	collectorrw   Zdsetr   r   r   r   C  s    


zDatabook._packagec                 C   s
   t | jS )zDThe number of the :class:`Dataset` objects within :class:`Databook`.)r   r   r   r   r   r   sizeS  s    zDatabook.sizec                 K   sL   t |}|st|}t|}t|ds8td| d|j| |f| | S )z
        Import `in_stream` to the :class:`Databook` object using the `format`.
        `in_stream` can be a file-like object, a string, or a bytestring.

        :param \*\*kwargs: (optional) custom configuration to the format `import_book`.
        import_bookr   z cannot be loaded.)r   r   r
   rk   r   r	   r   r   r   r   r   r   X  s    

zDatabook.loadc                 K   s2   t |}t|ds$td| d|j| f|S )z
        Export :class:`Databook` object to `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `export_book`.
        export_bookr   r   )r
   rk   r   r	   r   r   r   r   r   r   k  s    

zDatabook.export)N)T)r=   r>   r?   r@   r   r!   r   r   r   r   rB   r   r   r   r   r   r   r   r   (  s   


r   c              
   C   sn   t | } d}t D ]T}z8z|| r6|j}W W   qjW n tk
rL   Y nX W 5 t| drf| d X q|S )zMReturn format name of given stream (file-like object, string, or bytestring).Nseekr   )r   r
   formatsr   r   detectrG   r[   )r   Z	fmt_titler   r   r   r   r   x  s    


r   Nc                 K   s   t  jt| |f|S zIReturn dataset of given stream (file-like object, string, or bytestring).)rC   r   r   r   rc   rN   r   r   r   ro     s    ro   c                 K   s   t  jt| |f|S r   )r   r   r   r   r   r   r   r     s    r   )N)N)r@   collectionsr   r   operatorr   Ztablib.exceptionsr   r   r   r   r	   Ztablib.formatsr
   Ztablib.utilsr   	__title__
__author____license____copyright__Z__docformat__r   rC   r   r   ro   r   Zregister_builtinsr   r   r   r   <module>   s.   
E     HP

