U
    >Ke                      @   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   @/tmp/pip-unpacked-wheel-5xrpv5dc/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 )zWrite 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 )zValid ``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 )zStore 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 )zReturn 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 )zReturn 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
   