
    >WBeB                        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Zddl	m
Z
 ddlmZmZmZ ej                  dk\  reZ G d d	e      Z G d
 de      Zd Z G d de
      Z G d de
      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Zy)!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y)
InputErrorN__name__
__module____qualname__     +lib/python3.12/site-packages/docutils/io.pyr   r          r   r   c                       e Zd Zy)OutputErrorNr   r   r   r   r   r      r   r   r   c                     	 t        j                  | j                        t        j                  |      k(  S # t        t        t
        f$ r Y yw xY 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   s     r   check_encodingr      sA    }}V__-x1HHH3 s   47 AAc                       e Zd ZdZdZdZ	 	 ddZd Zd Zd Z	 e
j                  d      Z	 ej                  d	fej                  d
fej                   dffZ	 d Zy)Inputz1
    Abstract base class for input wrappers.
    inputNc                 x    || _         	 || _        	 || _        	 || _        	 |s| j                  | _        d | _        y N)r   error_handlersourcesource_pathdefault_source_pathsuccessful_encoding)selfr&   r'   r   r%   s        r   __init__zInput.__init__6   sH     1**'&-#77D#' Er   c                 R    | j                   d| j                  d| j                  S )Nz	: source=z, source_path=)	__class__r&   r'   r*   s    r   __repr__zInput.__repr__J   s$    26..$++262B2BD 	Dr   c                     t         r$   NotImplementedErrorr.   s    r   readz
Input.readN   s    !!r   c                    | j                   r4| j                   j                         dk(  rt        |t              sJ d       t        |t              r|S | j                   r| j                   g}n7| j	                  |      }|r|g}n ddg}t
        r|j                  dt
               |D ]4  }	 t        ||| j                        }|| _        |j                  dd      c S  t        d	d
j                  |D cg c]  }t        |       c}      dt              d      # t        t        f$ r}|}Y d}~d}~ww xY wc c}w )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   ﻿ Nz=Unable to decode input data.  Tried the following encodings: z, .
())r   lower
isinstancer5   determine_encoding_from_datar   insertr%   r)   replaceUnicodeErrorr   joinreprr   )r*   data	encodingsdata_encodingencdecodederrerrors           r   decodezInput.decodeQ   s1    ==T]]002i?dG, 545 dG$K== I ==dCM +O	
 %i0	"$$Q8 	C!$T-?-?@+.(y#66	 999%ECd3i%EF$U+-. 	. !+. 
 &Fs   */D2D<D9-D44D9s   coding[:=]\s*([-\w.]+)r6   z	utf-16-bez	utf-16-lec                    | j                   D ]  \  }}|j                  |      s|c S  |j                         dd D ]A  }| j                  j	                  |      }|s!|j                  d      j                  d      c S  y)z
        Try to determine the encoding of `data` by looking *in* `data`.
        Check for a byte order mark (BOM) or an encoding declaration.
        N   r7   ascii)byte_order_marks
