B
    dB                 @   s6  d Z ddlm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
 ddlmZmZmZ ejdkrjeZG dd	 d	eZG d
d deZdd ZG dd de
ZG dd de
ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZdS )"z
I/O classes provide a uniform API for low-level input and output.  Subclasses
exist for a variety of input/output mechanisms.
    )print_functionreStructuredTextN)TransformSpec)locale_encodingErrorStringErrorOutput)   r   c               @   s   e Zd ZdS )
InputErrorN)__name__
__module____qualname__ r   r   X/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/docutils/io.pyr	      s    r	   c               @   s   e Zd ZdS )OutputErrorN)r
   r   r   r   r   r   r   r      s    r   c          
   C   s6   yt | jt |kS  tttfk
r0   dS X dS )aN  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)codecslookupencodingLookupErrorAttributeError	TypeError)streamr   r   r   r   check_encoding   s    
r   c               @   sh   e Zd ZdZdZdZdddZdd Zd	d
 Zdd Z	e
dZejdfejdfejdffZdd ZdS )Inputz1
    Abstract base class for input wrappers.
    inputNstrictc             C   s.   || _ || _|| _|| _|s$| j| _d | _d S )N)r   error_handlersourcesource_pathdefault_source_pathsuccessful_encoding)selfr   r   r   r   r   r   r   __init__6   s    zInput.__init__c             C   s   d| j | j| jf S )Nz%s: source=%r, source_path=%r)	__class__r   r   )r    r   r   r   __repr__J   s    
zInput.__repr__c             C   s   t d S )N)NotImplementedError)r    r   r   r   readN   s    z
Input.readc             C   s   | j r&| j  dkr&t|ts&tdt|tr4|S | j rD| j g}n.| |}|rZ|g}nddg}trr|dt xX|D ]P}y t||| j}|| _	|
ddS  ttfk
r } z|}W dd}~X Y qxX qxW td	d
dd |D t|f dS )a   
        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, '')
        unicodez=input encoding is "unicode" but input is not a unicode objectzutf-8zlatin-1   u   ﻿ NzEUnable to decode input data.  Tried the following encodings: %s.
(%s)z, c             S   s   g | ]}t |qS r   )repr).0encr   r   r   
<listcomp>~   s    z Input.decode.<locals>.<listcomp>)r   lower
isinstancer&   AssertionErrordetermine_encoding_from_datar   insertr   r   replaceUnicodeErrorr   joinr   )r    data	encodingsdata_encodingr+   decodederrerrorr   r   r   decodeQ   s0    




zInput.decodes   coding[:=]\s*([-\w.]+)zutf-8z	utf-16-bez	utf-16-lec             C   s`   x | j D ]\}}||r|S qW x8| dd D ]$}| j|}|r4|ddS q4W dS )z
        Try to determine the encoding of `data` by looking *in* `data`.
        Check for a byte order mark (BOM) or an encoding declaration.
        N   r'   ascii)byte_order_marks
startswith
splitlinescoding_slugsearchgroupr;   )r    r5   Zstart_bytesr   linematchr   r   r   r0      s    
z"Input.determine_encoding_from_data)NNNr   )r
   r   r   __doc__component_typer   r!   r#   r%   r;   recompilerA   r   BOM_UTF8BOM_UTF16_BEBOM_UTF16_LEr>   r0   r   r   r   r   r   ,   s    
0
r   c               @   s:   e Zd ZdZdZdZdddZdd Zd	d
 Zdd Z	dS )Outputz2
    Abstract base class for output wrappers.
    outputNr   c             C   s,   || _ |pd| _|| _|| _|s(| j| _d S )Nr   )r   r   destinationdestination_pathdefault_destination_path)r    rO   rP   r   r   r   r   r   r!      s    
zOutput.__init__c             C   s   d| j | j| jf S )Nz'%s: destination=%r, destination_path=%r)r"   rO   rP   )r    r   r   r   r#      s    zOutput.__repr__c             C   s   t dS )z;`data` is a Unicode string, to be encoded by `self.encode`.N)r$   )r    r5   r   r   r   write   s    zOutput.writec             C   sL   | j r*| j  dkr*t|ts&td|S t|ts8|S || j | jS d S )Nr&   zFthe encoding given is "unicode" but the output is not a Unicode string)r   r-   r.   r&   r/   encoder   )r    r5   r   r   r   rS      s    
zOutput.encode)NNNr   )
r
   r   r   rF   rG   rQ   r!   r#   rR   rS   r   r   r   r   rM      s    
rM   c               @   sL   e Zd ZdZdddddejdkr$dndfdd	Zd
d Zdd Zdd Z	dS )	FileInputz5
    Input for single, simple file-like objects.
    Nr   T)r   r   rZrUc       	   
   C   s   t | |||| || _t | _|dkr|rtjdkrF| j| jd}ni }yt	||f|| _
W q tk
r } zt|j|j|W dd}~X Y qX qtj| _
n2tjdkrt| j
| jdkrtd| j| j
jf |sy| j
j| _W n tk
r   Y nX dS )a  
        :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 with Python 2.x.
        N)r   r   )r   errorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)r   r!   	autocloser   _stderrsysversion_infor   r   openr   IOErrorr	   errnostrerrorstdinr   r3   namer   r   )	r    r   r   r   r   rW   modekwargsr:   r   r   r   r!      s.    
$

zFileInput.__init__c          
   C   s   zyD| j tjkr:tjdkr:| j j }d| d }n
| j  }W nd tt	fk
r } zB| j
s| jrt| jd}| }|  d| d }n W dd}~X Y nX W d| jr|   X | |S )zU
        Read and decode a single file and return the data (Unicode string).
        )r   r      
rbN)r   rY   r_   rZ   bufferr%   r4   r@   r3   r   r   r   r[   closerW   r;   )r    r5   r9   Zb_sourcer   r   r   r%      s    
zFileInput.readc             C   s   |   dS )zK
        Return lines of a single file as list of Unicode strings.
        T)r%   r@   )r    r   r   r   	readlines  s    zFileInput.readlinesc             C   s   | j tjk	r| j   d S )N)r   rY   r_   rf   )r    r   r   r   rf   !  s    zFileInput.close)
r
   r   r   rF   rY   rZ   r!   r%   rg   rf   r   r   r   r   rT      s   ,rT   c               @   s6   e Zd ZdZdZdddZdd	 Zd
d Zdd ZdS )
FileOutputz6
    Output for single, simple file-like objects.
    wNr   Tc             C   s   t | |||| d| _|| _|dk	r6tjdtdd |dk	rD|| _t | _	|dkrj|r`d| _qt
