o
    !5p`�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r1e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                   @   ó   e Zd ZdS )Ú
InputErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   ú-/usr/lib/python3/dist-packages/docutils/io.pyr      ó    r   c                   @   r
   )ÚOutputErrorNr   r   r   r   r   r      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_encoding   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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__5   s   zInput.__init__c                 C   ó   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   ó   t ‚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ƒr|S | j r"| j g}n|  |¡}|r-|g}nddg}tr9| dt¡ |D ]+}zt||| jƒ}|| _| 	dd¡W   S  t
tfyf } z|}W Y d}~q;d}~ww t
d	d
 dd„ |D ƒ¡t|ƒf ƒ‚)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>}   s    z Input.decode.<locals>.<listcomp>)r   ÚlowerÚ
isinstancer/   Údetermine_encoding_from_datar   Úinsertr    r$   ÚreplaceÚUnicodeErrorr   Újoinr   )r%   ÚdataÚ	encodingsÚdata_encodingr5   ÚdecodedÚerrÚerrorr   r   r   ÚdecodeP   s<   
ÿ


€ÿÿÿÿzInput.decodes   coding[:=]\s*([-\w.]+)r0   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é   r1   Úascii)Úbyte_order_marksÚ
startswithÚ
splitlinesÚcoding_slugÚsearchÚgrouprD   )r%   r>   Ústart_bytesr   ÚlineÚmatchr   r   r   r9   Š   s   
ÿÿz"Input.determine_encoding_from_data©NNNr   )r   r   r   Ú__doc__Úcomponent_typer#   r&   r*   r.   rD   ÚreÚcompilerJ   r   ÚBOM_UTF8ÚBOM_UTF16_BEÚBOM_UTF16_LErG   r9   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   s8   || _ 	 |pd| _	 || _	 || _	 |s| j| _d S d S )Nr   )r   r    ÚdestinationÚdestination_pathÚdefault_destination_path)r%   rZ   r[   r   r    r   r   r   r&   ¥   s   
ÿzOutput.__init__c                 C   r'   )Nz'%s: destination=%r, destination_path=%r)r(   rZ   r[   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   r7   r8   r/   Úencoder    r]   r   r   r   r`   ¾   s   ÿ
zOutput.encoderP   )
r   r   r   rQ   rR   r\   r&   r*   r^   r`   r   r   r   r   rX   ›   s    
ÿrX   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   ÚrÚrUc           	   
   C   sö   t  | ||||¡ || _tƒ | _|du rK|rFtjdkr#| j| jdœ}ni }zt	||fi |¤Ž| _
W n2 tyE } zt|j|j|ƒ‚d}~ww tj| _
ntjdkrdt| j
| jƒdu rdtd| j| j
jf ƒ‚|syz| j
j| _W dS  tyx   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 '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   r<   Únamer"   r   )	r%   r!   r"   r   r    rf   ÚmodeÚkwargsrC   r   r   r   r&   Ð   s:   
ÿ€ÿ

þÿýzFileInput.__init__c              
   C   sÜ   z_z"| j tju rtjdkr| j j ¡ }d | ¡ ¡d }n| j  ¡ }W n3 tt	fyV } z%| j
sK| jrKt| jdƒ}| ¡ }| ¡  d | ¡ ¡d }n‚ W Y d}~nd}~ww W | jr_|  ¡  n	| jrh|  ¡  w w |  |¡S )zU
        Read and decode a single file and return the data (Unicode string).
        r   ó   
ÚrbN)r!   rh   rn   ri   Úbufferr.   r=   rI   r<   r   r   r"   rj   Úcloserf   rD   )r%   r>   rB   Úb_sourcer   r   r   r.   ÿ   s0   
€þ€ù€€ÿ
ÿ
zFileInput.readc                 C   s   |   ¡  d¡S )zK
        Return lines of a single file as list of Unicode strings.
        T)r.   rI   r)   r   r   r   Ú	readlines  s   zFileInput.readlinesc                 C   s   | j tjur| j  ¡  d S d S r   )r!   rh   rn   ru   r)   r   r   r   ru      s   ÿzFileInput.close)
r   r   r   rQ   rh   ri   r&   r.   rw   ru   r   r   r   r   ra   Ë   s    
ý/ra   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.
    ÚwNr   Tc                 C   s¶   t  | ||||¡ d| _|| _|dur|| _tƒ | _|du r)|r$d| _n tj| _	n|rDt
| j	dƒrD|| j	jkrDtd| j	j|f | jd� |sYz| j	j| _W dS  tyX   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.
        TNFrp   z?Warning: Destination mode "%s" differs from specified mode "%s")Úfile)rX   r&   Úopenedrf   rp   r   rg   rh   ÚstdoutrZ   ÚhasattrÚprintro   r[   r   )r%   rZ   r[   r   r    rf   Úhandle_io_errorsrp   r   r   r   r&   1  s:   
ÿ
ÿ
ÿ
ÿþÿýzFileOutput.__init__c              
   C   sz   t jdkrd| jvr| j| jdœ}ni }zt| j| jfi |¤Ž| _W n ty7 } z	t	|j
|j| jƒ‚d }~ww d| _d S )Nr   Úbrd   T)rh   ri   rp   r   r    rj   r[   rZ   rk   r   rl   rm   r{   )r%   rq   rC   r   r   r   rj   \  s   ÿ
ÿ€ÿ
zFileOutput.openc                 C   s^  | j s|  ¡  d| jvrtjdk st| j| jƒdu r3|  |¡}tjdkr3t	j
dkr3| dtt	j
dƒ¡}zrz| j |¡ W n_ tyƒ } z;tjdkryt|tƒryz	| jj |¡ W n! tyx   t| j| jƒdu rvtd| jpmd| 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 )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.
        r€   r   FÚ
rr   rF   z;Encoding of %s (%s) differs 
  from specified encoding (%s)rZ   Nz:Unable to encode output data. output-encoding is: %s.
(%s))r{   rj   rp   rh   ri   r   rZ   r   r`   ÚosÚlinesepr;   Úbytesr^   r   r8   rt   r   Ú
ValueErrorr[   r<   r   r   rf   ru   )r%   r>   rB   r   r   r   r^   j  sT   
ÿÿ
ÿþø€	ÿÿ€ÿ€þ
ÿzFileOutput.writec                 C   s*   | j tjtjfvr| j  ¡  d| _d S d S )NF)rZ   rh   r|   Ústderrru   r{   r)   r   r   r   ru   ‘  s   

þzFileOutput.close)NNNr   TNN)	r   r   r   rQ   rp   r&   rj   r^   ru   r   r   r   r   rx   %  s    
þ+'rx   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   rQ   rp   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.)rD   r!   r)   r   r   r   r.   ¨  s   zStringInput.readN©r   r   r   rQ   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`   rZ   r]   r   r   r   r^   µ  s   zStringOutput.writeN©r   r   r   rQ   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.r2   r   r)   r   r   r   r.   Ã  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_   zNullOutput.writeNr�   r   r   r   r   r’   È  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.   ß  s   zDocTreeInput.readNrŒ   r   r   r   r   r“   Õ  s    r“   )rQ   Ú
__future__r   Ú__docformat__rh   r‚   rS   r   Údocutilsr   Údocutils.utils.error_reportingr   r   r   ri   Ústrr/   rk   r   r   r   r   rX   ra   rx   r‡   rŠ   rŽ   r�   r’   r“   r   r   r   r   Ú<module>   s0   
p0Zr	