o
    „s¢Y2$  ã                   @   s°   d Z 	 ddd… Zddd… ZdZdgZdd	lmZ dd
lmZ ddlm	Z	 G dd„ de
ƒZdd„ Zdd„ ZedkrVddlZeejƒdkrJejd pKdZeeƒ eeƒ dS dS )a»  DBF accessing helpers.

FIXME: more documentation needed

Examples:

    Create new table, setup structure, add records:

        dbf = Dbf(filename, new=True)
        dbf.addField(
            ("NAME", "C", 15),
            ("SURNAME", "C", 25),
            ("INITIALS", "C", 10),
            ("BIRTHDATE", "D"),
        )
        for (n, s, i, b) in (
            ("John", "Miller", "YC", (1980, 10, 11)),
            ("Andy", "Larkin", "", (1980, 4, 11)),
        ):
            rec = dbf.newRecord()
            rec["NAME"] = n
            rec["SURNAME"] = s
            rec["INITIALS"] = i
            rec["BIRTHDATE"] = b
            rec.store()
        dbf.close()

    Open existed dbf, read some data:

        dbf = Dbf(filename, True)
        for rec in dbf:
            for fldName in dbf.fieldNames:
                print '%s:	 %s (%s)' % (fldName, rec[fldName],
                    type(rec[fldName]))
            print
        dbf.close()

z$Revision: 1.7 $é   éþÿÿÿz$Date: 2007/02/11 09:23:13 $é   z1Jeff Kunce <kuncej@mail.conservation.state.mo.us>ÚDbfé   )Úheader)Úrecord)ÚINVALID_VALUEc                   @   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/env/lib/python3.10/site-packages/tablib/packages/dbfpy3/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    z¹Error 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   F   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)rO   Ú__version__Ú__date__Ú
__author__Ú__all__r   r   r   Úutilsr   Úobjectr   rZ   ry   rL   Úsysr5   ÚargvÚ_namer   r   r   r   Ú<module>   s&   & >û