ó
È*Sc           @   sî   d  Z  d Z d d l Z d d l Z d d l Z d d l m Z m Z m	 Z	 m
 Z
 d d l m Z d d l m Z d d l m Z d d l m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s    
Directives for table elements.
t   reStructuredTextiÿÿÿÿN(   t   iot   nodest   statemachinet   utils(   t
   SafeString(   t   SystemMessagePropagation(   t	   Directive(   t
   directivest   Tablec           B   sg   e  Z d  Z d Z e Z i e j d 6e j d 6Z	 e Z
 d „  Z d „  Z d „  Z d „  Z d „  Z RS(	   s#   
    Generic table base class.
    i   t   classt   namec         C   sb   |  j  rL |  j  d } |  j j | |  j ƒ \ } } t j | d | Œ } n d  } g  } | | f S(   Ni    t    (   t	   argumentst   statet   inline_textt   linenoR   t   titlet   None(   t   selft
   title_textt
   text_nodest   messagesR   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt
   make_title#   s    	c         C   s   |  j  j |  j d ƒ } g  } d } d |  j k ru |  j |  j d j d ƒ |  j ƒ  | ƒ \ } } | j | ƒ n  | | f S(   Ni   i    t   headers   
(   t   state_machinet
   get_sourceR   t   optionst   parse_csv_data_into_rowst   splitt   HeaderDialectt   extend(   R   t   sourcet
   table_headt   max_header_colst   rows(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   process_header_option.   s    c         C   s×  t  | ƒ | k  rj |  j j j d | t  | ƒ |  j f t j |  j |  j ƒ d |  j ƒ} t	 | ƒ ‚ n  t  | ƒ | k o‡ d k n rá |  j j j d t  | ƒ |  j f t j |  j |  j ƒ d |  j ƒ} t	 | ƒ ‚ n  xï | D]ç } t  | ƒ | k  rX|  j j j d | t  | ƒ |  j f t j |  j |  j ƒ d |  j ƒ} t	 | ƒ ‚ n  t  | ƒ | k oud k n rè |  j j j d t  | ƒ |  j f t j |  j |  j ƒ d |  j ƒ} t	 | ƒ ‚ qè qè Wd  S(   NsP   %s header row(s) specified but only %s row(s) of data supplied ("%s" directive).t   linei    se   Insufficient data supplied (%s row(s)); no data remaining for table body, required by "%s" directive.sU   %s stub column(s) specified but only %s columns(s) of data supplied ("%s" directive).si   Insufficient data supplied (%s columns(s)); no data remaining for table body, required by "%s" directive.(
   t   lenR   t   reportert   errorR   R   t   literal_blockt
   block_textR   R   (   R   R#   t   header_rowst   stub_columnsR(   t   row(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   check_table_dimensions9   s2    ""c         C   sÝ   d |  j  k r€ |  j  d } t | ƒ | k rÙ |  j j j d |  j | f t j |  j |  j ƒ d |  j	 ƒ} t
 | ƒ ‚ qÙ nY | rš d | g | } n? |  j j j d t j |  j |  j ƒ d |  j	 ƒ} t
 | ƒ ‚ | S(   Nt   widthss=   "%s" widths do not match the number of columns in table (%s).R%   id   s#   No table data detected in CSV file.(   R   R&   R   R'   R(   R   R   R)   R*   R   R   (   R   t   max_colst
   col_widthsR(   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   get_column_widthsX   s    	c         C   se   x^ | D]V } xM | D]E } t  | ƒ | k  r | j d d d g  f g | t  | ƒ ƒ q q Wq Wd  S(   Ni    (   R&   R   (   R   t   columnst   partst   partR-   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt"   extend_short_rows_with_empty_cellsj   s    (   t   __name__t
   __module__t   __doc__t   optional_argumentst   Truet   final_argument_whitespaceR   t   class_optiont	   unchangedt   option_spect   has_contentR   R$   R.   R2   R6   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR	      s   				t   RSTTablec           B   s   e  Z d  „  Z RS(   c         C   sH  |  j  sJ |  j j j d |  j t j |  j |  j ƒ d |  j ƒ} | g S|  j	 ƒ  \ } } t j
 ƒ  } |  j j |  j  |  j | ƒ t | ƒ d k s­ t | d t j ƒ rî |  j j j d |  j t j |  j |  j ƒ d |  j ƒ} | g S| d } | d c |  j j d g  ƒ 7<|  j | ƒ | r=| j d | ƒ n  | g | S(   Ns:   Content block expected for the "%s" directive; none found.R%   i   i    sO   Error parsing content block for the "%s" directive: exactly one table expected.t   classesR
   (   t   contentR   R'   t   warningR   R   R)   R*   R   R   t   ElementR   t   nested_parset   content_offsetR&   t
   isinstancet   tableR(   R   t   gett   add_namet   insert(   R   RD   R   R   t   nodeR(   t
   table_node(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   runs   s*    	)
(   R7   R8   RO   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRA   q   s   t   CSVTablec           B   s4  e  Z i e j d  6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j	 d 6e j d 6e j
 d	 6e j d
 6e j d 6e j d 6Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d „  Z d „  Z d „  Z e j d k  rÿ d „  Z d „  Z n d „  Z d „  Z e e ƒ Z e e ƒ Z d „  Z RS(   s   header-rowss   stub-columnsR   R/   t   filet   urlt   encodingR
   R   t   delimt	   keepspacet   quotet   escapet   DocutilsDialectc           B   sD   e  Z d  Z d Z d Z e Z e Z e Z d Z	 e
 j Z d „  Z RS(   s&   CSV dialect for `csv_table` directive.t   ,t   "s   
c         C   s¤   d | k r% t  j | d ƒ |  _ n  d | k r= t |  _ n  d | k rb t  j | d ƒ |  _ n  d | k r t |  _ t  j | d ƒ |  _ n  t j	 j
 |  ƒ d  S(   NRT   RU   RV   RW   (   RP   t   encode_for_csvt	   delimitert   Falset   skipinitialspacet	   quotechart   doublequotet
   escapechart   csvt   Dialectt   __init__(   R   R   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRd   «   s    	(   R7   R8   R9   R\   R_   R;   R`   R^   t   strictt   lineterminatorRb   t   QUOTE_MINIMALt   quotingRd   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRX   Ÿ   s   	R   c           B   sA   e  Z d  Z d Z d Z d Z e Z e Z	 e Z
 d Z e j Z RS(   s0   CSV dialect to use for the "header" option data.RY   RZ   s   \s   
(   R7   R8   R9   R\   R_   Ra   R]   R`   R;   R^   Re   Rf   Rb   Rg   Rh   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR   ¸   s   c         C   s   d  S(   N(    (   R   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   check_requirementsÅ   s    c         C   s‘  yi|  j  j j j ru d |  j k s4 d |  j k ru |  j j j d |  j t	 j
 |  j |  j ƒ d |  j ƒ} | g S|  j ƒ  |  j ƒ  \ } } |  j ƒ  \ } } |  j ƒ  \ } } |  j | |  j |  j ƒ | ƒ \ } }	 t |	 | ƒ }	 |  j j d d ƒ }
 |  j j d d ƒ } |  j | |
 | ƒ | j | |
  ƒ | |
 } |  j |	 ƒ } |  j |	 | | f ƒ Wn« t k
 r‰} | j d g St j k
 r} t | ƒ } t j d k  rÏd	 | k rÏ| d
 7} n  |  j j j  d |  j | f t	 j
 |  j |  j ƒ d |  j ƒ} | g SX| | | f } |  j  j! | |  j" | ƒ } | d c |  j j d g  ƒ 7<|  j# | ƒ | r†| j$ d | ƒ n  | g | S(   NRQ   RR   s9   File and URL access deactivated; ignoring "%s" directive.R%   s   header-rowsi    s   stub-columnsi   s   1-character strings1   
