o
    „s¢Yÿ   ã                   @   sL   d Z 	 ddd… Zddd… ZdgZddlmZ dd	lZG d
d„ deƒZd	S )zDBF record definition.

z$Revision: 1.7 $é   éþÿÿÿz$Date: 2007/02/11 09:05:49 $é   Ú	DbfRecordé    )ÚizipNc                   @   s®   e Zd ZdZdZd$dd„Zedd„ ƒZd	d
„ Ze	eƒZdd„ Z
e	e
ƒZ
d%dd„Ze	eƒZdd„ Zdd„ Zd&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 record.

    Instances of this class shouldn't be created manualy,
    use `dbf.Dbf.newRecord` instead.

    Class implements mapping/sequence interface, so
    fields could be accessed via their names or indexes
    (names is a preffered way to access fields).

    Hint:
        Use `store` method to save modified record.

    Examples:
        Add new record to the database:
            db = Dbf(filename)
            rec = db.newRecord()
            rec["FIELD1"] = value1
            rec["FIELD2"] = value2
            rec.store()
        Or the same, but modify existed
        (second in this case) record:
            db = Dbf(filename)
            rec = db[2]
            rec["FIELD1"] = value1
            rec["FIELD2"] = value2
            rec.store()

    )ÚdbfÚindexÚdeletedÚ	fieldDataNFc                 C   s@   || _ || _|| _|du rdd„ |jjD ƒ| _dS t|ƒ| _dS )a$  Instance initialiation.

        Arguments:
            dbf:
                A `Dbf.Dbf` instance this record belonogs to.
            index:
                An integer record index or None. If this value is
                None, record will be appended to the DBF.
            deleted:
                Boolean flag indicating whether this record
                is a deleted record.
            data:
                A sequence or None. This is a data of the fields.
                If this argument is None, default values will be used.

        Nc                 S   s   g | ]}|j ‘qS © )ÚdefaultValue©Ú.0Ú_fdr   r   úN/var/www/html/env/lib/python3.10/site-packages/tablib/packages/dbfpy/record.pyÚ
<listcomp>O   s    z&DbfRecord.__init__.<locals>.<listcomp>)r   r   r	   ÚheaderÚfieldsr
   Úlist)Úselfr   r   r	   Údatar   r   r   Ú__init__9   s   zDbfRecord.__init__c                 C   s   | j jj| j| j jj  S ©N)r   r   ÚheaderLengthr   ÚrecordLength©r   r   r   r   Ú<lambda>T   s    ÿzDbfRecord.<lambda>c                 C   s,   |j  |jj||jj  ¡ |j  |jj¡S )ao  Return raw record contents read from the stream.

        Arguments:
            dbf:
                A `Dbf.Dbf` instance containing the record.
            index:
                Index of the record in the records' container.
                This argument can't be None in this call.

        Return value is a string containing record data in DBF format.

        )ÚstreamÚseekr   r   r   Úread©Úclsr   r   r   r   r   ÚrawFromStreamW   s   
ÿzDbfRecord.rawFromStreamc                 C   s   |   ||  ||¡|¡S )a]  Return a record read from the stream.

        Arguments:
            dbf:
                A `Dbf.Dbf` instance new record should belong to.
            index:
                Index of the record in the records' container.
                This argument can't be None in this call.

        Return value is an instance of the current class.

        )Ú
fromStringr"   r    r   r   r   Ú
fromStreaml   s   zDbfRecord.fromStreamc                    s(   | ||ˆ d dk‡ fdd„|j jD ƒƒS )a²  Return record read from the string object.

        Arguments:
            dbf:
                A `Dbf.Dbf` instance new record should belong to.
            string:
                A string new record should be created from.
            index:
                Index of the record in the container. If this
                argument is None, record will be appended.

        Return value is an instance of the current class.

        r   Ú*c                    s   g | ]}|  ˆ ¡‘qS r   )ÚdecodeFromRecordr   ©Ústringr   r   r   Œ   s    z(DbfRecord.fromString.<locals>.<listcomp>)r   r   )r!   r   r(   r   r   r'   r   r#   |   s   ÿzDbfRecord.fromStringc                 C   sv   dt dd„ | jjD ƒƒ }g }| jjD ]"}| | }|tju r)| ||ddf ¡ q| |||t|ƒf ¡ qd |¡S )Nz%%%ds: %%s (%%s)c                 S   s   g | ]}t |ƒ‘qS r   )Úlen)r   Ú_fldr   r   r   r   ’   s    ÿz&DbfRecord.__repr__.<locals>.<listcomp>ÚNonezvalue cannot be decodedÚ
)Úmaxr   Ú
fieldNamesÚutilsÚINVALID_VALUEÚappendÚtypeÚjoin)r   Ú	_templateÚ_rvr*   Ú_valr   r   r   Ú__repr__‘   s   

