B
    ûgÅbS  ã            
   @   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Zddlm	Z	 y$e 
¡ d p\e ¡ d Ze ¡ ZW nD ek
rž Z zdejkrŠdZndZW ddZ[X Y n   dZY nX ye e¡ W n eefk
rØ   dZY nX G dd	„ d	eƒZG d
d„ deƒZdd„ Zdd„ ZG dd„ de	ƒZG dd„ de	ƒZG d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#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é   zunknown locale: UTF-8zutf-8c               @   s   e Zd ZdS )Ú
InputErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú*lib/python3.7/site-packages/docutils/io.pyr   0   s    r   c               @   s   e Zd ZdS )ÚOutputErrorN)r   r   r   r   r   r   r	   r
   1   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_encoding4   s    
r   c             C   s   | j j› d| › S )z5Return string representation of Exception `err`.
    z: )Ú	__class__r   )Úerrr   r   r	   Úerror_stringD   s    r   c               @   sp   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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__T   s    zInput.__init__c             C   s   d| j | j| jf S )Nz%s: source=%r, source_path=%r)r   r   r   )r   r   r   r	   Ú__repr__h   s    
zInput.__repr__c             C   s   t ‚d S )N)ÚNotImplementedError)r   r   r   r	   Úreadl   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g}trvtdkrv| t¡ | d¡ xX|D ]P}y t||| jƒ}|| _	| 
dd¡S  ttfk
rÔ } z|}W dd}~X Y q†X q†W tdd	 d
d„ |D ƒ¡› dt|ƒ› dƒ‚dS )aÄ  
        Decode `data` if required.

        Return Unicode `str` instances unchanged (nothing to decode).

        If `self.encoding` is None, determine encoding from data
        or try UTF-8, locale encoding, and (as last ressort) 'latin-1'.
        The client application should call ``locale.setlocale`` at the
        beginning of processing::

            locale.setlocale(locale.LC_ALL, '')

        Raise UnicodeError if unsuccessful.
        Úunicodez;input encoding is "unicode" but input is not a `str` objectzutf-8zlatin-1u   ï»¿Ú Nz=Unable to decode input data.  Tried the following encodings: z, c             s   s   | ]}t |ƒV  qd S )N)Úrepr)Ú.0Úencr   r   r	   ú	<genexpr>¥   s    zInput.decode.<locals>.<genexpr>z.
(ú))r   ÚlowerÚ
isinstanceÚstrÚAssertionErrorÚdetermine_encoding_from_dataÚ_locale_encodingÚappendr   r   ÚreplaceÚUnicodeErrorr   Újoinr   )r   ÚdataZencoding_candidatesÚdata_encodingr'   Zdecodedr   Úerrorr   r   r	   Údecodeo   s,    





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Úgroupr7   )r   r4   Zstart_bytesr   ÚlineÚmatchr   r   r	   r.   ³   s    
z"Input.determine_encoding_from_datac             C   s$   y
| j  ¡ S  tk
r   dS X d S )NF)r   Úisattyr   )r   r   r   r	   rB   Ã   s    
zInput.isatty)NNNr   )r   r   r   Ú__doc__Úcomponent_typer   r   r    r"   r7   ÚreÚcompiler=   r   ÚBOM_UTF8ÚBOM_UTF16_BEÚBOM_UTF16_LEr:   r.   rB   r   r   r   r	   r   J   s    
:
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 )ÚErrorOutputz’
    Wrapper class for file-like error streams with
    failsafe de- and encoding of `str`, `bytes`, `unicode` and
    `Exception` instances.
    NÚbackslashreplacer1   c             C   s^   |dkrt j}n|sd}nt|tƒr.t|dƒ}|| _|pJt|ddƒpJtpJd| _|| _	|| _
dS )a›  
        :Parameters:
            - `destination`: a file-like object,
                        a string (path to a file),
                        `None` (write to `sys.stderr`, default), or
                        evaluating to `False` (write() requests are ignored).
            - `encoding`: `destination` text encoding. Guessed if None.
            - `encoding_errors`: how to treat encoding errors.
        NFÚwr   r9   )ÚsysÚstderrr+   r,   ÚopenrL   Úgetattrr/   r   Úencoding_errorsÚdecoding_errors)r   rL   r   rX   rY   r   r   r	   r     s    


zErrorOutput.__init__c          	   C   sÎ   | j s
dS t|tƒrt|ƒ}y| j  |¡ W nœ tk
rX   | j  | | j| j¡¡ Y nr t	k
rÈ   t|tƒrŠ| j  | | j| j¡¡ n:| j t
jt
jfkr¬| j j |¡ n| j  t|| j| jƒ¡ Y nX dS )z›
        Write `data` to self.destination. Ignore, if self.destination is False.

        `data` can be a `bytes`, `str`, or `Exception` instance.
        N)rL   r+   Ú	Exceptionr,   rO   ÚUnicodeEncodeErrorrP   r   rX   r   rT   rU   ÚstdoutÚbufferrY   )r   r4   r   r   r	   rO   "  s"    

zErrorOutput.writec             C   s>   | j tjtjfkrdS y| j  ¡  W n tk
r8   Y nX dS )z–
        Close the error-output stream.

        Ignored if the destination is` sys.stderr` or `sys.stdout` or has no
        close() method.
        N)rL   rT   r\   rU   Úcloser   )r   r   r   r	   r^   <  s    zErrorOutput.closec             C   s$   y
