
lR\$                 @   s   d  Z  d d d  Z d d d  Z d g Z y d d l Z Wn e k
 r_ d d l Z Yn Xd d l Z d d l Z d d l	 Z	 d	 d
 l
 m Z d	 d l
 m Z Gd d   d e  Z d S)zDBF header definition.

TODO:
  - handle encoding of the character fields
    (encoding information stored in the DBF header)

z$Revision: 1.6 $      z$Date: 2010/09/16 05:06:39 $   	DbfHeader    N   )fields)utilsc            	   @   s  e  Z d  Z d Z d+ Z d
 d d d d d
 d d d  Z d d   Z e e  Z d d   Z e e  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
 S),r   a|  Dbf header definition.

    For more information about dbf header format visit
    `http://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_STRUCT`

    Examples:
        Create an empty dbf header and add some field definitions:
            dbfh = DbfHeader()
            dbfh.addField(("name", "C", 10))
            dbfh.addField(("date", "D"))
            dbfh.addField(DbfNumericFieldDef("price", 5, 2))
        Create a dbf header with field definitions:
            dbfh = DbfHeader([
                ("name", "C", 10),
                ("date", "D"),
                DbfNumericFieldDef("price", 5, 2),
            ])

    	signaturer   
lastUpdaterecordLengthrecordCountheaderLengthchanged_ignore_errorsNr      Fc             C   s|   | |  _  | d k r! g  |  _ n t |  |  _ t j |  |  _ | |  _ | |  _ | |  _ | |  _	 t
 |  j  |  _ d S)a  Initialize instance.

        Arguments:
            fields:
                a list of field definitions;
            recordLength:
                size of the records;
            headerLength:
                size of the header;
            recordCount:
                number of records stored in DBF;
            signature:
                version number (aka signature). using 0x03 as a default meaning
                "File without DBT". for more information about this field visit
                ``http://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_NOTE_1_TARGET``
            lastUpdate:
                date of the DBF's update. this could be a string ('yymmdd' or
                'yyyymmdd'), timestamp (int or float), datetime/date value,
                a sequence (assuming (yyyy, mm, dd, ...)) or an object having
                callable ``ticks`` field.
            ignoreErrors:
                error processing mode for DBF fields (boolean)

        N)r	   r   listr   ZgetDater
   r   r   r   ignoreErrorsboolr   )selfr   r   r   r   r	   r
   r    r   Q/var/www/dbchiro/venv/lib/python3.4/site-packages/tablib/packages/dbfpy/header.py__init__=   s    					zDbfHeader.__init__c             C   s   |  j  t j t |    S)z.Return header instance from the string object.)
fromStream	cStringIOStringIOstr)clsstringr   r   r   
fromStringg   s    zDbfHeader.fromStringc       
      C   s=  | j  d  | j d  } t j d | d d   \ } } } t | d  } | d k  rj | d 7} n
 | d	 7} |  d
 | | | t | d  | t | d  t | d  f  } d } | j d  } xk | d d k r8| | j d  7} t j | d  j | |  }	 | j |	  |	 j	 } | j d  } q W| S)z%Return header object from the stream.r       z<I2H      r   P   i  il  Nr   r   
   r   )r#   r$   )
seekreadstructunpackordr   	lookupForr   	_addFieldend)
r   stream_dataZ_cntZ_hdrLenZ_recLen_year_obj_pos_fldr   r   r   r   m   s$    %
&	zDbfHeader.fromStreamc             C   s
   |  j  j S)N)r
   year)r   r   r   r   <lambda>   s    zDbfHeader.<lambda>c             C   s
   |  j  j S)N)r
   month)r   r   r   r   r5      s    c             C   s
   |  j  j S)N)r
   day)r   r   r   r   r5      s    c             C   s4   t  |  |  _ } x |  j D] } | | _ q Wd S)z1Update `ignoreErrors` flag on self and all fieldsN)r   r   r   r   )r   value_fieldr   r   r   r      s    zDbfHeader.ignoreErrorsc             C   s   |  j  S)N)r   )r   r   r   r   r5      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   sO   d |  j  |  j |  j |  j |  j f } | d j d d   |  j D  7} | S)NzVersion (signature): 0x%02x
        Last update: %s
      Header length: %d
      Record length: %d
       Record count: %d
 FieldName Type Len Dec
