
    f                      J    d dl Z ddlmZ ddd Zddd Zd	gZ	 	  G d
 d	      Zy)    N   )utilsz$Revision: 1.7 $   z$Date: 2007/02/11 09:05:49 $   	DbfRecordc                       e Zd ZdZdZddZ ed       Zd Z e	e      Zd Z
 e	e
      Z
ddZ e	e      Zd	 Zd
 ZddZd Zd Zd Zd Zd Zd Zd Zy)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	fieldDataNc                     || _         || _        || _        |4|j                  j                  D cg c]  }|j
                   c}| _        yt        |      | _        yc c}w )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.

        N)r
   r   r   headerfieldsdefaultValuer   list)selfr
   r   r   data_fds         CD:\switchATM\venv\Lib\site-packages\tablib/packages/dbfpy/record.py__init__zDbfRecord.__init__;   sQ    " 
<:=**:K:KL3c..LDN!$ZDN Ms   Ac                     | j                   j                  j                  | j                  | j                   j                  j                  z  z   S N)r
   r   headerLengthr   recordLengthr   s    r   <lambda>zDbfRecord.<lambda>V   s4    TXX__%A%A

TXX__%A%AA&B     c                     |j                   j                  |j                  j                  ||j                  j                  z  z          |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   s      r   rawFromStreamzDbfRecord.rawFromStreamY   sO      	



//%#**:Q:Q2QQRzzszz6677r   c                 H    | j                  || j                  ||      |      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#   s      r   
fromStreamzDbfRecord.fromStreamn   s$     ~~c3#4#4S%#@%HHr   c                      | |||d   dk(  |j                   j                  D cg c]  }|j                  |       c}      S c c}w )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   *)r   r   decodeFromRecord)r$   r
   stringr   r   s        r   r'   zDbfRecord.fromString   sH     3vayC/<?JJ<M<MNSC((0NP 	PNs   Ac           	      N   dt        d | j                  j                  D              z  }g }| j                  j                  D ]Q  }| |   }|t        j                  u r|j                  ||ddfz         2|j                  |||t        |      fz         S dj                  |      S )Nz%%%ds: %%s (%%s)c              3   2   K   | ]  }t        |        y wr   )len).0_flds     r   	<genexpr>z%DbfRecord.__repr__.<locals>.<genexpr>   s     ,W4SY,Ws   Nonezvalue cannot be decoded
)maxr
   
fieldNamesr   INVALID_VALUEappendtypejoin)r   	_template_rvr1   _vals        r   __repr__zDbfRecord.__repr__   s    &,W488CVCV,W)WW	HH'' 	AD:Du***

9f6O'PPQ

9dDJ'??@	A yy~r   c                    | j                  d       | j                  j                  j                  | j                         | j                  j                  j                  t        | j                         t        j                                      | j                  t        | j                        k(  r&| j                  j                  j                  d       yy)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   s    r   _writezDbfRecord._write   s     	E"T]]+eDMMO!779; 	< ::TXX& HHOO!!'* 'r   c                 >   | j                   |st        d      y| j                   dk  rt        d| j                   z        |rZ| j                   | j                  j                  j                  k  r,t        d| j                  j                  j                  z        yy)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   recordCount)r   allowUndefined
checkRanges      r   rA   zDbfRecord._validateIndex   s     ::! !566 "ZZ!^;djjHIIDJJ$((//*E*EEC!XX__889 : : FZr   c                     | j                          | j                  6t        | j                        | _        | j                  j	                  |        y| | j                  | j                  <   y)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)rA   r   r/   r
   r8   r   s    r   storezDbfRecord.store   sK     	::TXXDJHHOOD!#'DHHTZZ r   c                     d| _         y)zMark method as deleted.TN)r   r   s    r   deletezDbfRecord.delete   s	    r   c           
          dj                  d| j                     gt        | j                  j                  j
                  | j                        D cg c]  \  }}|j                  |       c}}z         S c c}}w )z#Return string packed record values. z *)r:   r   zipr
   r   r   r   encodeValue)r   _def_dats      r   rE   zDbfRecord.toString   si    wwT\\*+69$((//:P:PRVR`R`6a/
&2tT4D!/
 
  	 /
s   A7c                      | 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   s    r   asListzDbfRecord.asList   s     ~~a  r   c                     t        t        | j                  j                  | j                        D cg c]  }| c}      S c c}w )zReturn a dictionary of fields.

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

        )dictrT   r
   r6   r   )r   _is     r   asDictzDbfRecord.asDict   s2     #dhh&9&94>>"JKBRKLLKs   	Ac                     t        |t              r| j                  |   S | j                  | j                  j	                  |         S )z*Return value by field name or field index.
isinstanceintr   r
   indexOfFieldName)r   keys     r   __getitem__zDbfRecord.__getitem__   s9    c3>>#&&~~dhh77<==r   c                     t        |t              r| j                  |   S || j                  | j                  j	                  |      <   y)z=Set field value by integer index of the field or string name.Nr_   )r   rc   values      r   __setitem__zDbfRecord.__setitem__   s8    c3>>#&&9>txx0056r   )NFNr   )TF)__name__
__module____qualname____doc__	__slots__r   propertyrB   r%   classmethodr(   r'   r>   rH   rA   rO   rQ   rE   rY   r]   rd   rg    r   r   r   r      s    : 7I(6  B CH8&  .MI Z(JP$ Z(J	+.:$(!M>?r   )rF   rS   r   __version____date____all__r   ro   r   r   <module>rs      s@    
  B')!B/-l? l?r   