o
    e$                     @   s   d dl mZmZ d dlmZ ddd Zddd ZdZd	gZ	 	 G d
d	 d	Z	dd Z
dd ZedkrPddlZeejd krDejd  pEdZee e
e dS dS )   )headerrecord)INVALID_VALUEz$Revision: 1.7 $   z$Date: 2007/02/11 09:23:13 $   z1Jeff Kunce <kuncej@mail.conservation.state.mo.us>Dbfc                   @   s   e Zd ZdZdZejZej	Z
eZd(ddZedd Zedd Zed	d Zed
d Zedd Zdd Zedd e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'S ))r   a  DBF accessor.

    FIXME:
        docs and examples needed (dont' forget to tell
        about problems adding new fields on the fly)

    Implementation notes:
        ``_new`` field is used to indicate whether this is
        a new data table. `addField` could be used only for
        the new tables! If at least one record was appended
        to the table it's structure couldn't be changed.

    )namer   stream_changed_new_ignore_errorsFc                 C   s   t |tr|| _|rt|d| _nt|dt| | _n
t|dd| _|| _|r.|  | _n| j	| j| _|| _
t|| _d| _dS )af  Initialize instance.

        Arguments:
            f:
                Filename or file-like object.
            new:
                True if new data table must be created. Assume
                data table exists if this argument is False.
            readOnly:
                if ``f`` argument is a string file will
                be opend in read-only mode; in other cases
                this argument is ignored. This argument is ignored
                even if ``new`` argument is True.
            headerObj:
                `header.DbfHeader` instance or None. If this argument
                is None, new empty header will be used with the
                all fields set by default.
            ignoreErrors:
                if set, failing field value conversion will return
                ``INVALID_VALUE`` instead of raising conversion error.

        zw+b)zr+brbr	    FN)
isinstancestrr	   openr
   boolgetattrHeaderClassr   
fromStreamignoreErrorsr   r   )selffreadOnlynewr    r   L/var/www/html/venv/lib/python3.10/site-packages/tablib/packages/dbfpy/dbf.py__init__]   s   


zDbf.__init__c                 C      | j jS N)r
   closedr   r   r   r   <lambda>       zDbf.<lambda>c                 C   r   r    )r   recordCountr"   r   r   r   r#      r$   c                 C   s   dd | j jD S )Nc                 S      g | ]}|j qS r   r	   ).0_fldr   r   r   
<listcomp>       z Dbf.<lambda>.<locals>.<listcomp>r   fieldsr"   r   r   r   r#      r+   c                 C   r   r    r,   r"   r   r   r   r#      r$   c                 C   s   | j p| jjS r    )r   r   changedr"   r   r   r   r#      s    c                 C   s   t | | j_| _dS )z8Update `ignoreErrors` flag on the header object and selfN)r   r   r   r   )r   valuer   r   r   r      s   zDbf.ignoreErrorsc                 C      | j S r    )r   r"   r   r   r   r#      s    zError processing mode for DBF field value conversion

        if set, failing field value conversion will return
        ``INVALID_VALUE`` instead of raising conversion error.

        )docc                 C   sB   t |ts	td|dk r|t| d 7 }|t| krtd|S )a  Return fixed index.

        This method fails if index isn't a numeric object
        (long or int). Or index isn't in a valid range
        (less or equal to the number of records in the db).

        If ``index`` is a negative number, it will be
        treated as a negative indexes for list objects.

        Return:
            Return value is numeric object maning valid index.

        zIndex must be a numeric object    r   zRecord index out of range)r   int	TypeErrorlen
IndexErrorr   indexr   r   r   	_fixIndex   s   
zDbf._fixIndexc                 C   s   |    | j  d S r    )flushr
   closer"   r   r   r   r;      s   z	Dbf.closec                 C   s6   | j r| j  | j| j | j  d| _dS dS )z$Flush data to the associated stream.FN)r.   r   setCurrentDatewriter
   r:   r   r"   r   r   r   r:      s   


z	Dbf.flushc                 C   s    dd | j jD }|| S )zIndex of field named ``name``.c                 S   r&   r   r'   )r(   r   r   r   r   r*      r+   z(Dbf.indexOfFieldName.<locals>.<listcomp>)r   r-   r8   upper)r   r	   namesr   r   r   indexOfFieldName   s   zDbf.indexOfFieldNamec                 C   s
   |  | S )z.Return new record, which belong to this table.)RecordClassr"   r   r   r   	newRecord   s   
zDbf.newRecordc                 C   s2   | j j|_|  | j  jd7  _d| _d| _dS )z"Append ``record`` to the database.r   TFN)r   r%   r8   _writer   r   )r   r   r   r   r   append   s
   

z
Dbf.appendc                 G   s   | j r| jj|  dS td)z_Add field definitions.

        For more information see `header.DbfHeader.addField`.

        z9At least one record was added, structure can't be changedN)r   r   addFieldr4   )r   defsr   r   r   rE      s   zDbf.addFieldc                 C   s   d| j  t| j S )NzDbf stream '%s'
)r
   reprr   r"   r   r   r   __repr__   s   zDbf.__repr__c                 C   r0   )zReturn number of records.)r%   r"   r   r   r   __len__   s   zDbf.__len__c                 C   s   | j | | |S )zReturn `DbfRecord` instance.)rA   r   r9   r7   r   r   r   __getitem__   s   zDbf.__getitem__c                 C   s$   |  ||_|  d| _d| _dS )z)Write `DbfRecord` instance to the stream.TFN)r9   r8   rC   r   r   )r   r8   r   r   r   r   __setitem__   s   
zDbf.__setitem__N)FFF)__name__
__module____qualname____doc__	__slots__r   	DbfHeaderr   r   	DbfRecordrA   r   r   propertyr!   r%   
fieldNames	fieldDefsr.   r   r9   r;   r:   r@   rB   rD   rE   rH   rI   rJ   rK   r   r   r   r   r   E   s>    
1c                 C   s2   t | d}|D ]}t  tt| q|  d S )NT)r   printrG   r;   )filename_dbf_recr   r   r   	demo_read  s
   
rZ   c                 C   sv   t | dd}|dddd dD ]\}}}}| }||d< ||d	< ||d
< ||d< |  qtt| |  d S )NT)r   )NAMEC   )SURNAMEr\      )INITIALSr\   
   )	BIRTHDATED))JohnMillerYC)i  r      )AndyLarkinAL)i        )BillClinthr   )i        )BobbMcNailr   )i  r      r[   r^   r`   rb   )r   rE   rB   storerV   rG   r;   )rW   rX   _n_s_i_brY   r   r   r   demo_create
  s    
ry   __main__r2   Nz
county.dbf)r   r   r   utilsr   __version____date__
__author____all__r   rZ   ry   rL   sysr5   argv_namer   r   r   r   <module>   s$   & >