| j  ¡ S  tk
r   dS X d S )NF)rL   rB   r   )r   r   r   r	   rB   J  s    
zErrorOutput.isatty)NNrR   r1   )r   r   r   rC   r   rO   r^   rB   r   r   r   r	   rQ   ú   s     
rQ   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Úrc          
   C   sÞ   t  | ||||¡ || _tƒ | _|dkrŠ|r€yt||| jp<d| jd| _W qˆ t	k
r| } zt
|j|j|ƒ‚W dd}~X Y qˆX q²tj| _n(t| j| jƒdkr²td| j| jjf ƒ‚|sÚy| 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 is read only ('r').
        Nz	utf-8-sig)r   ÚerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)r   r   Ú	autocloserQ   Ú_stderrrV   r   r   r   ÚOSErrorr   ÚerrnoÚstrerrorrT   Ústdinr   r2   Únamer   r   )r   r   r   r   r   rb   Úmoder6   r   r   r	   r   V  s(    $
zFileInput.__init__c          	   C   s¢   zry(| j tjkr| j j ¡ }n
| j  ¡ }W nD ttfk
rn   | jsh| jrht	| jdƒ}| ¡ }| 
¡  n‚ Y nX W d| jr‚|  
¡  X |  |¡}d | ¡ dg ¡S )zU
        Read and decode a single file and return the data (Unicode string).
        ÚrbNÚ
r$   )r   rT   rg   r]   r"   r2   r   r   r   rV   r^   rb   r7   r3   r<   )r   r4   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   rT   rg   r^   )r   r   r   r	   r^     s    zFileInput.close)NNNr   Tr`   )r   r   r   rC   r   r"   rl   r^   r   r   r   r	   r_   Q  s     
&r_   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.
    rS   Nr   Tc             C   sÌ   t  | ||||¡ d| _|| _|dk	r6tjdtdd |dk	rD|| _tƒ | _	|dkrj|r`d| _q t
j| _n6|r t| jdƒr || jjkr td| jj|f | j	d	 |sÈy| 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.
        TNz_io.FileOutput: init argument "handle_io_errors" is ignored and will be removed in Docutils 2.0.r8   )Ú
stacklevelFri   z?Warning: Destination mode "%s" differs from specified mode "%s")Úfile)rJ   r   Úopenedrb   ÚwarningsÚwarnÚDeprecationWarningri   rQ   rc   rT   r\   rL   ÚhasattrÚprintrh   rM   r   )r   rL   rM   r   r   rb   Zhandle_io_errorsri   r   r   r	   r   ®  s0    



zFileOutput.__init__c          
   C   sv   d| j kr| j| jdœ}ni }yt| j| j f|Ž| _W n4 tk
rj } zt|j|j	| jƒ‚W d d }~X Y nX d| _
d S )NÚb)r   ra   T)ri   r   r   rV   rM   rL   rd   r
   re   rf   rp   )r   Úkwargsr6   r   r   r	   rV   Ý  s    

zFileOutput.openc             C   sF  | j s|  ¡  d| jkrRt| j| jƒdkrR|  |¡}tjdkrR| 	dt
tjdƒ¡}zÜy| j |¡ W nÆ tk
rî } zlt|t
ƒrÞy| jj |¡ W nJ tk
rÜ   t| j| jƒdkrÔtd| jpÀd| jj| jf ƒ‚n|‚Y nX W dd}~X Y n> ttfk
r* } ztd	| jt|ƒf ƒ‚W dd}~X Y nX W d| jr@|  ¡  X |S )
zÐ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.
        rv   Frk   ó   
r9   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rL   Nz:Unable to encode output data. output-encoding is: %s.
(%s))rp   rV   ri   r   rL   r   rP   ÚosÚlinesepr1   ÚbytesrO   r   r+   r]   r   Ú
ValueErrorrM   r2   r   r   rb   r^   )r   r4   r   r   r   r	   rO   ë  s:    



(
zFileOutput.writec             C   s&   | j tjtjfkr"| j  ¡  d| _d S )NF)rL   rT   r\   rU   r^   rp   )r   r   r   r	   r^     s    
zFileOutput.close)NNNr   TNN)	r   r   r   rC   ri   r   rV   rO   r^   r   r   r   r	   rm   ¢  s     
-(rm   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   ri   r   r   r   r	   r}     s   r}   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.)r7   r   )r   r   r   r	   r"   *  s    zStringInput.readN)r   r   r   rC   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.)rP   rL   )r   r4   r   r   r	   rO   7  s    zStringOutput.writeN)r   r   r   rC   rN   rO   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"   E  s    zNullInput.readN)r   r   r   rC   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   r4   r   r   r	   rO   R  s    zNullOutput.writeN)r   r   r   rC   rN   rO   r   r   r   r	   r‚   J  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"   a  s    zDocTreeInput.readN)r   r   r   rC   r   r"   r   r   r   r	   rƒ   W  s   rƒ   )$rC   Z__docformat__r   Zlocalery   rE   rT   rq   Zdocutilsr   Z	getlocaleZgetdefaultlocaler/   r*   r|   r6   Úargsr   r   r   rd   r   r
   r   r   r   rJ   rQ   r_   rm   r}   r   r€   r   r‚   rƒ   r   r   r   r	   Ú<module>   sJ   	


 0WQw	