
lR\2$                 @   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	 Gd d   d e
  Z d d   Z d d   Z e d k r d d l Z e e j  d k r e j d p d Z e e  e e  n  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               @   sN  e  Z d  Z d Z d- Z e j Z e j	 Z
 e Z d d 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  rR | |  _ | r3 t | d  |  _ qp t | d t |   |  _ n t | d d  |  _ | |  _ | r |  j   |  _ n |  j 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r+brbr	    FN)r   zrb)
isinstancestrr	   openr
   boolgetattrHeaderClassr   
fromStreamignoreErrorsr   r   )selffZreadOnlynewr    r   O/var/www/dbchiro/venv/lib/python3.4/site-packages/tablib/packages/dbfpy3/dbf.py__init__^   s    			zDbf.__init__c             C   s
   |  j  j S)N)r
   closed)r   r   r   r   <lambda>   s    zDbf.<lambda>c             C   s
   |  j  j S)N)r   recordCount)r   r   r   r   r       s    c             C   s   d d   |  j  j D S)Nc             S   s   g  |  ] } | j   q Sr   )r	   ).0Z_fldr   r   r   
<listcomp>   s   	 z Dbf.<lambda>.<locals>.<listcomp>)r   fields)r   r   r   r   r       s    c             C   s
   |  j  j S)N)r   r$   )r   r   r   r   r       s    c             C   s   |  j  p |  j j S)N)r   r   changed)r   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   s   |  j  S)N)r   )r   r   r   r   r       s    doczError processing mode for DBF field value conversion

        if set, failing field value conversion will return
        ``INVALID_VALUE`` instead of raising conversion error.

        c             C   sf   t  | t  s t d   n  | d k  rA | t |   d 7} n  | t |   k rb t d   n  | 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    |  j j   d  S)N)flushr
   close)r   r   r   r   r0      s    
z	Dbf.closec             C   sF   |  j  rB |  j j   |  j j |  j  |  j j   d |  _ n  d S)z$Flush data to the associated stream.FN)r%   r   ZsetCurrentDatewriter
   r/   r   )r   r   r   r   r/      s
    	z	Dbf.flushc             C   s,   d d   |  j  j D } | j | j    S)zIndex of field named ``name``.c             S   s   g  |  ] } | j   q Sr   )r	   )r"   r   r   r   r   r#      s   	 z(Dbf.indexOfFieldName.<locals>.<listcomp>)r   r$   r-   upper)r   r	   namesr   r   r   indexOfFieldName   s    zDbf.indexOfFieldNamec             C   s   |  j  |   S)z.Return new record, which belong to this table.)RecordClass)r   r   r   r   	newRecord   s    zDbf.newRecordc             C   sA   |  j  j | _ | j   |  j  j d 7_ d |  _ d |  _ d S)z"Append ``record`` to the database.r   TFN)r   r!   r-   _writer   r   )r   r   r   r   r   append   s
    
	z
Dbf.appendc             G   s,   |  j  r |  j j |   n t d   d S)z_Add field definitions.

        For more information see `header.DbfHeader.addField`.

        z9At least one record was added, structure can't be changedN)r   r   addFieldr*   )r   Zdefsr   r   r   r9      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   s   |  j  S)zReturn number of records.)r!   )r   r   r   r   __len__   s    zDbf.__len__c             C   s   |  j  j |  |  j |   S)zReturn `DbfRecord` instance.)r5   r   r.   )r   r-   r   r   r   __getitem__   s    zDbf.__getitem__c             C   s2   |  j  |  | _ | j   d |  _ d |  _ d S)z)Write `DbfRecord` instance to the stream.TFN)r.   r-   r7   r   r   )r   r-   r   r   r   r   __setitem__   s    
	zDbf.__setitem__N)znamezheaderzstreamz_changedz_newz_ignore_errors)__name__
__module____qualname____doc__	__slots__r   Z	DbfHeaderr   r   Z	DbfRecordr5   r   r   propertyr   r!   Z
fieldNamesZ	fieldDefsr%   r   r.   r0   r/   r4   r6   r8   r9   r;   r<   r=   r>   r   r   r   r   r   F   s:    		1		c             C   sE   t  |  d  } x% | D] } t   t t |   q W| j   d  S)NT)r   printr:   r0   )filename_dbf_recr   r   r   	demo_read  s
    rI   c          	   C   s   t  |  d d } | j d# d$ d% d&  xX d/ D]P \ } } } } | j   } | | d <| | d <| | d <| | d
 <| j   q/ Wt t |   | j   d  S)0Nr   TNAMEC   SURNAME   INITIALS
   	BIRTHDATEDJohnMillerYC  r   r   AndyLarkinAL        BillClinthr           BobbMcNail  r      )zNAMErK   rL   )rM   rK   rN   )rO   rK   rP   )rQ   rR   rV   r   r   rS   rT   rU   rf   rZ   r[   r\   rW   rX   rY   rh   r_   r`   ra   r]   r^   r   rj   rd   r   re   rb   rc   r   rl   )rg   ri   rk   rm   )r   r9   r6   storerE   r:   r0   )rF   rG   Z_nZ_sZ_iZ_brH   r   r   r   demo_create  s&       



ro   __main__r(   Nz
county.dbfrq   )rB   __version____date__
__author____all__r   r   r   utilsr   objectr   rI   ro   r?   sysr+   argv_namer   r   r   r   <module>'   s   	(
