B
    <[)D                 @   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 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.
ZreStructuredText    N)TransformSpec)b)locale_encodingErrorStringErrorOutputc               @   s   e Zd ZdS )
InputErrorN)__name__
__module____qualname__ r   r   *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               @   sl   e Zd ZdZdZdZdddZdd Zd	d
 Zdd Z	e
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__2   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__F   s    
zInput.__repr__c             C   s   t d S )N)NotImplementedError)r   r   r   r   readJ   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>z   s    z Input.decode.<locals>.<listcomp>)r   lower
isinstancestrAssertionErrordetermine_encoding_from_datar   insertr   r   replaceUnicodeErrorr   joinr   )r   dataZ	encodingsdata_encodingr)   Zdecodederrerrorr   r   r   decodeM   s0    




zInput.decodez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groupr8   )r   r4   Zstart_bytesr   linematchr   r   r   r/      s    
z"Input.determine_encoding_from_data)NNNr   )r   r	   r
   __doc__component_typer   r   r!   r#   r8   recompiler   r>   r   BOM_UTF8BOM_UTF16_BEBOM_UTF16_LEr;   r/   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   rL   rM   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    rL   rM   )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   r4   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   r4   r   r   r   rP      s    
zOutput.encode)NNNr   )
r   r	   r
   rC   rD   rN   r   r!   rO   rP   r   r   r   r   rJ      s    
rJ   c               @   s2   e Zd ZdZdddZdd	 Zd
d Zdd ZdS )	FileInputz5
    Input for single, simple file-like objects.
    Nr   TrUc       
   
   K   s4  t | |||| || _t | _x.|D ]&}|dkr@tjd q&td| q&W |dkr|rtj	dkrv| j
| jd}ni }yt||f|| _W q tk
r }	 zt|	j|	j|W dd}	~	X Y qX ntj| _n6tj	dkrt| j| j
dkrtd| j
| jj
f |s0y| 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.
        handle_io_errorszdeprecation warning: io.FileInput() argument `handle_io_errors` is ignored since "Docutils 0.10 (2012-12-16)" and will soon be removed.z2__init__() got an unexpected keyword argument '%s'N)   r   )r   errorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)r   r   	autocloser   _stderrsysstderrrO   r   version_infor   r   openr   IOErrorr   errnostrerrorstdinr   r2   namer   r   )
r   r   r   r   r   rV   modekwargskeyr7   r   r   r   r      s8    

$
zFileInput.__init__c          
   C   s   zyL| j tjkrBtjdkrB| j j }td| td }n
| j  }W nl t	t
fk
r } zJ| js| jrt| jd}| }|  td| t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).
        )rT   r   
rbN)r   rX   r_   rZ   bufferr#   r   r3   r=   r2   r   r   r   r[   closerV   r8   )r   r4   r6   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   rX   r_   rg   )r   r   r   r   rg   (  s    zFileInput.close)NNNr   TrR   )r   r	   r
   rC   r   r#   rh   rg   r   r   r   r   rQ      s     
7rQ   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	r,|| _t | _|dkrR|rHd| _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.
        TNFra   z?Warning: Destination mode "%s" differs from specified mode "%s")file)rJ   r   openedrV   ra   r   rW   rX   stdoutrL   hasattrprintr`   rM   r   )r   rL   rM   r   r   rV   rS   ra   r   r   r   r   9  s(    

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)rT   r   r   )r   rU   T)rX   rZ   ra   r   r   r[   rM   rL   r\   r   r]   r^   rl   )r   rb   r7   r   r   r   r[   d  s    
zFileOutput.openc             C   sn  | j s|   d| jkr"tjdk s4t| j| jdkrh| |}tjdkrht	j
dkrh|tdtt	j
}zy| j| W n tk
r } z|tjdkrt|try| jj| W nL 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
rR } ztd| jt|f W dd}~X Y nX W d| jrh|   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.
        r   )rT   r   Frd   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rL   Nz:Unable to encode output data. output-encoding is: %s.
(%s))rl   r[   ra   rX   rZ   r   rL   r   rP   oslinesepr1   r   rO   r   r,   bytesrf   r   
ValueErrorrM   r2   r   r   rV   rg   )r   r4   er6   r   r   r   rO   r  s8    
(
zFileOutput.writec             C   s&   | j tjtjfkr"| j   d| _d S )NF)rL   rX   rm   rY   rg   rl   )r   r   r   r   rg     s    
zFileOutput.close)NNNr   TNN)	r   r	   r
   rC   ra   r   r[   rO   rg   r   r   r   r   ri   -  s     
)(ri   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
   rC   ra   r   r   r   r   ru     s   ru   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.)r8   r   )r   r   r   r   r#     s    zStringInput.readN)r   r	   r
   rC   r   r#   r   r   r   r   rw     s   rw   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.)rP   rL   )r   r4   r   r   r   rO     s    zStringOutput.writeN)r   r	   r
   rC   rN   rO   r   r   r   r   rx     s   rx   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
   rC   r   r#   r   r   r   r   ry     s   ry   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   r4   r   r   r   rO     s    zNullOutput.writeN)r   r	   r
   rC   rN   rO   r   r   r   r   rz     s   rz   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
   rC   r   r#   r   r   r   r   r{     s   r{   )rC   Z__docformat__rX   rp   rE   r   Zdocutilsr   Zdocutils._compatr   Zdocutils.utils.error_reportingr   r   r   r\   r   r   r   r   rJ   rQ   ri   ru   rw   rx   ry   rz   r{   r   r   r   r   <module>   s*   p0es	