o
    /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	 ze
 d p.e d Ze ZW n! eyP Z zdejv rDdZndZW Y dZ[n
dZ[w   dZY zee W n eefyj   dZY nw 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-8utf-8c                   @      e Zd ZdS )
InputErrorN__name__
__module____qualname__ r   r   /usr/lib/python3.10/site-packages/docutils/io.pyr   0       r   c                   @   r   )OutputErrorNr   r   r   r   r   r   1   r   r   c              
   C   s4   zt | jt |kW S  tttfy   Y dS w )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                   @   sx   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   s6   || _ 	 || _	 || _	 || _	 |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      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      t r   NotImplementedErrorr&   r   r   r   readl   s   z
Input.readc                 C   s  | j r| j  dkrt|tsJ dt|tr|S | j r"| j g}n| |}|r-|g}ndg}tr;tdkr;|t |d |D ]+}zt||| j}|| _|	ddW   S  t
tfym } z|}W Y d}~qBd}~ww t
dd	d
d |D  dt| d)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` objectr   zlatin-1u   ﻿ Nz=Unable to decode input data.  Tried the following encodings: z, c                 s   s    | ]}t |V  qd S r   )repr).0encr   r   r   	<genexpr>   s    zInput.decode.<locals>.<genexpr>z.
())r   lower
isinstancestrdetermine_encoding_from_data_locale_encodingappendr   r"   replaceUnicodeErrorr   joinr   )r#   dataZencoding_candidatesdata_encodingr0   decodedr   errorr   r   r   decodeo   s:   




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|}|r-|dd  S q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#   r<   Zstart_bytesr   linematchr   r   r   r6      s   
z"Input.determine_encoding_from_datac                 C   "   z| j  W S  ty   Y dS w NF)r   isattyr   r&   r   r   r   rM      
   zInput.isattyNNNr   )r   r	   r
   __doc__component_typer!   r$   r'   r+   r@   recompilerF   r   BOM_UTF8BOM_UTF16_BEBOM_UTF16_LErC   r6   rM   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   s8   || _ 	 |pd| _	 || _	 || _	 |s| j| _d S d S )Nr   )r   r   destinationdestination_pathdefault_destination_path)r#   rY   rZ   r   r   r   r   r   r$      s   
zOutput.__init__c                 C   r%   )Nz'%s: destination=%r, destination_path=%r)r   rY   rZ   r&   r   r   r   r'      s   zOutput.__repr__c                 C   r(   )z;`data` is a Unicode string, to be encoded by `self.encode`.r)   r#   r<   r   r   r   write      zOutput.writec                 C   sH   | j r| j  dkrt|tsJ d|S t|ts|S || j | jS )Nr,   zFthe encoding given is "unicode" but the output is not a Unicode string)r   r3   r4   r5   encoder   r\   r   r   r   r_      s   
zOutput.encoderO   )
r   r	   r
   rP   rQ   r[   r$   r'   r]   r_   r   r   r   r   rW      s    
rW   c                   @   s8   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backslashreplacer9   c                 C   sd   |du rt j}n|sd}n
t|trt|d}|| _	 |p&t|ddp&tp&d| _	 || _		 || _
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   rB   )sysstderrr4   r5   openrY   getattrr7   r   encoding_errorsdecoding_errors)r#   rY   r   rg   rh   r   r   r   r$     s$   

zErrorOutput.__init__c              	   C   s   | j sdS t|trt|}z	| j | W dS  ty-   | j || j| j Y dS  t	yi   t|trG| j || j| j Y dS | j t
jt
jfv rZ| j j| Y dS | j t|| j| j Y dS w )z
        Write `data` to self.destination. Ignore, if self.destination is False.

        `data` can be a `bytes`, `str`, or `Exception` instance.
        N)rY   r4   	Exceptionr5   r]   UnicodeEncodeErrorr_   r   rg   r   rc   rd   stdoutbufferrh   r\   r   r   r   r]   "  s*   

zErrorOutput.writec                 C   s<   | j tjtjfv rdS z| j   W dS  ty   Y dS w )z
        Close the error-output stream.

        Ignored if the destination is` sys.stderr` or `sys.stdout` or has no
        close() method.
        N)rY   rc   rk   rd   closer   r&   r   r   r   rm   <  s   zErrorOutput.closec                 C   rK   rL   )rY   rM   r   r&   r   r   r   rM   J  rN   zErrorOutput.isatty)NNra   r9   )r   r	   r
   rP   r$   r]   rm   rM   r   r   r   r   r`      s    
