U
    d                     @   sJ   d Z ddlZddlZddlZddlmZmZ ddlmZ G dd dZ	dS )z Tablib - DBF Support.
    N)dbfdbfnew)recordc                   @   s>   e Zd ZdZdZdZedd ZedddZed	d
 Z	dS )	DBFFormatr   )csvzutf-8c                 C   s   t  }t \}}|d }t|j|D ]8\}}t|ttfkrR|	|ddd q(|	|dd q(|
| tj|dd}|D ]4}	t|}
t|j|	D ]\}}||
|< q|
  q~|  t|d}t| }|  t| t| | S )	z'Returns DBF representation of a Datasetr   N
      CP   ZreadOnlyrb)r   Zdbf_newtempfilemkstempzipheaderstypeintfloatZ	add_fieldwriter   Dbf	dbfrecordZ	DbfRecordstorecloseopenioBytesIOreadosremovegetvalue)clsZdatasetZnew_dbf	temp_fileZtemp_uriZ	first_row	fieldnameZfield_valueZdbf_filerowr   Z
dbf_streamstream r&   7/tmp/pip-unpacked-wheel-_3ph8cfy/tablib/formats/_dbf.py
export_set   s*    






zDBFFormat.export_setTc                    sN   |   t|  j|_t jD ]$ fdd jD }|| q$dS )z$Returns a dataset from a DBF stream.c                    s   g | ]}  | qS r&   r&   ).0f_dbfr   r&   r'   
<listcomp>8   s     z(DBFFormat.import_set.<locals>.<listcomp>N)Zwiper   r   Z
fieldNamesr   rangeZrecordCountappend)r!   ZdsetZ	in_streamr   r$   r&   r+   r'   
import_set0   s    
zDBFFormat.import_setc                 C   s0   zt j|dd}W dS  tk
r*   Y dS X dS )z-Returns True if the given stream is valid DBFTr   FN)r   r   	Exception)r!   r%   r,   r&   r&   r'   detect;   s
    zDBFFormat.detectN)T)
__name__
__module____qualname__title
extensionsZDEFAULT_ENCODINGclassmethodr(   r0   r2   r&   r&   r&   r'   r      s   

r   )
__doc__r   r   r   Ztablib.packages.dbfpyr   r   r   r   r   r&   r&   r&   r'   <module>   s   