
Sc           @   s  d  Z  d Z d d l Z d d l 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 m Z d d l m Z d d l m Z m Z d d l m Z m Z m Z d d	 l m Z 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 e f d     YZ! d e f d     YZ" d e f d     YZ# d e f d     YZ$ d e f d     YZ% d e f d     YZ& d S(    s   Miscellaneous directives.t   reStructuredTextiN(   t   iot   nodest   statemachinet   utils(   t
   SafeStringt   ErrorString(   t   locale_encoding(   t	   Directivet   convert_directive_function(   t
   directivest   rolest   states(   t	   CodeBlockt   NumberLines(   t   set_classes(   t   misct   Includec           B   s   e  Z d  Z d Z d Z e Z i e j d 6e j	 d 6e j
 d 6e d 6e d 6e d 6e j d	 6e j d
 6e j	 d 6e j d 6e j	 d 6Z e j j e j e j  d  Z d   Z RS(   s]  
    Include content read from a separate source file.

    Content may be parsed by the parser, or included as a literal
    block.  The encoding of the included file can be specified.  Only
    a part of the given file argument may be included by specifying
    start and end line or text to match before and/or after the text
    to be used.
    i   i    t   literalt   codet   encodings	   tab-widths
   start-lines   end-lines   start-afters
   end-befores   number-linest   classt   namet   includec      
   C   s  |  j  j j j s+ |  j d |  j   n  |  j j j |  j	 |  j j
 d  } t j j t j j |   } t j |  j d  } | j d  r | j d  r t j j |  j | d d ! } n  t j j t j j | |   } t j d |  } t j |  } |  j j d |  j  j j j  } |  j  j j j } |  j j d |  j  j j j  } y; |  j  j j j  j! |  t" j# d	 | d | d
 |  } Wni t$ k
 r} |  j% d |  j t& |  f   n5 t' k
 r} |  j% d |  j t( |  f   n X|  j j d d  }	 |  j j d d  }
 yG |	 sA|
 d k	 rf| j)   } d j | |	 |
 ! } n | j*   } Wn5 t+ k
 r} |  j% d |  j t( |  f   n X|  j j d d  } | r| j, |  } | d k  r|  j% d |  j   n  | | t- |  } n  |  j j d d  } | rm| j, |  } | d k  r`|  j% d |  j   n  | |  } n  t. j/ | | d t0 } d |  j k r | d k r| j1 |  } n | } t j2 | d | d |  j j d g   } d | _3 |  j4 |  d |  j k ry t5 |  j d pd  }	 Wn  t6 k
 rF|  j7 d   n X|	 t- |  }
 | j d  rs| d  } n  t8 g  | f g |	 |
  } xh | D]G \ } } | r| t j9 | | d | 7} q| t j: | |  7} qWn | t j: | |  7} | g Sd |  j k rn| |  j d <t; |  j |  j j< d  g |  j | |  j	 |  j= |  j> |  j  |  j 	 } | j?   S|  j j@ | |  g  S(   s8   Include a file as part of the content of this reST file.s   "%s" directive disabled.i   i    t   <t   >iR   s	   tab-widtht   source_patht   error_handleruV   Problems with "%s" directive path:
Cannot encode input file path "%s" (wrong locale?).u&   Problems with "%s" directive path:
%s.s
   start-lines   end-linet    u   Problem with "%s" directive:
%ss   start-aftersD   Problem with "start-after" option of "%s" directive:
Text not found.s
   end-beforesC   Problem with "end-before" option of "%s" directive:
Text not found.t   convert_whitespaceR   t   sourcet   classesR   s   number-liness+   :number-lines: with non-integer start values   
R   N(A   t   statet   documentt   settingst   file_insertion_enabledt   warningR   t   state_machinet   input_linesR   t   linenot   input_offsett   ost   patht   dirnamet   abspathR
   t	   argumentst
   startswitht   endswitht   joint   standard_include_patht   normpathR   t   relative_patht   NoneR   t   reprunicodet   optionst   gett   input_encodingt   input_encoding_error_handlert	   tab_widtht   record_dependenciest   addR   t	   FileInputt   UnicodeEncodeErrort   severeR   t   IOErrorR   t	   readlinest   readt   UnicodeErrort   findt   lenR   t   string2linest   Truet
   expandtabst   literal_blockt   linet   add_namet   intt
   ValueErrort   errorR   t   inlinet   TextR   t   popt   content_offsett
   block_textt   runt   insert_input(   t   selfR   t
   source_dirR*   R   t	   e_handlerR:   t   include_fileRN   t	   startlinet   endlinet   linest   rawtextt
   after_textt   after_indext   before_textt   before_indext   include_linest   textRI   t   tokensR   t   valuet	   codeblock(    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT   6   s    "!										
(   t   __name__t
   __module__t   __doc__t   required_argumentst   optional_argumentsRG   t   final_argument_whitespaceR
   t   flagt	   unchangedR   RL   t   unchanged_requiredt   class_optiont   option_specR)   R*   R0   t   docutilst   _datadirR   t   __file__R1   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR      s$   






	t   Rawc           B   sS   e  Z d  Z d Z d Z e Z i e j d 6e j	 d 6e j
 d 6Z e Z d   Z RS(   s   
    Pass through content unchanged

    Content is included in output based on type argument

    Content may be included inline (content section of directive) or
    imported from a file or url.
    i   i    t   filet   urlR   c      	   C   s  |  j  j j j sD |  j  j j j r] d |  j k sD d |  j k r] |  j d |  j   n  i d j |  j	 d j
   j    d 6} |  j j d |  j  j j j  } |  j  j j j } |  j rd |  j k s d |  j k r |  j d |  j   n  d	 j |  j  } nrd |  j k rnd |  j k rE|  j d
 |  j   n  t j j t j j |  j  j j   } t j j t j j | |  j d   } t j d  |  } y; t j d | d | d |  } |  j  j j j j |  Wn5 t k
 r} |  j d |  j t  |  f   n Xy | j!   } Wn5 t" k
 r`} |  j d |  j t  |  f   n X| | d <nd |  j k rv|  j d }	 d d  l# }
 y |
 j$ |	  j!   } WnK |
 j% t t& f k
 r} |  j d |  j |  j d t  |  f   n Xt j' d | d |	 d | d |  } y | j!   } Wn5 t" k
 rh} |  j d |  j t  |  f   n X|	 | d <n
 |  j(   t) j* d | |  } |  j+ j, |  j-  \ | _. | _/ | g S(   NRv   Rw   s   "%s" directive disabled.t    i    t   formatR   sF   "%s" directive may not both specify an external file and have content.s   
sX   The "file" and "url" options may not be simultaneously specified for the "%s" directive.R   R   u&   Problems with "%s" directive path:
%s.u   Problem with "%s" directive:
%sR   iu*   Problems with "%s" directive URL "%s":
%s.R   (0   R    R!   R"   t   raw_enabledR#   R6   R$   R   R0   R-   t   lowert   splitR7   R8   R9   t   contentRN   R)   R*   R+   R,   t   current_sourceR2   R   R3   R4   R   R=   R;   R<   R@   R?   R   RB   RC   t   urllib2t   urlopent   URLErrort   OSErrort   StringInputt   assert_has_contentR   t   rawR%   t   get_source_and_lineR'   R   RJ   (   RV   t
   attributesR   RX   Rc   RW   R*   t   raw_fileRN   R   R   t   raw_textt   raw_node(    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT      sx    )							'		
!(   Rg   Rh   Ri   Rj   Rk   RG   Rl   R
   R*   t   uriR   Rq   t   has_contentRT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRu      s   	
t   Replacec           B   s   e  Z e Z d    Z RS(   c         C   s  t  |  j t j  s. |  j d |  j   n  |  j   d j |  j  } t	 j
 |  } |  j j |  j |  j |  d  } g  } x | D]z } | r t  | t	 j  r | } q t  | t	 j  r g  | d <| j |  q |  j j j d |  j d |  j g Sq W| r| | j S| S(   NsV   Invalid context: the "%s" directive can only be used within a substitution definition.s   
t   backrefss=   Error in "%s" directive: may contain a single paragraph only.RJ   (   t
   isinstanceR    R   t   SubstitutionDefRN   R   R   R0   R}   R   t   Elementt   nested_parseRR   R4   t	   paragrapht   system_messaget   appendR%   t   reporterR'   t   children(   RV   Rc   t   elementt   nodet   messagest   elem(    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT      s.    
	
(   Rg   Rh   RG   R   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR      s   t   Unicodec           B   s\   e  Z d  Z d Z d Z e Z i e j d 6e j d 6e j d 6Z	 e
 j d  Z d   Z RS(   s{  
    Convert Unicode character codes (numbers) to characters.  Codes may be
    decimal numbers, hexadecimal numbers (prefixed by ``0x``, ``x``, ``\x``,
    ``U+``, ``u``, or ``\u``; e.g. ``U+262E``), or XML-style numeric character
    entities (e.g. ``&#x262E;``).  Text following ".." is a comment and is
    ignored.  Spaces are ignored, and any other text remains as-is.
    i   i    t   trimt   ltrimt   rtrims   ( |\n|^)\.\. c         C   sF  t  |  j t j  s. |  j d |  j   n  |  j j } d |  j k rf d | j	 d <d | j	 d <n  d |  j k r d | j	 d <n  d |  j k r d | j	 d <n  |  j
 j |  j d  d j   } t j   } xi | D]a } y t j |  } Wn2 t k
 r'} |  j d | t |  f   n X| t j |  7} q W| j S(   NsV   Invalid context: the "%s" directive can only be used within a substitution definition.R   i   R   R   i    u   Invalid character code: %s
