Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/dbf.cpython-37.pyc

B

�d{T}�@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	m
Z
Gdd�d�Zdd�Ze
d	kr�ed
�Zx,eD]$Zeded��ed
��f�qxWdS)a?This is a DBF reader which reads Visual Fox Pro DBF format with Memo field

Usage:

>>> rec = readDbf('test.dbf')
>>> for line in rec:
>>>     print line['name']


:date: 13/07/2007

http://www.physics.ox.ac.uk/users/santoso/Software.Repository.html
page says code is "available as is without any warranty or support".
�)�print_functionN)�rangec@s\eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�ZdS)�DbasecCsFd|_d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
dS)Nr�)�fdb�fmemo�db_data�	memo_data�fields�num_records�header�	memo_file�memo_header�memo_block_size�memo_header_len)�self�r�C/opt/alt/python37/lib/python3.7/site-packages/logilab/common/dbf.py�__init__,szDbase.__init__cCsDx>tdt|��D],}tt�d||�d�dkr|d|�SqW|S)Nr�c)r�len�ord�struct�unpack)rZtxt�irrr�_drop_after_NULL9szDbase._drop_after_NULLcCs<t|�sdSt�d|�}t�d|d�}t�d|�}|dS)Nrz<Lz>L)rrr�pack)r�num�valrrr�_reverse_endian?szDbase._reverse_endiancCs2i}d}x$|D]}||}|||<|d7}qW|S)Nr�r)r�lst�ids�result�idx�item�idrrr�_assign_idsGs
zDbase._assign_idsc
Cs�tj�|�}|dkrtd��t|d�|_d|_tj�|dd�d�rZ|dd�d|_n*tj�|dd�d	�r�|dd�d	|_|jr�t|jd�|_|j�	�|_
|�t�
d
|j
dd��dg�|_|jd}|s�d
}||_||_tj�|j�}|j�	d�}|�t�
d|�dddddddg�|_t|jd�|jd<|jd|_|j�	|jdd�}i|_d}d}ddddg}d}	x�tdt|�d�D]�}
t||
�dk�r�P|d7}||
|
|	�}t|�|	k�r�|d|	t|�7}|�t�
||�|�|j|<|�|j|d�|j|d<�q�W|j�	d �|jd�rX|jd|jdd}|j�	|�|_nd|_d!|_g|_d|_xR|jD]H}
|j|
}|jd"|d7_|j�|d�|j|d7_�qxWdS)#N�Dz-The file is not large enough to be a dbf file�rbrr����t���Zfptz>6x1H�z
Block sizei� z<B 3B L 2H 20xr&ZYearZMonthZDayz# of RecordszHeader SizezRecord Size�"z<11s c 4x B B 14xz
Field Namez
Field TypezField LengthzField Precision�
r � ��<z%ds )�os�path�getsize�IOError�openrr
�isfiler�readr	r'rrrrrr�hexrr
rrrrr�
row_format�row_ids�row_len�append)rZdb_name�filesize�
block_sizeZ	memo_size�data�xZheader_patternr"Zpattern_len�offsetZdata_subsetZ	data_size�key�fieldrrrr8Psf"
$$
z
Dbase.opencCs$|jr|j��|jr |j��dS)N)r�closer)rrrrrG�s
zDbase.closecCs|jS)N)r)rrrr�get_numrecords�szDbase.get_numrecordsc
Cs�|dks||jkrtd��|jd|}|j|||j�}|�t�|j|�|j	�}|j
r�x�|jD]x}|j|}|d}|d}||}	|dks�|dks�|dks�|d	kr�|�|	�}	|	r�|�
|	d
���||<qb|	��||<qbW|S)zB
        This function accept record number from 0 to N-1
        rz(Unable to extract data outside the rangezRecord Sizez
Field Typez
Field Name�M�G�B�Pr )r�	Exceptionrrr>r'rrr<r=r
r
r�	read_memo�strip)
rZrec_norDrB�recordrErFZf_type�f_nameZc_datarrr�get_record_with_names�s"
 
zDbase.get_record_with_namescCsR|dkr|j}|j||j}|j�|�|dkr:|}|dkrFdS|j�|�S)z�
        Read the record of given number. The second parameter is the length of
        the record to read. It can be undefined, meaning read the whole record,
        and it can be negative, meaning at most the length
        rr)rrr�seekr:)rrZ	in_lengthrDrrr�read_memo_record�szDbase.read_memo_recordcCs�d}|�|d�}t|�dkr dSt�d|dd��dd}|j}||krV|d|�S||}|�|d|�}t|�dkr~dS|dd�|S)Nrr*rz>L�r-r )rTrrrr)rrr#�buffer�lengthrAZrest_lengthZ	rest_datarrrrN�szDbase.read_memoN)
�__name__�
__module__�__qualname__rrrr'r8rGrHrRrTrNrrrrr+s
	>rcCsPt�}|�|�|��}g}x&td|�D]}|�|�}|�|�q(W|��|S)z�
    Read the DBF file specified by the filename and
    return the records as a list of dictionary.

    :param: filename File name of the DBF
    :return: List of rows
    r)rr8rHrrRr?rG)�filenameZdbr�recrrPrrr�readDbf�s

r]�__main__zdbf/sptable.dbfz%s %sZGENUSZSPECIES)�__doc__�
__future__rrr4�os.path�sysZcsv�tempfileZ	six.movesrrr]rXr\�line�printrOrrrr�<module> s(