
    $g$                        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	          Z	d Z
d Zedk    r?ddlZ eej                  d k    rej        d          pd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                      e Zd ZdZdZej        Zej	        Z
eZddZ ed           Z ed           Z ed           Z ed           Z ed	           Zd
 Z ed ed          Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z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                    t          |t                    rH|| _        |rt          |d          | _        nFt          |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   s        X/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/tablib/_vendor/dbfpy/dbf.py__init__zDbf.__init__]   s    . a 	DI E #1enn #1mDNN&CDD  62..DIDK 	C**,,DKK *55dkBBDK(II	    c                     | j         j        S N)r   closedr   s    r   <lambda>zDbf.<lambda>   s    4;#5 r!   c                     | j         j        S r#   )r   recordCountr%   s    r   r&   zDbf.<lambda>   s    (? r!   c                 .    d | j         j        D             S )Nc                     g | ]	}|j         
S  r   ).0_flds     r   
<listcomp>z Dbf.<lambda>.<locals>.<listcomp>   s    ???Ddi???r!   r   fieldsr%   s    r   r&   zDbf.<lambda>   s    ??DK,>??? r!   c                     | j         j        S r#   r0   r%   s    r   r&   zDbf.<lambda>   s    dk&8 r!   c                 (    | j         p| j        j        S r#   )r   r   changedr%   s    r   r&   zDbf.<lambda>   s    DM$HT[5H r!   c                 F    t          |          x| j        _        | _        dS )z8Update `ignoreErrors` flag on the header object and selfN)r   r   r   r   )r   values     r   r   zDbf.ignoreErrors   s     9=eD 4#6#6#6r!   c                     | j         S r#   )r   r%   s    r   r&   zDbf.<lambda>   s	    T( r!   zError processing mode for DBF field value conversion

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

        )docc                     t          |t                    st          d          |dk     r|t          |           dz   z  }|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   indexs     r   	_fixIndexzDbf._fixIndex   si     %%% 	><===199 SYY]"ECII8999r!   c                 `    |                                   | j                                         d S r#   )flushr   closer%   s    r   rD   z	Dbf.close   s)    

r!   c                     | j         rZ| j                                         | j                            | j                   | j                                         d| _        dS dS )z$Flush data to the associated stream.FN)r4   r   setCurrentDatewriter   rC   r   r%   s    r   rC   z	Dbf.flush   sa    < 	"K&&(((Kdk***K!DMMM		" 	"r!   c                 |    d | j         j        D             }|                    |                                          S )zIndex of field named ``name``.c                     g | ]	}|j         
S r+   r,   )r-   r   s     r   r/   z(Dbf.indexOfFieldName.<locals>.<listcomp>   s    444A444r!   )r   r1   r@   upper)r   r   namess      r   indexOfFieldNamezDbf.indexOfFieldName   s6     54!3444{{4::<<(((r!   c                 ,    |                      |           S )z.Return new record, which belong to this table.)RecordClassr%   s    r   	newRecordzDbf.newRecord   s    %%%r!   c                     | j         j        |_         |j                     | j         xj        dz  c_        d| _        d| _        dS )z"Append ``record`` to the database.r   TFN)r   r(   r@   _writer   r   )r   r   s     r   appendz
Dbf.append   sC    {.1$			r!   c                 P    | 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   addFieldr<   )r   defss     r   rT   zDbf.addField   s=     9 	: DK $'''' 9 : : :r!   c                 @    d| j         z  t          | j                  z   S )NzDbf stream '%s'
)r   reprr   r%   s    r   __repr__zDbf.__repr__   s    "T[043D3DDDr!   c                     | j         S )zReturn number of records.)r(   r%   s    r   __len__zDbf.__len__   s    r!   c                 ^    | j                             | |                     |                    S )zReturn `DbfRecord` instance.)rN   r   rA   r?   s     r   __getitem__zDbf.__getitem__   s'    **41F1FGGGr!   c                 t    |                      |          |_         |j                     d| _        d| _        dS )z)Write `DbfRecord` instance to the stream.TFN)rA   r@   rQ   r   r   )r   r@   r   s      r   __setitem__zDbf.__setitem__   s4    ~~e,,			r!   N)FFF)__name__
__module____qualname____doc__	__slots__r   	DbfHeaderr   r   	DbfRecordrN   r   r    propertyr$   r(   
fieldNames	fieldDefsr4   r   rA   rD   rC   rL   rO   rR   rT   rX   rZ   r\   r^   r+   r!   r   r	   r	   E   s        7I "K"K!M- - - -b X5566F(??@@K??A AJ8899IhHHIIGE E E 8((  L  4  " " ") ) )& & &  
: 
: 
:E E E     H H H    r!   c                     t          | d          }|D ],}t                       t          t          |                     -|                                 d S )NT)r	   printrW   rD   )filename_dbf_recs      r   	demo_readrn     sQ    xD  d4jjJJLLLLLr!   c                 D   t          | d          }|                    dddd           dD ]C\  }}}}|                                }||d<   ||d	<   ||d
<   ||d<   |                                 Dt	          t          |                     |                                 d S )NT)r   )NAMEC   )SURNAMErq      )INITIALSrq   
   )	BIRTHDATED))JohnMillerYC)i  r      )AndyLarkinAL)i        )BillClinthr   )i        )BobbMcNailr   )i  r      rp   rs   ru   rw   )r	   rT   rO   storerj   rW   rD   )rk   rl   _n_s_i_brm   s          r   demo_creater   
  s    xT"""DMM	    RR ~~VYZ[

	$t**JJLLLLLr!   __main__r:   Nz
county.dbf)r   r   r   utilsr   __version____date__
__author____all__r	   rn   r   r_   sysr=   argv_namer+   r!   r   <module>r      s                       B')!B$/@
'%L(v v v v v v v vz    0 zJJJCMMA-#(1+=EKIe r!   