with Python 2.x this must be an ASCII character.s)   Error with CSV data in "%s" directive:
%sRB   R
   (   i   (%   R   t   documentt   settingst   file_insertion_enabledR   R   R'   RD   R   R   R)   R*   R   Ri   R   t   get_csv_dataR$   R   RX   t   maxRJ   R.   R   R2   R6   R   t   argsRb   t   Errort   strt   syst   version_infoR(   t   build_tableRG   RK   RL   (   R   RD   R   R   t   csv_dataR    R!   R"   R#   R0   R+   R,   t
   table_bodyR1   t   detailt   messageR(   RI   RN   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRO   È   sV    
!
		c      	   C   s˜  |  j  j d |  j j j j ƒ } |  j j j j } |  j rÁ d |  j  k sZ d |  j  k r£ |  j j	 j
 d |  j t j |  j |  j ƒ d |  j ƒ} t | ƒ ‚ n  |  j j d ƒ } |  j } nÍd |  j  k rAd |  j  k r(|  j j	 j
 d |  j t j |  j |  j ƒ d |  j ƒ} t | ƒ ‚ n  t j j t j j |  j j j ƒ ƒ } t j j t j j | |  j  d ƒ ƒ } t j d | ƒ } yM |  j j j j j | ƒ t j d | d | d	 | ƒ } | j  ƒ  j! ƒ  } WqŽt" k
 r=} |  j j	 j# d
 |  j t$ | ƒ f t j |  j |  j ƒ d |  j ƒ} t | ƒ ‚ qŽXnMd |  j  k rHd d l% }	 |  j  d } y |	 j& | ƒ j  ƒ  }
 Wn~ |	 j' t" t( t) f k
 r} |  j j	 j# d |  j |  j  d t$ | ƒ f t j |  j |  j ƒ d |  j ƒ} t | ƒ ‚ n Xt j* d |
 d | d | d	 |  j j j j ƒ } | j  ƒ  j! ƒ  } nF |  j j	 j+ d |  j t j |  j |  j ƒ d |  j ƒ} t | ƒ ‚ | | f S(   sr   
        Get CSV data from the directive content, from an external
        file, or from a URL reference.
        RS   RQ   RR   sF   "%s" directive may not both specify an external file and have content.R%   i    sX   The "file" and "url" options may not be simultaneously specified for the "%s" directive.t   source_patht   error_handleru&   Problems with "%s" directive path:
%s.iÿÿÿÿNs*   Problems with "%s" directive URL "%s":
%s.R    s3   The "%s" directive requires content; none supplied.(,   R   RJ   R   Rj   Rk   t   input_encodingt   input_encoding_error_handlerRC   R   R'   R(   R   R   R)   R*   R   R   R    t   ost   patht   dirnamet   abspatht   current_sourcet   normpatht   joinR   t   relative_pathR   t   record_dependenciest   addR   t	   FileInputt   readt
   splitlinest   IOErrort   severeR   t   urllib2t   urlopent   URLErrort   OSErrort
   ValueErrort   StringInputRD   (   R   RS   Rz   R(   R    Ru   t
   source_dirt   csv_fileR‹   RŒ   t   csv_text(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRm   õ   sx    					i   c         C   s   |  j  d ƒ S(   Ns   utf-8(   t   decode(   t   s(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   decode_from_csvA  s    c         C   s   |  j  d ƒ S(   Ns   utf-8(   t   encode(   R–   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR[   C  s    c         C   s   |  S(   N(    (   R–   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR—   F  s    c         C   s   |  S(   N(    (   R–   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR[   H  s    c      	   C   sØ   t  j g  | D] } |  j | d ƒ ^ q d | ƒ} g  } d } xŠ | D]‚ } g  }	 xQ | D]I }
 |  j |
 ƒ } d d d t j | j ƒ  d | ƒf } |	 j | ƒ q[ W| j |	 ƒ t | t	 | ƒ ƒ } qH W| | f S(   Ns   
t   dialecti    R    (
   Rb   t   readerR[   R—   R   t
   StringListR‰   t   appendRn   R&   (   R   Ru   R™   R    R%   t
   csv_readerR#   R0   R-   t   row_datat   cellt	   cell_textt	   cell_data(    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR   M  s    	#	(   i   (   R7   R8   R   t   nonnegative_intR>   t   positive_int_listR~   t   uriRS   R=   t$   single_char_or_whitespace_or_unicodet   flagt   single_char_or_unicodeR?   Rb   Rc   RX   R   Ri   RO   Rm   Rr   Rs   R—   R[   t   staticmethodR   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRP   ‹   s4   










		-	J			t	   ListTablec           B   sa   e  Z d  Z i e j d 6e j d 6e j d 6e j d 6e j d 6Z d „  Z	 d „  Z
 d „  Z RS(	   s¼   
    Implement tables whose data is encoded as a uniform two-level bullet list.
    For further ideas, see
    http://docutils.sf.net/docs/dev/rst/alternatives.html#list-driven-tables
    s   header-rowss   stub-columnsR/   R
   R   c         C   sš  |  j  sJ |  j j j d |  j t j |  j |  j ƒ d |  j ƒ} | g S|  j	 ƒ  \ } } t j
 ƒ  } |  j j |  j  |  j | ƒ y |  j | ƒ \ } } g  | d D]& } g  | d D] } | j ^ q¸ ^ q§ }	 |  j j d d ƒ }
 |  j j d d ƒ } |  j |	 |
 | ƒ Wn t k
 r1} | j d g SX|  j |	 | |
 | ƒ } | d c |  j j d g  ƒ 7<|  j | ƒ | r| j d | ƒ n  | g | S(   Ns.   The "%s" directive is empty; content required.R%   i    s   header-rowss   stub-columnsRB   R
   (   RC   R   R'   R(   R   R   R)   R*   R   R   RE   R   RF   RG   t   check_list_contentt   childrenR   RJ   R.   R   Ro   t   build_table_from_listRK   RL   (   R   R(   R   R   RM   t   num_colsR1   t   row_listt   itemt
   table_dataR+   R,   Rw   RN   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRO   o  s2    	
4c         C   sÊ  t  | ƒ d k s) t | d t j ƒ rr |  j j j d |  j t j |  j	 |  j	 ƒ d |  j
 ƒ} t | ƒ ‚ n  | d } x2t t  | ƒ ƒ D]} | | } t  | ƒ d k sÈ t | d t j ƒ r|  j j j d |  j | d f t j |  j	 |  j	 ƒ d |  j
 ƒ} t | ƒ ‚ q | rt  | d ƒ | k r­|  j j j d |  j | d t  | d ƒ | f t j |  j	 |  j	 ƒ d |  j
 ƒ} t | ƒ ‚ q­q t  | d ƒ } q W|  j | ƒ } | | f S(   Ni   i    sU   Error parsing content block for the "%s" directive: exactly one bullet list expected.R%   s‹   Error parsing content block for the "%s" directive: two-level bullet list expected, but row %s does not contain a second-level bullet list.s¥   Error parsing content block for the "%s" directive: uniform two-level bullet list expected, but row %s does not contain the same number of items as row 1 (%s vs %s).(   R&   RH   R   t   bullet_listR   R'   R(   R   R)   R*   R   R   t   rangeR2   (   R   RM   R(   t	   list_nodet
   item_indexR¯   R­   R1   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyRª   Š  s6    )

)!c         C   s7  t  j ƒ  } t  j d t | ƒ ƒ } | | 7} xJ | D]B } t  j d | ƒ } | rm d | j d <| d 8} n  | | 7} q5 Wg  }	 xX | D]P }
 t  j ƒ  } x. |
 D]& } t  j ƒ  } | | 7} | | 7} q¡ W|	 j | ƒ qˆ W| rt  j	 ƒ  } | j
 |	 |  ƒ | | 7} n  t  j ƒ  } | j
 |	 | ƒ | | 7} | S(   Nt   colst   colwidthi   t   stub(   R   RI   t   tgroupR&   t   colspect
   attributesR-   t   entryRœ   t   theadR   t   tbody(   R   R°   R1   R+   R,   RI   R¸   t	   col_widthR¹   R#   R-   t   row_nodeRŸ   R»   R¼   R½   (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR¬   ±  s2    


(   R7   R8   R9   R   R¢   R£   R=   R>   R?   RO   Rª   R¬   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyR©   a  s   


		'(   R9   t   __docformat__Rr   t   os.pathR}   Rb   t   docutilsR   R   R   R   t   docutils.utils.error_reportingR   t   docutils.utilsR   t   docutils.parsers.rstR   R   R	   RA   RP   R©   (    (    (    sJ   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/tables.pyt   <module>   s   "ZÖ