!r`   c                   @   s8   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u r?|r:zt||| jpd| jd| _W n- t	y9 } zt
|j|j|d}~ww tj| _nt| j| jdu rStd| j| jjf |shz| jj| _W dS  tyg   Y dS w 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$   	autocloser`   _stderrre   r   r   r   OSErrorr   errnostrerrorrc   stdinr   r:   namer    r   )r#   r   r    r   r   rr   moder?   r   r   r   r$   V  s4   
zFileInput.__init__c              	   C   s   z?z| j tju r| j j }n| j  }W n! ttfy6   | js3| jr3t	| jd}| }|
  n Y nw W | jr?| 
  n	| jrH| 
  w w | |}d| dg S )zU
        Read and decode a single file and return the data (Unicode string).
        rb
r-   )r   rc   rw   rl   r+   r:   r   r   r    re   rm   rr   r@   r;   rE   )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+   rE   r&   r   r   r   	readlines  s   zFileInput.readlinesc                 C   s   | j tjur| j   d S d S r   )r   rc   rw   rm   r&   r   r   r   rm     s   zFileInput.close)NNNr   Tro   )r   r	   r
   rP   r$   r+   r|   rm   r   r   r   r   rn   Q  s    
(rn   c                   @   s>   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.
    rb   Nr   Tc                 C   s   t | |||| d| _|| _|durtjdtdd |dur"|| _t | _	|du r5|r0d| _n t
j| _n|rPt| jdrP|| jjkrPtd| jj|f | j	d	 |sez| jj| _W dS  tyd   Y dS w 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.rA   )
stacklevelFry   z?Warning: Destination mode "%s" differs from specified mode "%s")file)rW   r$   openedrr   warningswarnDeprecationWarningry   r`   rs   rc   rk   rY   hasattrprintrx   rZ   r   )r#   rY   rZ   r   r   rr   Zhandle_io_errorsry   r   r   r   r$     sB   



zFileOutput.__init__c              
   C   sp   d| j vr| j| jd}ni }zt| j| j fi || _W n ty2 } z	t|j|j	| jd }~ww d| _
d S )Nbrp   T)ry   r   r   re   rZ   rY   rt   r   ru   rv   r   )r#   kwargsr?   r   r   r   re     s   


zFileOutput.openc                 C   s@  | j s|   d| jvr)t| j| jdu r)| |}tjdkr)|	dt
tjd}zmz| j| W nZ tyt } z6t|t
rjz	| jj| W n! tyi   t| j| jdu rgtd| jp^d| jj| jf |w W Y d}~nd}~w ttfy } ztd	| jt|f d}~ww W | jr|   |S | jr|   w w )
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   Fr{      
rB   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rY   Nz:Unable to encode output data. output-encoding is: %s.
(%s))r   re   ry   r   rY   r   r_   oslinesepr9   bytesr]   r   r4   rl   r   
ValueErrorrZ   r:   r   r   rr   rm   )r#   r<   r   r   r   r   r]     sZ   




zFileOutput.writec                 C   s*   | j tjtjfvr| j   d| _d S d S rL   )rY   rc   rk   rd   rm   r   r&   r   r   r   rm     s   

zFileOutput.close)NNNr   TNN)	r   r	   r
   rP   ry   r$   re   r]   rm   r   r   r   r   r}     s    
/(r}   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
   rP   ry   r   r   r   r   r     s    r   c                   @      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
   rP   r!   r+   r   r   r   r   r   "      r   c                   @   r   )StringOutputz
    Direct string output.
    r   c                 C   s   |  || _| jS )z=Encode `data`, store it in `self.destination`, and return it.)r_   rY   r\   r   r   r   r]   7  s   zStringOutput.writeNr   r	   r
   rP   r[   r]   r   r   r   r   r   /  r   r   c                   @   r   )	NullInputz)
    Degenerate input: read nothing.
    z
null inputc                 C      dS )zReturn a null string.r-   r   r&   r   r   r   r+   E  r^   zNullInput.readNr   r   r   r   r   r   =  r   r   c                   @   r   )
NullOutputz+
    Degenerate output: write nothing.
    znull outputc                 C   r   )z6Do nothing ([don't even] send data to the bit bucket).Nr   r\   r   r   r   r]   R  r^   zNullOutput.writeNr   r   r   r   r   r   J  r   r   c                   @   r   )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.readNr   r   r   r   r   r   W  s    r   )$rP   Z__docformat__r   localer   rR   rc   r   docutilsr   	getlocalegetdefaultlocaler7   r3   r   r?   argsr   r   r   rt   r   r   r   r   r   rW   r`   rn   r}   r   r   r   r   r   r   r   r   r   r   <module>   sP   	
 0WQw	