a
    mߛ`A                     @   s.  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m	Z	 ddl
mZmZmZ ejdkrbe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_functionZreStructuredTextN)TransformSpec)locale_encodingErrorStringErrorOutput   r   c                   @   s   e Zd ZdS )
InputErrorN__name__
__module____qualname__ r   r   *lib/python3.9/site-packages/docutils/io.pyr	          r	   c                   @   s   e Zd ZdS )OutputErrorNr
   r   r   r   r   r      r   r   c              
   C   s8   zt | jt |kW S  tttfy2   Y dS 0 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__5   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__I   s    
zInput.__repr__c                 C   s   t d S r   NotImplementedErrorr&   r   r   r   readM   s    z
Input.readc                 C   s   | j r&| j  dkr&t|ts&J dt|tr4|S | j rD| j g}n.| |}|rZ|g}nddg}trr|dt |D ]X}z&t||| j}|| _|	ddW   S  t
tfy } z|}W Y d}~qvd}~0 0 qvt
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 object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>}   r   z Input.decode.<locals>.<listcomp>)r   lower
isinstancer+   determine_encoding_from_datar   insertr   r"   replaceUnicodeErrorr   joinr   )r#   dataZ	encodingsdata_encodingr1   Zdecodederrerrorr   r   r   decodeP   s8    



zInput.decodes   coding[:=]\s*([-\w.]+)r,   z	utf-16-bez	utf-16-lec                 C   s`   | j D ]\}}||r|  S q| dd D ](}| j|}|r2|dd  S q2d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#   r:   Zstart_bytesr   linematchr   r   r   r5      s    

z"Input.determine_encoding_from_data)NNNr   )r   r   r   __doc__component_typer!   r$   r'   r*   r>   recompilerD   r   BOM_UTF8BOM_UTF16_BEBOM_UTF16_LErA   r5   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#   rR   rS   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%   rR   rS   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`.Nr(   r#   r:   r   r   r   write   s    zOutput.writec                 C   sL   | j r*| j  dkr*t|ts&J 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   r3   r4   r+   encoder   rU   r   r   r   rW      s    
zOutput.encode)NNNr   )
r   r   r   rI   rJ   rT   r$   r'   rV   rW   r   r   r   r   rP      s     
rP   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   Tr   rZrUc           	   
   C   s   t | |||| || _t | _|du r|rtjdkrF| j| jd}ni }zt	||fi || _
W q ty } zt|j|j|W Y d}~qd}~0 0 qtj| _
n2tjdkrt| j
| jdu rtd| j| j
jf |sz| j
j| _W n ty   Y n0 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.
        Nr   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   r8   namer    r   )	r#   r   r    r   r   r\   modekwargsr=   r   r   r   r$      s4    
(

zFileInput.__init__c              
   C   s   zzD| j tju r:tjdkr:| j j }d| d }n
| j  }W nf tt	fy } zJ| j
s| jrt| jd}| }|  d| d }n W Y d}~n
d}~0 0 W | jr|   n| jr|   0 | |S )zU
        Read and decode a single file and return the data (Unicode string).
        r      
rbN)r   r^   rd   r_   bufferr*   r9   rC   r8   r   r   r    r`   closer\   r>   )r#   r:   r<   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*   rC   r&   r   r   r   	readlines  s    zFileInput.readlinesc                 C   s   | j tjur| j   d S r   )r   r^   rd   rk   r&   r   r   r   rk      s    zFileInput.close)
r   r   r   rI   r^   r_   r$   r*   rl   rk   r   r   r   r   rX      s   
/rX   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ur,|| _t | _|du rR|rHd| _qtj| _	n6|rt
| j	dr|| j	jkrtd| j	j|f | jd |sz| j	j| _W n ty   Y n0 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.
        TNFrf   z?Warning: Destination mode "%s" differs from specified mode "%s")file)rP   r$   openedr\   rf   r   r]   r^   stdoutrR   hasattrprintre   rS   r   )r#   rR   rS   r   r   r\   Zhandle_io_errorsrf   r   r   r   r$   1  s8    




zFileOutput.__init__c              
   C   s   t jdkr$d| jvr$| j| jd}ni }zt| j| jfi || _W n6 tyz } zt	|j
|j| jW Y d }~n
d }~0 0 d| _d S )Nr   brZ   T)r^   r_   rf   r   r   r`   rS   rR   ra   r   rb   rc   rp   )r#   rg   r=   r   r   r   r`   \  s    
zFileOutput.openc                 C   sx  | j s|   d| jvr"tjdk s4t| j| jdu rf| |}tjdkrft	j
dkrf|dtt	j
d}zz| j| W n ty } z|tjdkrt|trz| jj| W nH ty   t| j| jdu rtd| jpd| jj| jf n|Y n0 W Y d	}~nHd	}~0  ttfyL } z"td
| jt|f W Y d	}~n
d	}~0 0 W | jrt|   n| jrr|   0 |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.
        rt   r   F
rh   r@   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rR   Nz:Unable to encode output data. output-encoding is: %s.
(%s))rp   r`   rf   r^   r_   r   rR   r   rW   oslinesepr7   bytesrV   r   r4   rj   r   
ValueErrorrS   r8   r   r   r\   rk   )r#   r:   r<   r   r   r   rV   j  sJ    



zFileOutput.writec                 C   s&   | j tjtjfvr"| j   d| _d S )NF)rR   r^   rq   stderrrk   rp   r&   r   r   r   rk     s    
zFileOutput.close)NNNr   TNN)	r   r   r   rI   rf   r$   r`   rV   rk   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   rI   rf   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.
    <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   rI   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.
    r~   c                 C   s   |  || _| jS )z=Encode `data`, store it in `self.destination`, and return it.)rW   rR   rU   r   r   r   rV     s    zStringOutput.writeNr   r   r   rI   rT   rV   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.readNr   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   rU   r   r   r   rV     s    zNullOutput.writeNr   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.readNr   r   r   r   r   r     s   r   )rI   Z
__future__r   Z__docformat__r^   rv   rK   r   Zdocutilsr   Zdocutils.utils.error_reportingr   r   r   r_   strr+   ra   r	   r   r   r   rP   rX   rm   r{   r}   r   r   r   r   r   r   r   r   <module>   s.   
p0Zr	