U
    yagî   ã                   @   sD   d dl Z ddlmZ ddd… Zddd… Zd	gZG d
d	„ d	ƒZdS )é    Né   )Úutilsz$Revision: 1.7 $é   éþÿÿÿz$Date: 2007/02/11 09:05:49 $é   Ú	DbfRecordc                   @   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 manually,
    use `dbf.Dbf.newRecord` instead.

    Class implements mapping/sequence interface, so
    fields could be accessed via their names or indexes
    (names is a preferred 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kr0dd„ |jjD ƒ| _n
t|ƒ| _dS )a$  Instance initialization.

        Arguments:
            dbf:
                A `Dbf.Dbf` instance this record belongs 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©Ú.0Z_fdr   r   úP/var/www/html/myvenv/lib/python3.8/site-packages/tablib/packages/dbfpy/record.pyÚ
<listcomp>Q   s     z&DbfRecord.__init__.<locals>.<listcomp>)r   r	   r
   ÚheaderÚfieldsr   Úlist)Úselfr   r	   r
   Údatar   r   r   Ú__init__;   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>V   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   ÚrawFromStreamY   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   Ú
fromStreamn   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   )Z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 ]D}| | }|tjkrR| ||ddf ¡ q&| |||t|ƒf ¡ q&d |¡S )Nz%%%ds: %%s (%%s)c                 s   s   | ]}t |ƒV  qd S r   )Úlen)r   Ú_fldr   r   r   Ú	<genexpr>–   s     z%DbfRecord.__repr__.<locals>.<genexpr>ÚNonezvalue cannot be decodedÚ
)Úmaxr   Ú
fieldNamesr   ZINVALID_VALUEÚappendÚtypeÚjoin)r   Ú	_templateZ_rvr)   Z_valr   r   r   Ú__repr__•   s    
zDbfRecord.__repr__c                 C   sX   |   d¡ | jj | j¡ | jj t|  ¡ t 	¡ ƒ¡ | j
t| jƒkrT| jj d¡ dS )zúWrite data to the dbf stream.

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

        Fó   N)Ú_validateIndexr   r   r   ÚpositionÚwriteÚbytesÚtoStringÚsysÚgetfilesystemencodingr	   r(   r   r   r   r   Ú_write¢   s    

ÿzDbfRecord._writeTc                 C   s\   | j dkr|sXtdƒ‚n@| j dk r2td| j  ƒ‚n&|rX| j | jjjkrXtd| jjj ƒ‚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   ZrecordCount)r   ZallowUndefinedZ
checkRanger   r   r   r5   ¹   s    


ÿzDbfRecord._validateIndexc                 C   s<   |   ¡  | jdkr,t| jƒ| _| j | ¡ n| | 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)r5   r	   r(   r   r/   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   )ZencodeValue)r   Z_defZ_datr   r   r   r   ß   s    z&DbfRecord.toString.<locals>.<listcomp>)r1   r
   Úzipr   r   r   r   r   r   r   r   r9   Ý   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   Z_ir   r   r   r   õ   s     z$DbfRecord.asDict.<locals>.<listcomp>)ÚdictrA   r   r.   r   r   r   r   r   ÚasDictí   s    zDbfRecord.asDictc                 C   s&   t |tƒr| j| S | j| j |¡ S )z*Return value by field name or field index.©Ú
isinstanceÚintr   r   ZindexOfFieldName)r   Úkeyr   r   r   Ú__getitem__÷   s    

zDbfRecord.__getitem__c                 C   s*   t |tƒr| j| S || j| j |¡< dS )z=Set field value by integer index of the field or string name.NrE   )r   rH   Úvaluer   r   r   Ú__setitem__ÿ   s    

zDbfRecord.__setitem__)NFN)N)TF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Úpropertyr6   r"   Úclassmethodr$   r#   r3   r<   r5   r>   r?   r9   rB   rD   rI   rK   r   r   r   r   r      s(   




)r:   r@   r   Ú__version__Ú__date__Ú__all__r   r   r   r   r   Ú<module>   s
   