%s(   R   R    R   R   RN   R   R%   R   R6   R   t   comment_patternR|   R-   R   R   R
   t   unicode_codeRM   R   RP   R   (   RV   t   substitution_definitiont   codesR   R   t   decodedRN   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT   /  s,    #	(   Rg   Rh   Ri   Rj   Rk   RG   Rl   R
   Rm   Rq   t   ret   compileR   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR     s   
t   Classc           B   s/   e  Z d  Z d Z d Z e Z e Z d   Z RS(   s   
    Set a "class" attribute on the directive content or the next element.
    When applied to the next element, a "pending" element is inserted, and a
    transform does the work later.
    i   i    c         C   s  y t  j |  j d  } Wn4 t k
 rP |  j d |  j |  j d f   n Xg  } |  j r t j   } |  j	 j
 |  j |  j |  x | D] } | d j |  q W| j | j  nL t j t j i | d 6|  j d 6|  j  } |  j j j |  | j |  | S(   Ni    s7   Invalid class attribute value for "%s" directive: "%s".R   R   t	   directive(   R
   Rp   R-   RM   RN   R   R}   R   R   R    R   RR   t   extendR   t   pendingR   t   ClassAttributeRS   R%   R!   t   note_pendingR   (   RV   t   class_valuet	   node_listt	   containerR   R   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT   U  s*    	(	   Rg   Rh   Ri   Rj   Rk   RG   Rl   R   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR   H  s   t   Rolec           B   s7   e  Z e Z e j d  e j j f d  Z	 d   Z
 RS(   s   (%s)\s*(\(\s*(%s)\s*\)\s*)?$i   c         C   s  |  j  |  j k s |  j r5 |  j d |  j   n  |  j d } |  j j |  } | sy |  j d |  j | f   n  | j d  } | j d  } g  } | r!t j	 | |  j
 j |  j |  j j  \ } } | d k r*|  j j j d | t j |  j |  j  d |  j } | | g Sn	 t j } t | d  sSt d	 |  j | f   yD t |  } |  j j |  j d |  j  | d
 i  \ }	 }
 } } Wn^ t j k
 r} |  j
 j j d |  j | f t j |  j |  j  d |  j } | | g SXd |
 k ry t j |  |
 d <Wqt k
 r~} |  j
 j j d |  j t |  f t j |  j |  j  d |  j } | | g SXn  t j | | |
 |  } t j | |  | S(   s?   Dynamically create and register a custom interpreted text role.s4   "%s" directive requires arguments on the first line.i    s4   "%s" directive arguments not valid role names: "%s".i   i   s#   Unknown interpreted text role "%s".RJ   R-   s[   Supplemental directive arguments for "%s" directive not supported (specified by "%r" role).t   option_presetss   Error in "%s" directive:
%s.R   u(   Invalid argument for "%s" directive:
%s.N(   RR   R'   R}   RN   R   t   argument_patternt   matcht   groupR   t   roleR%   t   languageR    R   R4   R   RI   RS   t   generic_custom_rolet   hasattrt   AssertionErrorR	   t   parse_directive_blockR   t   MarkupErrorR
   Rp   RM   R   t
   CustomRolet   register_local_role(   RV   t   argsR   t   new_role_namet   base_role_nameR   t	   base_roleRN   t   converted_roleR-   R6   R}   RR   t   detailR   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT   u  s`    				(   Rg   Rh   RG   R   R   R   R   t   Inlinert
   simplenameR   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR   n  s   	t   DefaultRolec           B   s#   e  Z d  Z d Z e Z d   Z RS(   s&   Set the default interpreted text role.i   c         C   s   |  j  s) d t j k r% t j d =n  g  S|  j  d } t j | |  j j |  j |  j j  \ } } | d  k r |  j j j
 d | t j |  j |  j  d |  j } | | g S| t j d <| S(   NR   i    s#   Unknown interpreted text role "%s".RJ   (   R-   R   t   _rolesR   R%   R   R'   R    R   R4   RN   R   RI   RS   (   RV   t	   role_nameR   R   RN   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT     s    	(   Rg   Rh   Ri   Rk   t   FalseRl   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR     s   t   Titlec           B   s#   e  Z d  Z d Z e Z d   Z RS(   i   i    c         C   s   |  j  d |  j j d <g  S(   Ni    t   title(   R-   R%   R!   (   RV   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT     s    (   Rg   Rh   Rj   Rk   RG   Rl   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR     s   t   Datec           B   s   e  Z e Z d    Z RS(   c         C   s'  t  |  j t j  s. |  j d |  j   n  d j |  j  pC d } t j	 d
 k  r y | j
 t pg d  } Wq t k
 r |  j d t   q Xn  t j |  } t j	 d k  ry | j t p d  } Wqt k
 r| j t p d d  } |  j d	 | t f   qXn  t j |  g S(   NsV   Invalid context: the "%s" directive can only be used within a substitution definition.s   
s   %Y-%m-%di   i    s   utf-8u;   Cannot encode date format string with locale encoding "%s".t   replaceu-   Error decoding "%s"with locale encoding "%s".(   i   i    (   i   i    (   R   R    R   R   RN   R   R0   R}   t   syst   version_infot   encodeR   R>   R$   t   timet   strftimet   decodet   UnicodeDecodeErrorR   RP   (   RV   t
   format_strRc   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT     s(    		(   Rg   Rh   RG   R   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR     s   t   TestDirectivec           B   s9   e  Z d  Z d Z e Z i e j d 6Z e Z	 d   Z
 RS(   s3   This directive is useful only for testing purposes.i   t   optionc         C   s   |  j  ra d j |  j   } |  j j j d |  j |  j |  j f t j	 | |  d |  j
 } n4 |  j j j d |  j |  j |  j f d |  j
 } | g S(   Ns   
sB   Directive processed. Type="%s", arguments=%r, options=%r, content:RJ   sG   Directive processed. Type="%s", arguments=%r, options=%r, content: None(   R}   R0   R%   R   t   infoR   R-   R6   R   RI   R'   (   RV   Rc   R   (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyRT     s    	!(   Rg   Rh   Ri   Rk   RG   Rl   R
   Ro   Rq   R   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyR     s   ('   Ri   t   __docformat__R   t   os.pathR)   R   R   Rr   R   R   R   R   t   docutils.utils.error_reportingR   R   R   t   docutils.parsers.rstR   R	   R
   R   R   t$   docutils.parsers.rst.directives.bodyR   R   t   docutils.parsers.rst.rolesR   t   docutils.transformsR   R   Ru   R   R   R   R   R   R   R   R   (    (    (    sH   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/directives/misc.pyt   <module>   s0   "Z ,&=