startswith
splitlinescoding_slugsearchgrouprJ   )r*   rC   start_bytesr   linematchs         r   r=   z"Input.determine_encoding_from_data   s     &*%:%: 	 !K{+	  OO%bq) 	6D$$++D1E{{1~,,W55	6 r   NNNstrict)r   r   r   __doc__component_typer(   r+   r/   r3   rJ   recompilerQ   r   BOM_UTF8BOM_UTF16_BEBOM_UTF16_LErN   r=   r   r   r   r!   r!   ,   s     N?C'F(D"..` "**78K''2,,k:,,k:=-r   r!   c                   6    e Zd ZdZdZdZ	 	 ddZd Zd Zd Z	y)	Outputz2
    Abstract base class for output wrappers.
    outputNc                 t    || _         	 |xs d| _        	 || _        	 || _        	 |s| j                  | _        y y )NrX   )r   r%   destinationdestination_pathdefault_destination_path)r*   rd   re   r   r%   s        r   r+   zOutput.__init__   sH     7*6h*&. 02$($A$AD!  r   c                 R    | j                   d| j                  d| j                  S )Nz: destination=z, destination_path=)r-   rd   re   r.   s    r   r/   zOutput.__repr__   s%    >>4#3#3T5J5JL 	Mr   c                     t         )z;`data` is a Unicode string, to be encoded by `self.encode`.r1   r*   rC   s     r   writezOutput.write   s    !!r   c                     | j                   r6| j                   j                         dk(  rt        |t              sJ d       |S t        |t              s|S |j	                  | j                   | j
                        S )Nr5   zFthe encoding given is "unicode" but the output is not a Unicode string)r   r;   r<   r5   encoder%   ri   s     r   rl   zOutput.encode   sg    ==T]]002i?dG, $#$ K$(K;;t}}d.@.@AAr   rW   )
r   r   r   rY   rZ   rf   r+   r/   rj   rl   r   r   r   ra   ra      s1     N#:>.6B"M"
Br   ra   c                   X    e Zd ZdZdddddej
                  dk\  rdndfdZd	 Zd
 Zd Z	y)	FileInputz5
    Input for single, simple file-like objects.
    NrX   Tr	   rrUc                    t         j                  | ||||       || _        t               | _        |[|rCt
        j                  dk\  r| j                  | j                  d}ni }	 t        ||fi || _
        n{t
        j                  | _
        net
        j                  dk\  rRt!        | j                  | j                        du r0t#        d| j                  d| j                  j                  d      |s	 | j                  j$                  | _        yy# t        $ r&}t        |j                  |j                  |      d}~ww xY w# t(        $ r Y yw xY w)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   errorsFz#Encoding clash: encoding given is "z&" but source is opened with encoding "z".)r!   r+   	autocloser   _stderrsysversion_infor   r%   openr&   IOErrorr   errnostrerrorstdinr   r@   namer'   r   )	r*   r&   r'   r   r%   rt   modekwargsrI   s	            r   r+   zFileInput.__init__   s.   " 	tV[(MJ""}>##v-*.--(,(:(:<F  FO"&{D"CF"CDK "ii&(T[[$--8EA $t{{/C/C E F F #';;#3#3    O$U[[%..+NNO " s*   $D 5E 	E!D<<E	EEc                    	 | j                   t        j                  u rZt        j                  dk\  rG| j                   j                  j                         }dj                  |j                               dz   }n| j                   j                         }| j                  r| j                          	 | j                  |      S # t        t        f$ r|}| j                  se| j                  rYt        | j                  d      }|j                         }|j                          dj                  |j                               dz   }n Y d}~d}~ww xY w# | j                  r| j                          w w xY w)zU
        Read and decode a single file and return the data (Unicode string).
        r	      
rbN)r&   rv   r|   rw   bufferr3   rA   rP   r@   r   r   r'   rx   closert   rJ   )r*   rC   rH   b_sources       r   r3   zFileInput.read   s   	{{cii'C,<,<,F{{))..0zz$//"34u<{{'') ~~

{{4   k* 		==T%5%5 0 0$7}} zz$//"34u< 		 ~~

 s+   BC   EA2EE EE E-c                 @    | j                         j                  d      S )zK
        Return lines of a single file as list of Unicode strings.
        T)r3   rP   r.   s    r   	readlineszFileInput.readlines  s     yy{%%d++r   c                 r    | j                   t        j                  ur| j                   j                          y y r$   )r&   rv   r|   r   r.   s    r   r   zFileInput.close!  s'    ;;cii'KK (r   )
r   r   r   rY   rv   rw   r+   r3   r   r   r   r   r   rn   rn      s=     #h --7cT-^!6, r   rn   c                   6    e Zd ZdZdZ	 	 	 	 ddZd Zd Zd Zy)	
FileOutputz6
    Output for single, simple file-like objects.
    wNc                 $   t         j                  | ||||       d| _        || _        |t	        j
                  dt        d       ||| _        t               | _	        | |rd| _        nyt        j                  | _        nc|rat        | j                  d      rK|| j                  j                  k7  r2t        d| j                  j                  d	|d
| j                         |s	 | j                  j                  | _        yy# t"        $ r Y yw xY w)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.
        TNziio.FileOutput: initialization argument "handle_io_errors" is ignored and will be removed in Docutils 1.2.rL   )
stacklevelFr~   zWarning: Destination mode "z" differs from specified mode "")file)ra   r+   openedrt   warningswarnDeprecationWarningr~   r   ru   rv   stdoutrd   hasattrprintr}   re   r   )r*   rd   re   r   r%   rt   handle_io_errorsr~   s           r   r+   zFileOutput.__init__2  s    & 	k+; -	1"'MM  !3C DI"}##&:: wt//8$**///%)%5%5%:%:DB<<)  (,(8(8(=(=%   " s   &D 	DDc                 R   t         j                  dk\  r(d| j                  vr| j                  | j                  d}ni }	 t        | j                  | j                  fi || _        d| _        y # t        $ r0}t        |j                  |j                  | j                        d }~ww xY w)Nr	   brr   T)rv   rw   r~   r   r%   rx   re   rd   ry   r   rz   r{   r   )r*   r   rI   s      r   rx   zFileOutput.opena  s    v%#TYY*>"&-- $ 2 24F F	5#D$9$9499OOD   	5ekk5>>"335 5	5s   &A- -	B&6+B!!B&c                 L   | j                   s| j                          d| j                  vrt        j                  dk  s"t        | j                  | j                        du ra| j                  |      }t        j                  dk\  r=t        j                  dk7  r*|j                  dt        t        j                  d            }	 | j                  j                  |       | j.                  r| j1                          |S # t        $ r}t        j                  dk\  rt        |t              r	 | j                  j                   j                  |       nr# t"        $ rf t        | j                  | j                        du rAt%        d| j&                  xs dd	| j                  j                  d
| j                  d      |w xY wY d}~d}~wt(        t*        f$ r*}t)        d| j                  dt-        |      d      d}~ww xY w# | j.                  r| j1                          w w xY 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   r	   F
r   rM   zEncoding of rd   z (z&) differs 
  from specified encoding (r:   Nz2Unable to encode output data. output-encoding is: r9   )r   rx   r~   rv   rw   r   rd   r   rl   oslinesepr?   bytesrj   r   r<   r   r   
ValueErrorre   r@   r   r   rt   r   )r*   rC   rH   s      r   rj   zFileOutput.writeo  s    {{IIKtyy S%5%5%>d..>%G;;t$D6)bjjD.@||E5W+EF	""4(& ~~

)  	"6)ju.E
"$$++11$7% "%d&6&6&*mm58=>(!22CmC ,,55t}}*F G G
 "	" k* 	A#}}k#.>@A A	A
 ~~

 sO   C; ;	H#G(%EGA/F==G H H%G<<HH H#c                     | j                   t        j                  t        j                  fvr"| j                   j	                          d| _        y y )NF)rd   rv   r   stderrr   r   r.   s    r   r   zFileOutput.close  s;    CJJ

#;;""$DK <r   )NNNrX   TNN)	r   r   r   rY   r~   r+   rx   rj   r   r   r   r   r   r   &  s2     D)
 ;?BF-1-^%N r   r   c                       e Zd ZdZdZy)BinaryFileOutputzL
    A version of docutils.io.FileOutput which writes to a binary file.
    wbN)r   r   r   rY   r~   r   r   r   r   r     s    
 Dr   r   c                       e Zd ZdZdZd Zy)StringInputz
    Direct string input.
    <string>c                 8    | j                  | j                        S )z$Decode and return the source string.)rJ   r&   r.   s    r   r3   zStringInput.read  s    {{4;;''r   Nr   r   r   rY   r(   r3   r   r   r   r   r     s     %(r   r   c                       e Zd ZdZdZd Zy)StringOutputz
    Direct string output.
    r   c                 F    | j                  |      | _        | j                  S )z=Encode `data`, store it in `self.destination`, and return it.)rl   rd   ri   s     r   rj   zStringOutput.write  s    ;;t,r   Nr   r   r   rY   rf   rj   r   r   r   r   r     s      * r   r   c                       e Zd ZdZdZd Zy)	NullInputz)
    Degenerate input: read nothing.
    z
null inputc                      y)zReturn a null string.r8   r   r.   s    r   r3   zNullInput.read  s    r   Nr   r   r   r   r   r     s     'r   r   c                       e Zd ZdZdZd Zy)
NullOutputz+
    Degenerate output: write nothing.
    znull outputc                      y)z6Do nothing ([don't even] send data to the bit bucket).Nr   ri   s     r   rj   zNullOutput.write  s    r   Nr   r   r   r   r   r     s      -r   r   c                       e Zd ZdZdZd Zy)DocTreeInputzm
    Adapter for document tree input.

    The document tree must be passed in the ``source`` parameter.
    zdoctree inputc                     | j                   S )zReturn the document tree.)r&   r.   s    r   r3   zDocTreeInput.read  s    {{r   Nr   r   r   r   r   r     s     *r   r   ) rY   
__future__r   __docformat__r   r   r[   rv   r   docutilsr   docutils.utils.error_reportingr   r   r   rw   strr5   ry   r   r   r   r!   ra   rn   r   r   r   r   r   r   r   r   r   r   <module>r      s   
 &"  	 	 
  " T TvG    '   mM m`-B] -B`W  W ts  s lz 
(% 
( 6  
 

 
5 r   