r$   c             S   s    g  |  ] } d  | j     q S)z%10s %4s %3s %3s)Z	fieldInfo).0r3   r   r   r   
<listcomp>   s   	 z&DbfHeader.__repr__.<locals>.<listcomp>)r	   r
   r   r   r   joinr   )r   Z_rvr   r   r   __repr__   s
    	zDbfHeader.__repr__c             G   s   g  } d } x | D] } t  | t j  r4 | } nP t |  d d d  \ } } } }	 t j |  }
 |
 | | |	 d |  j } | | j 7} | j |  q W|  j | 7_ | S)ah  Internal variant of the `addField` method.

        This method doesn't set `self.changed` field to True.

        Return value is a length of the appended records.
        Note: this method doesn't modify ``recordLength`` and
        ``headerLength`` fields. Use `addField` instead of this
        method if you don't exactly know what you're doing.

        r   Nr    r   )N)NNNN)
isinstancer   ZDbfFieldDeftupler+   r   lengthappend)r   defsZ_defsZ_recordLengthZ_defr1   _nameZ_type_lenZ_dec_clsr   r   r   r,      s    	&zDbfHeader._addFieldc             G   sd   |  j  } |  j  |  j |   7_  | s9 |  j  d 7_  n  d d t |  j  d |  _ d |  _ d S)a  Add field definition to the header.

        Examples:
            dbfh.addField(
                ("name", "C", 20),
                dbf.DbfCharacterFieldDef("surname", 20),
                dbf.DbfDateFieldDef("birthdate"),
                ("member", "L"),
            )
            dbfh.addField(("price", "N", 5, 2))
            dbfh.addField(dbf.DbfNumericFieldDef("origprice", 5, 2))

        r   r   TN)r   r,   lenr   r   r   )r   rC   Z_oldLenr   r   r   addField   s    	zDbfHeader.addFieldc             C   sf   | j  d  | j |  j    | j d j d d   |  j D   | j t d   d |  _ d S)z&Encode and write header to the stream.r    c             S   s   g  |  ] } | j     q Sr   )toString)r;   r3   r   r   r   r<      s   	 z#DbfHeader.write.<locals>.<listcomp>   FN)r&   writerJ   r=   r   chrr   )r   r.   r   r   r   rL      s
    &zDbfHeader.writec          	   C   s?   t  j d |  j |  j d |  j |  j |  j |  j |  j  d S)z4Returned 32 chars length string with encoded header.z<4BI2Hil      Z                    )	r(   packr	   r4   r6   r7   r   r   r   )r   r   r   r   rJ      s    	
zDbfHeader.toStringc             C   s   t  j j   |  _ d S)z9Update ``self.lastUpdate`` field with current date value.N)datetimedatetodayr
   )r   r   r   r   setCurrentDate  s    zDbfHeader.setCurrentDatec             C   s`   t  | t  rQ | j   } x> |  j D] } | j | k r% | Sq% Wt |   n |  j | Sd S)z9Return a field definition by numeric index or name stringN)r?   
basestringupperr   nameKeyError)r   itemrD   r9   r   r   r   __getitem__  s    zDbfHeader.__getitem__)z	signaturezfieldsz
lastUpdatezrecordLengthzrecordCountzheaderLengthzchangedz_ignore_errors)__name__
__module____qualname____doc__	__slots__r   r   classmethodr   propertyr4   r6   r7   r   r>   r,   rH   rL   rJ   rT   rZ   r   r   r   r   r   #   s0    	)		!rb   )r^   __version____date____all__r   ImportErroriorQ   r(   timerI   r   r   objectr   r   r   r   r   <module>   s   
	