ó
æ²Vc           @   sS   d  Z  d d l Z d d l Z d d l Z d e f d „  ƒ  YZ e d k rO n  d S(   sf   
Created on Sep 7, 2011

@author: vieglais

Import a Excel worksheet as tables in an SQLite database.
iÿÿÿÿNt   Xls2Sqlc           B   s_   e  Z d  Z d
 d „ Z d „  Z d d „ Z d d d „ Z d „  Z d d „ Z	 d
 d	 „ Z
 RS(   sc   Utility class that will import worksheets from an Excel workbook as
  tables in an SQL database.
  c         C   sX   t  j d ƒ |  _ | |  _ d g |  _ g  |  _ |  j d k rT t j d ƒ |  _ n  d S(   s¤   
    Initialize the class.
    
    :param db: An open DB-API 2.0 database connection. If None then an 
               in-memory SQLite3 database will be made.
    R    t   nones   :memory:N(	   t   loggingt	   getLoggert   logt   dbt   RESERVEDt   columnst   Nonet   sqlite3t   connect(   t   selfR   (    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   __init__   s    		c         C   sð   xé | j  ƒ  D]Û } d | } g  } d } xe | | d D]U } | d |  j k r8 | j d | d | d f ƒ |  j j | ƒ | d 7} q8 q8 W| d j | ƒ 7} | d	 7} t j d
 | ƒ |  j j ƒ  } | j	 | ƒ |  j j
 ƒ  q Wd S(   s˜  Create a new table in the database using the column names contained
    in meta.
    
    meta is structured like::

      meta = {name: {'start_row':
                     'columns': [{'name': column name, 
                                  'id': zero offset from left, 
                                  'type': sqlite type}, 
                                 ...],
                    },
            }
    s   CREATE TABLE %s (i    R   t   names   %s %st   typei   s   , s   );s   Create table SQL: %sN(   t   keysR   t   appendR   t   joinR   t   infoR   t   cursort   executet   commit(   R   t   metat   tnamet   sqlt   fieldst   idxt   colt   cn(    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   createTables$   s    

i    c         C   sF   i  } x9 | j  ƒ  D]+ } | j | ƒ } |  j | | ƒ | | <q W| S(   s®   Load a meta structure from the workbook.
    
    :param workbook: An openpyxl workbook object.
    
    :param meta_row: Which row to read metadata (column names) from.
    (   t   get_sheet_namest   get_sheet_by_namet   getSheetMeta(   R   t   workbookt   meta_rowR   t   snamet   sheet(    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   getBookMetaC   s
    i   c         C   s   d S(   s¡   Place holder for code that will guess the type of the column. This is
    not really necessary for SQLite databases, as it figures stuff out on
    the fly.
    t   text(    (   R   t   rowR   (    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   guessColumnTypeQ   s    c         C   sF   | d k r d S| d k r& t | ƒ S| d k r< t | ƒ St | ƒ S(   s‹   Try and coerce the supplied value to the indicated type.
    
    :param val: The value to coerce
    
    :param typ: The target type
    u    R&   t   realN(   R   t   unicodet   float(   R   t   valt   typ(    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt
   coerceTypeY   s    

c         C   s£   i | d d 6g  d 6} | j  | } xw t d t | ƒ ƒ D]` } i  } t | | j ƒ j ƒ  | d <| | d <|  j d d d | ƒ | d	 <| d j | ƒ q; W| S(
   s³   Attempt to read column names for sheet from the specified row.
    
    :param worksheet: Worksheet being examined
    
    :param meta_row: Row of worksheet to be looked at.
    i   t	   start_rowR   i    R   t   idR'   R   R   (   t   rowst   xranget   lenR*   t   valuet   lowerR(   R   (   R   t	   worksheetR"   R   t   headert   it   cmeta(    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyR    i   s    

c         C   s¶  t  | t ƒ r' t j j j | ƒ } n  | d k rE |  j | ƒ } n  |  j | ƒ |  j	 j
 ƒ  } xK| j ƒ  D]=} | j | ƒ } t j d | ƒ xt | | d t | j ƒ ƒ D]ò } | j | } g  } xo | | d D]_ }	 |	 d |  j k rÝ |  j | |	 d j |	 d ƒ }
 |
 d k r,d }
 n  | j |
 ƒ qÝ qÝ Wd g t | ƒ } d	 | } | d
 j | ƒ 7} | d 7} |  j j | ƒ | j | | ƒ |  j	 j ƒ  qµ Wqn W|  j	 S(   s   Load the supplied workbook into the database that was provided with
    the constructor.
    
    :param workbook: Path to an Excel file or an openpyxl workbook object
    
    :param meta: Optional metadata structure to use instead of auto generated.
    s   Loading from Sheet: %sR/   R   R   R0   R   t    t   ?s   INSERT INTO %s VALUES (s   , s   );N(   t
   isinstancet
   basestringt   openpyxlt   readert   excelt   load_workbookR   R%   R   R   R   R   R   R   R   R2   R3   R1   R   R.   R4   R   R   R   t   debugR   R   (   R   R!   R   R   R#   R$   R8   R'   t   rdataR   t   vt   qsR   (    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   load|   s4    '!	

N(   t   __name__t
   __module__t   __doc__R   R   R   R%   R(   R.   R    RF   (    (    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyR       s   		t   __main__(   RI   R   R	   R>   t   objectR    RG   (    (    (    sz   /var/lib/jenkins/jobs/API Documentation - trunk/workspace/api-documentation/tools/docutils/rst_directives/utils/xls2sql.pyt   <module>   s   ’