ó ü¤Sc@s»dZdZddlZddlZddlZddlZddlmZm Z m Z m Z ddl m Z ddlmZddlmZddlmZd ejfd „ƒYZdS( s¸Implements the SQLTable docutils directive. License :: This work was created by participants in the DataONE project, and is jointly copyrighted by participating institutions in DataONE. For more information on DataONE, see our web site at http://dataone.org. Copyright 2011 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SQLTable is an extension to docutils reStructuredText processor which adds the ability to pull table content from an SQL data source, a CSV file, or an Excel spread sheet. SQLManager_ is a convenient tool for working with SQLite databases. Project: DataONE Author: Dave Vieglais Example for use in Sphinx, add to conf.py after ``import sys, os``:: #Import the SQLTable directive RST extension from sqltable import SQLTable from docutils.parsers.rst import directives directives.register_directive('sql-table', SQLTable) .. _SQLManager: http://code.google.com/p/sqlite-manager/ treStructuredTextiÿÿÿÿN(tiotnodest statemachinetutils(tSystemMessagePropagation(t Directive(t directives(ttablestSQLTablecBs eZdZdZdZeZeZiej d6ej d6ej d6ej d6ej d6ejd6ejd 6ejd 6Zd „Zd „Zd „Zd„ZRS(s iitheadertwidthstencodings stub-columnstclasstdrivertsourcetsqlcCsdS(N((tself((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pytcheck_requirementsKsc Csry‹|jƒ|jjddƒ}|jƒ\}}|jƒ\}}|jƒ}|j|ƒ}|j|d|ƒ|j|||fƒWnwt k r«}|j dgSt j k r}|j jjd|j|ftj|j|jƒd|jƒ} | gSX|||f} |jj| |j|ƒ} | dc|jjdgƒ7<|rg| jd|ƒn| g|S(Ns stub-columnsis)Error with SQL data in "%s" directive: %stlinetclassesR (Rtoptionstgett make_titlet get_sql_datatprocess_header_optiontget_column_widthstcheck_table_dimensionst"extend_short_rows_with_empty_cellsRtargstcsvtErrort state_machinetreporterterrortnameRt literal_blockt block_texttlinenotstatet build_tabletcontent_offsettinsert( Rt stub_columnsttitletmessagest table_bodytmax_colst table_headt col_widthstdetailR"ttablet table_node((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pytrunOs0    cCsjg}|jjddƒ}xE|jdƒD]4}|jdddtj|jƒjƒƒgƒq+W|gS(sReturns table_head R tt,i(RRtsplittappendRt StringListtstript splitlines(Rtrestcolnamestcol((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pyRjs 2cBs°|jjddƒ}|dkrwddlm}ddl}|jdƒ}||ƒ}|j|jjddƒƒnHd }d }d |jjdd ƒdUd |jjd|ƒ}|dU|jƒ}e|jjdƒƒ} |j | ƒ} g} d} |j ƒ} x| dk r›g}xH| D]@}e |ƒ}ddde j|jƒƒf}|j|ƒq&W| j|ƒe| e|ƒƒ} |j ƒ} q W|jƒ| | fS(sReturns rows, max_cols Rtxlsxiÿÿÿÿ(tXls2SqlNs:memory:Rs data.xlsxs"database="data.xlsx",driver="xslx"s database="csv-data",driver="csv"simport %s as DBDRVtSnakeSQLsdbconn = DBDRV.connect(%s)Ri(RRt utils.xls2sqlRAtsqlite3tconnecttloadtcursortstrtexecutetfetchonetNonetunicodeRR:R<R9tmaxtlentclose(RRRARDtdbconntloadert default_srctcnstrRGtSQLR=trowsR/trowtrow_datatcellt cell_textt cell_data((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pyRts<         (t__name__t __module__t__doc__trequired_argumentstoptional_argumentstTruetfinal_argument_whitespacet has_contentRt unchangedtpositive_int_listR tnonnegative_intt class_optiontunchanged_requiredt option_specRR5RR(((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pyR 9s"           (R]t __docformat__tsystos.pathtosRtloggingtdocutilsRRRRtdocutils.utilsRtdocutils.parsers.rstRRtdocutils.parsers.rst.directivesRtTableR (((sn/var/lib/jenkins/jobs/DataONE-Operations-Manual/workspace/operations/tools/docutils/rst_directives/sqltable.pyt*s    "