ó
¡V]c           @   s}  d  Z  d Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 m
 Z
 m Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 „  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   
I/O classes provide a uniform API for low-level input and output.  Subclasses
exist for a variety of input/output mechanisms.
t   reStructuredTextiÿÿÿÿN(   t   TransformSpec(   t   locale_encodingt   ErrorStringt   ErrorOutputt
   InputErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s*   lib/python2.7/site-packages/docutils/io.pyR      s    t   OutputErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyR      s    c         C   sE   y# t  j |  j ƒ t  j | ƒ k SWn t t t f k
 r@ d SXd S(   sN  Test, whether the encoding of `stream` matches `encoding`.

    Returns

    :None:  if `encoding` or `stream.encoding` are not a valid encoding
            argument (e.g. ``None``) or `stream.encoding is missing.
    :True:  if the encoding argument resolves to the same value as `encoding`,
    :False: if the encodings differ.
    N(   t   codecst   lookupt   encodingt   LookupErrort   AttributeErrort	   TypeErrort   None(   t   streamR   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   check_encoding   s    
#t   Inputc           B   sŒ   e  Z d  Z d Z d Z d d d d d „ Z d „  Z d „  Z d „  Z	 e
 j d ƒ Z e j d f e j d	 f e j d
 f f Z d „  Z RS(   s1   
    Abstract base class for input wrappers.
    t   inputt   strictc         C   sF   | |  _  | |  _ | |  _ | |  _ | s9 |  j |  _ n  d  |  _ d  S(   N(   R   t   error_handlert   sourcet   source_patht   default_source_pathR   t   successful_encoding(   t   selfR   R   R   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   __init__1   s    					c         C   s   d |  j  |  j |  j f S(   Ns   %s: source=%r, source_path=%r(   t	   __class__R   R   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   __repr__E   s    c         C   s
   t  ‚ d  S(   N(   t   NotImplementedError(   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   readI   s    c         C   sT  |  j  r< |  j  j ƒ  d k r< t | t ƒ s< t d ƒ ‚ n  t | t ƒ rO | S|  j  rg |  j  g } nF |  j | ƒ } | rˆ | g } n% d d g } t r­ | j d t ƒ n  xb | D]Z } y2 t | | |  j ƒ } | |  _	 | j
 d d ƒ SWq´ t t f k
 r} | } q´ Xq´ Wt d d	 j g  | D] } t | ƒ ^ q%ƒ t | ƒ f ƒ ‚ d
 S(   s   
        Decode a string, `data`, heuristically.
        Raise UnicodeError if unsuccessful.

        The client application should call ``locale.setlocale`` at the
        beginning of processing::

            locale.setlocale(locale.LC_ALL, '')
        t   unicodes=   input encoding is "unicode" but input is not a unicode objects   utf-8s   latin-1i   u   ï»¿u    sE   Unable to decode input data.  Tried the following encodings: %s.
(%s)s   , N(   R   t   lowert
   isinstanceR    t   AssertionErrort   determine_encoding_from_dataR   t   insertR   R   t   replacet   UnicodeErrorR   t   joint   reprR   (   R   t   datat	   encodingst   data_encodingt   enct   decodedt   errt   error(    (    s*   lib/python2.7/site-packages/docutils/io.pyt   decodeL   s0    
		%s   coding[:=]\s*([-\w.]+)s   utf-8s	   utf-16-bes	   utf-16-lec         C   sz   x* |  j  D] \ } } | j | ƒ r
 | Sq
 WxF | j ƒ  d  D]4 } |  j j | ƒ } | r> | j d ƒ j d ƒ Sq> Wd S(   s—   
        Try to determine the encoding of `data` by looking *in* `data`.
        Check for a byte order mark (BOM) or an encoding declaration.
        i   i   t   asciiN(   t   byte_order_markst
   startswitht
   splitlinest   coding_slugt   searcht   groupR1   R   (   R   R*   t   start_bytesR   t   linet   match(    (    s*   lib/python2.7/site-packages/docutils/io.pyR$   †   s    N(   R   R   t   __doc__t   component_typeR   R   R   R   R   R1   t   ret   compileR6   R	   t   BOM_UTF8t   BOM_UTF16_BEt   BOM_UTF16_LER3   R$   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   '   s   				0t   Outputc           B   sJ   e  Z d  Z d Z d Z d d d d d „ Z d „  Z d „  Z d „  Z	 RS(   s2   
    Abstract base class for output wrappers.
    t   outputR   c         C   sC   | |  _  | p d |  _ | |  _ | |  _ | s? |  j |  _ n  d  S(   NR   (   R   R   t   destinationt   destination_patht   default_destination_path(   R   RE   RF   R   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   ¡   s    			c         C   s   d |  j  |  j |  j f S(   Ns'   %s: destination=%r, destination_path=%r(   R   RE   RF   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   ²   s    c         C   s
   t  ‚ d S(   s;   `data` is a Unicode string, to be encoded by `self.encode`.N(   R   (   R   R*   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   write¶   s    c         C   sj   |  j  r= |  j  j ƒ  d k r= t | t ƒ s9 t d ƒ ‚ | St | t ƒ sP | S| j |  j  |  j ƒ Sd  S(   NR    sF   the encoding given is "unicode" but the output is not a Unicode string(   R   R!   R"   R    R#   t   encodeR   (   R   R*   (    (    s*   lib/python2.7/site-packages/docutils/io.pyRI   º   s    	N(
   R   R   R<   R=   R   RG   R   R   RH   RI   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRC   —   s   		t	   FileInputc           B   sY   e  Z d  Z d
 d
 d
 d e e j d k r0 d n d d „ Z d „  Z d „  Z	 d	 „  Z
 RS(   s5   
    Input for single, simple file-like objects.
    R   i   i   t   rt   rUc   
      K   s‹  t  j |  | | | | ƒ | |  _ t ƒ  |  _ x= | D]5 } | d k rZ t j j d ƒ q5 t d | ƒ ‚ q5 W| d	 k r| rü t j
 d
 k r¬ i |  j d 6|  j d 6} n i  } y t | | |  |  _ Wqt k
 rø }	 t |	 j |	 j | ƒ ‚ qXqWt j |  _ nL t j
 d k rWt |  j |  j ƒ t k rWt d |  j |  j j f ƒ ‚ n  | s‡y |  j j |  _ Wq‡t k
 rƒq‡Xn  d	 S(   sÅ  
        :Parameters:
            - `source`: either a file-like object (which is read directly), or
              `None` (which implies `sys.stdin` if no `source_path` given).
            - `source_path`: a path to a file, which is opened and then read.
            - `encoding`: the expected text encoding of the input file.
            - `error_handler`: the encoding error handler to use.
            - `autoclose`: close automatically after read (except when
              `sys.stdin` is the source).
            - `mode`: how the file is to be opened (see standard function
              `open`). The default 'rU' provides universal newline support
              for text files on Python < 3.4.
        t   handle_io_errorss…   deprecation warning: io.FileInput() argument `handle_io_errors` is ignored since Docutils 0.10 (2012-12-16) and will soon be removed.s2   __init__() got an unexpected keyword argument '%s'i   i    R   t   errorssO   Encoding clash: encoding given is "%s" but source is opened with encoding "%s".N(   i   i    (   i   i    (   R   R   t	   autocloseR   t   _stderrt   syst   stderrRH   R   R   t   version_infoR   R   t   openR   t   IOErrorR   t   errnot   strerrort   stdinR   t   FalseR'   t   nameR   R   (
   R   R   R   R   R   RO   t   modet   kwargst   keyR0   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   Ì   s8    	c         C   s  zÜ yb |  j  t j k rU t j d k rU |  j  j j ƒ  } d j | j ƒ  ƒ d } n |  j  j ƒ  } Wns t t	 f k
 rÚ } |  j
 rÔ |  j rÔ t |  j d ƒ } | j ƒ  } | j ƒ  d j | j ƒ  ƒ d } qÛ ‚  n XWd |  j rõ |  j ƒ  n  X|  j | ƒ S(   sU   
        Read and decode a single file and return the data (Unicode string).
        i   i    s   
t   rbN(   i   i    (   R   RQ   RX   RS   t   bufferR   R(   R5   R'   R   R   R   RT   t   closeRO   R1   (   R   R*   R/   t   b_source(    (    s*   lib/python2.7/site-packages/docutils/io.pyR     s      !
	c         C   s   |  j  ƒ  j t ƒ S(   sK   
        Return lines of a single file as list of Unicode strings.
        (   R   R5   t   True(   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyt	   readlines!  s    c         C   s&   |  j  t j k	 r" |  j  j ƒ  n  d  S(   N(   R   RQ   RX   R`   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR`   '  s    N(   i   i   (   R   R   R<   R   Rb   RQ   RS   R   R   Rc   R`   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRJ   Ç   s   !7		t
   FileOutputc           B   sM   e  Z d  Z d Z d d d d e d d d „ Z d „  Z d „  Z d „  Z	 RS(   s6   
    Output for single, simple file-like objects.
    t   wR   c         C   sú   t  j |  | | | | ƒ t |  _ | |  _ | d k	 rC | |  _ n  t ƒ  |  _ | d k r| | rm t	 |  _ qÆ t
 j |  _ nJ | rÆ t |  j d ƒ rÆ | |  j j k rÆ |  j d |  j j | f IJn  | sö y |  j j |  _ Wqö t k
 rò qö Xn  d S(   sA  
        :Parameters:
            - `destination`: either a file-like object (which is written
              directly) or `None` (which implies `sys.stdout` if no
              `destination_path` given).
            - `destination_path`: a path to a file, which is opened and then
              written.
            - `encoding`: the text encoding of the output file.
            - `error_handler`: the encoding error handler to use.
            - `autoclose`: close automatically after write (except when
              `sys.stdout` or `sys.stderr` is the destination).
            - `handle_io_errors`: ignored, deprecated, will be removed.
            - `mode`: how the file is to be opened (see standard function
              `open`). The default is 'w', providing universal newline
              support for text files.
        R[   s?   Warning: Destination mode "%s" differs from specified mode "%s"N(   RC   R   Rb   t   openedRO   R   R[   R   RP   RY   RQ   t   stdoutRE   t   hasattrRZ   RF   R   (   R   RE   RF   R   R   RO   RM   R[   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   8  s(    
		
c         C   sž   t  j d k r; d |  j k r; i |  j d 6|  j d 6} n i  } y t |  j |  j |  |  _ Wn. t k
 r } t	 | j
 | j |  j ƒ ‚ n Xt |  _ d  S(   Ni   i    t   bR   RN   (   i   i    (   RQ   RS   R[   R   R   RT   RF   RE   RU   R   RV   RW   Rb   Rf   (   R   R\   R0   (    (    s*   lib/python2.7/site-packages/docutils/io.pyRT   c  s    c         C   s¾  |  j  s |  j ƒ  n  d |  j k r4 t j d
 k  sO t |  j |  j ƒ t k r  |  j	 | ƒ } t j d k r  t
 j d k r  | j d t t
 j d ƒ ƒ } q  n  z y |  j j | ƒ Wnå t k
 rg} t j d k rŸt | t ƒ rŸy |  j j j | ƒ Wqdt k
 r`t |  j |  j ƒ t k rWt d |  j p;d |  j j |  j f ƒ ‚ qa| ‚ qdXqŸn8 t t f k
 rž} t d |  j t | ƒ f ƒ ‚ n XWd	 |  j r¹|  j ƒ  n  X| S(   sÐ   Encode `data`, write it to a single file, and return it.

        With Python 3 or binary output mode, `data` is returned unchanged,
        except when specified encoding and output encoding differ.
        Ri   i   i    s   
R2   s;   Encoding of %s (%s) differs 
  from specified encoding (%s)RE   s:   Unable to encode output data. output-encoding is: %s.
(%s)N(   i   i    (   i   i    (   i   i    (   Rf   RT   R[   RQ   RS   R   RE   R   RY   RI   t   ost   linesepR&   t   bytesRH   R   R"   R_   R   t
   ValueErrorRF   R'   R   R   RO   R`   (   R   R*   t   eR/   (    (    s*   lib/python2.7/site-packages/docutils/io.pyRH   q  s8    	$ 	!	c         C   s8   |  j  t j t j f k r4 |  j  j ƒ  t |  _ n  d  S(   N(   RE   RQ   Rg   RR   R`   RY   Rf   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR`   ˜  s    N(
   R   R   R<   R[   R   Rb   R   RT   RH   R`   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRd   ,  s   	)		't   BinaryFileOutputc           B   s   e  Z d  Z d Z RS(   sL   
    A version of docutils.io.FileOutput which writes to a binary file.
    t   wb(   R   R   R<   R[   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRo   ž  s   t   StringInputc           B   s   e  Z d  Z d Z d „  Z RS(   s   
    Direct string input.
    s   <string>c         C   s   |  j  |  j ƒ S(   s$   Decode and return the source string.(   R1   R   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   ¯  s    (   R   R   R<   R   R   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRq   §  s   t   StringOutputc           B   s   e  Z d  Z d Z d „  Z RS(   s   
    Direct string output.
    s   <string>c         C   s   |  j  | ƒ |  _ |  j S(   s=   Encode `data`, store it in `self.destination`, and return it.(   RI   RE   (   R   R*   (    (    s*   lib/python2.7/site-packages/docutils/io.pyRH   ¼  s    (   R   R   R<   RG   RH   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRr   ´  s   t	   NullInputc           B   s   e  Z d  Z d Z d „  Z RS(   s)   
    Degenerate input: read nothing.
    s
   null inputc         C   s   d S(   s   Return a null string.u    (    (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   Ê  s    (   R   R   R<   R   R   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRs   Â  s   t
   NullOutputc           B   s   e  Z d  Z d Z d „  Z RS(   s+   
    Degenerate output: write nothing.
    s   null outputc         C   s   d S(   s6   Do nothing ([don't even] send data to the bit bucket).N(    (   R   R*   (    (    s*   lib/python2.7/site-packages/docutils/io.pyRH   ×  s    (   R   R   R<   RG   RH   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRt   Ï  s   t   DocTreeInputc           B   s   e  Z d  Z d Z d „  Z RS(   sm   
    Adapter for document tree input.

    The document tree must be passed in the ``source`` parameter.
    s   doctree inputc         C   s   |  j  S(   s   Return the document tree.(   R   (   R   (    (    s*   lib/python2.7/site-packages/docutils/io.pyR   æ  s    (   R   R   R<   R   R   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyRu   Ü  s   (   R<   t   __docformat__RQ   Rj   R>   R	   t   docutilsR   t   docutils.utils.error_reportingR   R   R   RU   R   R   R   R   RC   RJ   Rd   Ro   Rq   Rr   Rs   Rt   Ru   (    (    (    s*   lib/python2.7/site-packages/docutils/io.pyt   <module>   s(   	p0er	