ÿ
ÿ
zDbfRecord.__repr__c                 C   sR   |   d¡ | jj | j¡ | jj |  ¡ ¡ | jt| jƒkr'| jj d¡ dS dS )zýWrite data to the dbf stream.

        Note:
            This isn't a public method, it's better to
            use 'store' instead publically.
            Be design ``_write`` method should be called
            only from the `Dbf` instance.


        FúN)	Ú_validateIndexr   r   r   ÚpositionÚwriteÚtoStringr   r)   r   r   r   r   Ú_write    s   
ýzDbfRecord._writeTc                 C   s`   | j du r|stdƒ‚dS | j dk rtd| j  ƒ‚|r,| j | jjjkr.td| jjj ƒ‚dS dS )z§Valid ``self.index`` value.

        If ``allowUndefined`` argument is True functions does nothing
        in case of ``self.index`` pointing to None object.

        NzIndex is undefinedr   zIndex can't be negative (%s)z$There are only %d records in the DBF)r   Ú
ValueErrorr   r   ÚrecordCount)r   ÚallowUndefinedÚ
checkRanger   r   r   r9   ·   s   
ÿ
ÿÿzDbfRecord._validateIndexc                 C   s>   |   ¡  | jdu rt| jƒ| _| j | ¡ dS | | j| j< dS )z½Store current record in the DBF.

        If ``self.index`` is None, this record will be appended to the
        records of the DBF this records belongs to; or replaced otherwise.

        N)r9   r   r)   r   r1   r   r   r   r   ÚstoreÉ   s
   
zDbfRecord.storec                 C   s
   d| _ dS )zMark method as deleted.TN)r	   r   r   r   r   Údelete×   s   
zDbfRecord.deletec                 C   s.   d  d| j gdd„ t| jjj| jƒD ƒ ¡S )z#Return string packed record values.Ú z *c                 S   s   g | ]	\}}|  |¡‘qS r   )ÚencodeValue)r   Ú_defÚ_datr   r   r   r   Ý   s    ÿÿz&DbfRecord.toString.<locals>.<listcomp>)r3   r	   r   r   r   r   r
   r   r   r   r   r<   Û   s   
þzDbfRecord.toStringc                 C   s   | j dd… S )zšReturn a flat list of fields.

        Note:
            Change of the list's values won't change
            real values stored in this object.

        N)r
   r   r   r   r   ÚasListâ   s   zDbfRecord.asListc                 C   s   t dd„ t| jj| jƒD ƒƒS )zœReturn a dictionary of fields.

        Note:
            Change of the dicts's values won't change
            real values stored in this object.

        c                 S   s   g | ]}|‘qS r   r   )r   Ú_ir   r   r   r   ô   s    z$DbfRecord.asDict.<locals>.<listcomp>)Údictr   r   r.   r
   r   r   r   r   ÚasDictì   s   zDbfRecord.asDictc                 C   s*   t |ttfƒr| j| S | j| j |¡ S )z*Return value by field name or field index.)Ú
isinstanceÚlongÚintr
   r   ÚindexOfFieldName)r   Úkeyr   r   r   Ú__getitem__ö   s   
zDbfRecord.__getitem__c                 C   s.   t |ttfƒr| j| S || j| j |¡< dS )z=Set field value by integer index of the field or string name.N)rL   rN   rM   r
   r   rO   )r   rP   Úvaluer   r   r   Ú__setitem__þ   s   
zDbfRecord.__setitem__)NFNr   )TF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Úpropertyr:   r"   Úclassmethodr$   r#   r7   r=   r9   rB   rC   r<   rH   rK   rQ   rS   r   r   r   r   r      s*    




)	rW   Ú__version__Ú__date__Ú__all__Ú	itertoolsr   r/   Úobjectr   r   r   r   r   Ú<module>   s    
