ó
¹‹Qc           @   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	 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   b(   t   locale_encodingt   ErrorStringt   ErrorOutputt
   InputErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR      s    t   OutputErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s/   /usr/lib/python2.7/dist-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/   /usr/lib/python2.7/dist-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 e 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/   /usr/lib/python2.7/dist-packages/docutils/io.pyt   __init__2   s    					c         C   s   d |  j  |  j |  j f S(   Ns   %s: source=%r, source_path=%r(   t	   __class__R   R   (   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyt   __repr__F   s    c         C   s
   t  ‚ d  S(   N(   t   NotImplementedError(   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyt   readJ   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/   /usr/lib/python2.7/dist-packages/docutils/io.pyt   decodeM   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   groupR2   R   (   R   R+   t   start_bytesR   t   linet   match(    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR%   ‡   s    N(   R   R   t   __doc__t   component_typeR   R   R   R   R    R2   t   ret   compileR   R7   R
   t   BOM_UTF8t   BOM_UTF16_BEt   BOM_UTF16_LER4   R%   (    (    (    s/   /usr/lib/python2.7/dist-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   RF   RG   R   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR   ¢   s    			c         C   s   d |  j  |  j |  j f S(   Ns'   %s: destination=%r, destination_path=%r(   R   RF   RG   (   R   (    (    s/   /usr/lib/python2.7/dist-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/   /usr/lib/python2.7/dist-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/   /usr/lib/python2.7/dist-packages/docutils/io.pyRJ   »   s    	N(
   R   R   R=   R>   R   RH   R   R   RI   RJ   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRD   ˜   s   		t	   FileInputc           B   sG   e  Z d  Z d d d d e d d d „ Z d „  Z d „  Z d „  Z RS(   s5   
    Input for single, simple file-like objects.
    R   t   rUc   
      C   sK  t  j |  | | | | ƒ | |  _ t ƒ  |  _ | d k rË | r¼ t j d k rl i |  j d 6|  j	 d 6} n i  } y t
 | | |  |  _ WqÈ t k
 r¸ }	 t |	 j |	 j | ƒ ‚ qÈ Xqt j |  _ nL t j d k rt |  j |  j ƒ t k rt d |  j |  j j f ƒ ‚ n  | sGy |  j j |  _ WqGt k
 rCqG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).
            - `handle_io_errors`: ignored, deprecated, will be removed.
            - `mode`: how the file is to be opened (see standard function
              `open`). The default 'rU' provides universal newline support
              for text files.
        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   _stderrR   t   syst   version_infoR   R   t   openR   t   IOErrorR   t   errnot   strerrort   stdinR   t   FalseR(   t   nameR   R   (
   R   R   R   R   R   RN   t   handle_io_errorst   modet   kwargsR1   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR   Í   s.    	c         C   s  zô yn |  j  t j k ra t j d k ra |  j  j j ƒ  } t d ƒ j | j ƒ  ƒ t d ƒ } n |  j  j ƒ  } Wn t	 t
 f k
 rò } |  j rì |  j rì t |  j d ƒ } | j ƒ  } | j ƒ  t d ƒ j | j ƒ  ƒ t 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   RP   RV   RQ   t   bufferR    R   R)   R6   R(   R   R   R   RR   t   closeRN   R2   (   R   R+   R0   t   b_source(    (    s/   /usr/lib/python2.7/dist-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    R6   t   True(   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyt	   readlines  s    c         C   s&   |  j  t j k	 r" |  j  j ƒ  n  d  S(   N(   R   RP   RV   R^   (   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR^     s    N(	   R   R   R=   R   R`   R   R    Ra   R^   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRK   È   s   .		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.
        RZ   s?   Warning: Destination mode "%s" differs from specified mode "%s"N(   RD   R   R`   t   openedRN   R   RZ   R   RO   RW   RP   t   stdoutRF   t   hasattrRX   RG   R   (   R   RF   RG   R   R   RN   RY   RZ   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR   0  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    R   R   RM   (   i   i    (   RP   RQ   RZ   R   R   RR   RG   RF   RS   R	   RT   RU   R`   Rd   (   R   R[   R1   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRR   [  s    c         C   sµ  |  j  s |  j ƒ  n  d |  j k r4 t j d	 k  sO t |  j |  j ƒ t k r— t j d
 k r… t	 j
 d k r… | j d t	 j
 ƒ } n  |  j | ƒ } n  z y |  j j | ƒ Wnå t k
 r^} t j d k r–t | t ƒ r–y |  j j j | ƒ Wq[t k
 rWt |  j |  j ƒ t k rNt d |  j p2d |  j j |  j f ƒ ‚ qX| ‚ q[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.
        R   i   i    s   
s;   Encoding of %s (%s) differs 
  from specified encoding (%s)RF   s:   Unable to encode output data. output-encoding is: %s.
(%s)N(   i   i    (   i   i    (   i   i    (   Rd   RR   RZ   RP   RQ   R   RF   R   RW   t   ost   linesepR'   RJ   RI   R   R#   t   bytesR]   R   t
   ValueErrorRG   R(   R   R   RN   R^   (   R   R+   t   eR0   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRI   i  s8    		!	c         C   s8   |  j  t j t j f k r4 |  j  j ƒ  t |  _ n  d  S(   N(   RF   RP   Re   t   stderrR^   RW   Rd   (   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR^   ‘  s    N(
   R   R   R=   RZ   R   R`   R   RR   RI   R^   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRb   $  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=   RZ   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRm   —  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.(   R2   R   (   R   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyR    ¨  s    (   R   R   R=   R   R    (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRo      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.(   RJ   RF   (   R   R+   (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRI   µ  s    (   R   R   R=   RH   RI   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRp   ­  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/   /usr/lib/python2.7/dist-packages/docutils/io.pyR    Ã  s    (   R   R   R=   R   R    (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRq   »  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/   /usr/lib/python2.7/dist-packages/docutils/io.pyRI   Ð  s    (   R   R   R=   RH   RI   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRr   È  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/   /usr/lib/python2.7/dist-packages/docutils/io.pyR    ß  s    (   R   R   R=   R   R    (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyRs   Õ  s   (   R=   t   __docformat__RP   Rg   R?   R
   t   docutilsR   t   docutils._compatR   t   docutils.utils.error_reportingR   R   R   RS   R   R	   R   R   RD   RK   Rb   Rm   Ro   Rp   Rq   Rr   Rs   (    (    (    s/   /usr/lib/python2.7/dist-packages/docutils/io.pyt   <module>   s*   	p0\s	