
lR\                 @   s  d  Z  d d d%  Z d d d&  Z d g Z d d l Td d l Td d l Td d l TGd	 d
   d
 e  Z	 Gd d   d e  Z
 e d k re
   Z e j d d d  e j d d d d  e j d d d  e j d  e d  e d d d Z e e e   e e  Z d e d <d e d <d' e d <e j   e e  Z d e d <d e d <d( e d <e j   e d"  e e e   xW e e e   D]C Z e e Z x) e j D] Z e d# e e e f  qWe   qWe j   n  d$ S))a!  .DBF creation helpers.

Note: this is a legacy interface.  New code should use Dbf class
    for table creation (see examples in dbf.py)

TODO:
  - handle Memo fields.
  - check length of the fields accoring to the
    `http://www.clicketyclick.dk/databases/xbase/format/data_types.html`

z$Revision: 1.4 $      z$Date: 2006/07/04 08:18:18 $   dbf_new   )*c               @   s   e  Z d  Z d Z d Z i e d f d 6e d f d	 6e d
 f d 6e d f d 6e	 d f d 6Z
 d d d d  Z d d   Z d d   Z d S)_FieldDefinitionaj  Field definition.

    This is a simple structure, which contains ``name``, ``type``,
    ``len``, ``dec`` and ``cls`` fields.

    Objects also implement get/setitem magic functions, so fields
    could be accessed via sequence iterface, where 'name' has
    index 0, 'type' index 1, 'len' index 2, 'dec' index 3 and
    'cls' could be located at index 4.

    nametypelendecclsNCNr   L   D   T    c             C   st   |  j  | \ } } | d  k rC | d  k r: t d   n  | } n  | |  _ | |  _ | |  _ | |  _ | |  _ d  S)NzField length must be defined)	FLD_TYPES
ValueErrorr   r	   r
   r   r   )selfr   r	   r
   r   _cls_len r   R/var/www/dbchiro/venv/lib/python3.4/site-packages/tablib/packages/dbfpy3/dbfnew.py__init__?   s    					z_FieldDefinition.__init__c             C   s   |  j  |  j |  j |  j  S)z:Return `DbfFieldDef` instance from the current definition.)r   r   r
   r   )r   r   r   r   getDbfFieldK   s    z_FieldDefinition.getDbfFieldc             C   s   |  j    } | j |  d S)zCreate a `DbfFieldDef` instance and append it to the dbf header.

        Arguments:
            dbfh: `DbfHeader` instance.

        N)r   ZaddField)r   ZdbfhZ_dbffr   r   r   appendToHeaderO   s    z_FieldDefinition.appendToHeader)znameztypezlenzdeczcls)__name__
__module____qualname____doc__	__slots__ZDbfCharacterFieldDefZDbfNumericFieldDefZDbfLogicalFieldDefZDbfDateFieldDefZDbfDateTimeFieldDefr   r   r   r   r   r   r   r   r   !   s   r   c               @   sI   e  Z d  Z d Z d Z e Z d d   Z d d d  Z d d	   Z	 d
 S)r   a3  New .DBF creation helper.

    Example Usage:

        dbfn = dbf_new()
        dbfn.add_field("name",'C',80)
        dbfn.add_field("price",'N',10,2)
        dbfn.add_field("date",'D',8)
        dbfn.write("tst.dbf")

    Note:
        This module cannot handle Memo-fields,
        they are special.

    fieldsc             C   s   g  |  _  d  S)N)r$   )r   r   r   r   r   o   s    zdbf_new.__init__r   c             C   s&   |  j  j |  j | | | |   d S)a  Add field definition.

        Arguments:
            name:
                field name (str object). field name must not
                contain ASCII NULs and it's length shouldn't
                exceed 10 characters.
            typ:
                type of the field. this must be a single character
                from the "CNLMDT" set meaning character, numeric,
                logical, memo, date and date/time respectively.
            len:
                length of the field. this argument is used only for
                the character and numeric fields. all other fields
                have fixed length.
                FIXME: use None as a default for this argument?
            dec:
                decimal precision. used only for the numric fields.

        N)r$   appendFieldDefinitionClass)r   r   typr
   r   r   r   r   	add_fieldr   s    zdbf_new.add_fieldc             C   s^   t    } | j   x |  j D] } | j |  q Wt | d  } | j |  | j   d S)z/Create empty .DBF file using current structure.wbN)Z	DbfHeaderZsetCurrentDater$   r   openwriteclose)r   filenameZ_dbfhZ_fldDefZ
_dbfStreamr   r   r   r+      s    	
zdbf_new.writeN)zfields)
r   r    r!   r"   r#   r   r&   r   r(   r+   r   r   r   r   r   Z   s   __main__r   r   P   Zpricer   
   dater   r   ztst.dbfz*** created tst.dbf: ***ZreadOnlyr   Z	somethingg      %@     zfoo and bari/       z(*** inserted 2 records into tst.dbf: ***z%s:	 %sNr6   )r2   r   r3   )r4   r   r5   )r"   __version____date____all__Zdbfr$   headerrecordobjectr   r   r   Zdbfnr(   r+   printZDbfZdbftreprZ	DbfRecordZrecstoreranger
   Zi1Z
fieldNamesZfldNamer,   r   r   r   r   <module>   sH   
	



9;	