j| _n6|rt| jdr|| jjkrtd| jj|f | j	d	 |sy| jj| _W n tk
r   Y nX dS )
aA  
        :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.
        TNziio.FileOutput: initialization argument "handle_io_errors" is ignored and will be removed in Docutils 1.2.r<   )
stacklevelFra   z?Warning: Destination mode "%s" differs from specified mode "%s")file)rM   r!   openedrW   warningswarnDeprecationWarningra   r   rX   rY   stdoutrO   hasattrprintr`   rP   r   )r    rO   rP   r   r   rW   Zhandle_io_errorsra   r   r   r   r!   2  s0    



zFileOutput.__init__c          
   C   s   t jdkr$d| jkr$| j| jd}ni }yt| j| jf|| _W n4 tk
rt } zt	|j
|j| jW d d }~X Y nX d| _d S )N)r   r   b)r   rV   T)rY   rZ   ra   r   r   r[   rP   rO   r\   r   r]   r^   rl   )r    rb   r:   r   r   r   r[   a  s    
zFileOutput.openc             C   sf  | j s|   d| jkr"tjdk s4t| j| jdkrf| |}tjdkrft	j
dkrf|dtt	j
d}zy| j| W n tk
r } zvtjdkrt|try| jj| W nJ tk
r   t| j| jdkrtd| jpd| jj| jf n|Y nX W d	d	}~X Y n> ttfk
rJ } ztd
| jt|f W d	d	}~X Y nX W d	| jr`|   X |S )zEncode `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.
        rs   )r   r   F
rc   r=   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rO   Nz:Unable to encode output data. output-encoding is: %s.
(%s))rl   r[   ra   rY   rZ   r   rO   r   rS   oslinesepr2   bytesrR   r   r.   re   r   
ValueErrorrP   r3   r   r   rW   rf   )r    r5   r9   r   r   r   rR   o  s6    
(
zFileOutput.writec             C   s&   | j tjtjfkr"| j   d| _d S )NF)rO   rY   rp   stderrrf   rl   )r    r   r   r   rf     s    
zFileOutput.close)NNNr   TNN)	r
   r   r   rF   ra   r!   r[   rR   rf   r   r   r   r   rh   &  s     
-'rh   c               @   s   e Zd ZdZdZdS )BinaryFileOutputzL
    A version of docutils.io.FileOutput which writes to a binary file.
    wbN)r
   r   r   rF   ra   r   r   r   r   rz     s   rz   c               @   s   e Zd ZdZdZdd ZdS )StringInputz
    Direct string input.
    z<string>c             C   s   |  | jS )z$Decode and return the source string.)r;   r   )r    r   r   r   r%     s    zStringInput.readN)r
   r   r   rF   r   r%   r   r   r   r   r|     s   r|   c               @   s   e Zd ZdZdZdd ZdS )StringOutputz
    Direct string output.
    z<string>c             C   s   |  || _| jS )z=Encode `data`, store it in `self.destination`, and return it.)rS   rO   )r    r5   r   r   r   rR     s    zStringOutput.writeN)r
   r   r   rF   rQ   rR   r   r   r   r   r}     s   r}   c               @   s   e Zd ZdZdZdd ZdS )	NullInputz)
    Degenerate input: read nothing.
    z
null inputc             C   s   dS )zReturn a null string.r(   r   )r    r   r   r   r%     s    zNullInput.readN)r
   r   r   rF   r   r%   r   r   r   r   r~     s   r~   c               @   s   e Zd ZdZdZdd ZdS )
NullOutputz+
    Degenerate output: write nothing.
    znull outputc             C   s   dS )z6Do nothing ([don't even] send data to the bit bucket).Nr   )r    r5   r   r   r   rR     s    zNullOutput.writeN)r
   r   r   rF   rQ   rR   r   r   r   r   r     s   r   c               @   s   e Zd ZdZdZdd ZdS )DocTreeInputzm
    Adapter for document tree input.

    The document tree must be passed in the ``source`` parameter.
    zdoctree inputc             C   s   | j S )zReturn the document tree.)r   )r    r   r   r   r%     s    zDocTreeInput.readN)r
   r   r   rF   r   r%   r   r   r   r   r     s   r   ) rF   
__future__r   __docformat__r   ru   rH   rY   rm   Zdocutilsr   Zdocutils.utils.error_reportingr   r   r   rZ   strr&   r\   r	   r   r   r   rM   rT   rh   rz   r|   r}   r~   r   r   r   r   r   r   <module>   s0   
p0Zv	