3

T'ícAã@s.dZddlmZdZddlZddlZddlZddlZddlm	Z	ddl
mZmZm
Z
ejd"krbeZGdd	„d	eƒZGd
d„deƒZdd
„ZGdd„de	ƒZGdd„de	ƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGd d!„d!eƒZdS)#z
I/O classes provide a uniform API for low-level input and output.  Subclasses
exist for a variety of input/output mechanisms.
é)Úprint_functionZreStructuredTextN)Ú
TransformSpec)Úlocale_encodingÚErrorStringÚErrorOutputéc@seZdZdS)Ú
InputErrorN)Ú__name__Ú
__module__Ú__qualname__©rrú//tmp/pip-build-gk9425m9/docutils/docutils/io.pyrsrc@seZdZdS)ÚOutputErrorN)r	r
rrrrr
rsrc
Cs6ytj|jƒtj|ƒkStttfk
r0dSXdS)aNTest, 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)Ústreamrrrr
Úcheck_encodings
rc@sheZdZdZdZdZddd„Zdd„Zd	d
„Zdd„Z	e
jd
ƒZe
jdfe
jdfe
jdffZdd„ZdS)ÚInputz1
    Abstract base class for input wrappers.
    ÚinputNÚstrictcCs.||_||_||_||_|s$|j|_d|_dS)N)rÚ
error_handlerÚsourceÚsource_pathÚdefault_source_pathÚsuccessful_encoding)Úselfrrrrrrr
Ú__init__5szInput.__init__cCsd|j|j|jfS)Nz%s: source=%r, source_path=%r)Ú	__class__rr)rrrr
Ú__repr__Is
zInput.__repr__cCst‚dS)N)ÚNotImplementedError)rrrr
ÚreadMsz
Input.readcCsô|jr&|jjƒdkr&t|tƒs&tdƒ‚t|tƒr4|S|jrD|jg}n.|j|ƒ}|rZ|g}nddg}trr|jdtƒxX|D]P}y t|||jƒ}||_	|j
ddƒSttfk
rÆ}z
|}WYdd}~XqxXqxWtd	d
j
dd„|Dƒƒt|ƒfƒ‚dS)
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 objectzutf-8zlatin-1éuÚNzEUnable to decode input data.  Tried the following encodings: %s.
(%s)z, cSsg|]}t|ƒ‘qSr)Úrepr)Ú.0Úencrrr
ú
<listcomp>}sz Input.decode.<locals>.<listcomp>)rÚlowerÚ
isinstancer%ÚAssertionErrorÚdetermine_encoding_from_datarÚinsertrrÚreplaceÚUnicodeErrorrÚjoinr)rÚdataÚ	encodingsÚ
data_encodingr*ÚdecodedÚerrÚerrorrrr
ÚdecodePs0




zInput.decodescoding[:=]\s*([-\w.]+)zutf-8z	utf-16-bez	utf-16-lecCs`x |jD]\}}|j|ƒr|SqWx8|jƒdd…D]$}|jj|ƒ}|r4|jdƒjdƒSq4WdS)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:)rr4Zstart_bytesrÚlineÚmatchrrr
r/Šs
z"Input.determine_encoding_from_data)NNNr)r	r
rÚ__doc__Úcomponent_typerr r"r$r:ÚreÚcompiler@rÚBOM_UTF8ÚBOM_UTF16_BEÚBOM_UTF16_LEr=r/rrrr
r+s
0
rc@s:eZdZdZdZdZd
dd„Zdd„Zd	d
„Zdd„Z	dS)ÚOutputz2
    Abstract base class for output wrappers.
    ÚoutputNrcCs,||_|pd|_||_||_|s(|j|_dS)Nr)rrÚdestinationÚdestination_pathÚdefault_destination_path)rrNrOrrrrr
r ¥s
zOutput.__init__cCsd|j|j|jfS)Nz'%s: destination=%r, destination_path=%r)r!rNrO)rrrr
r"¶szOutput.__repr__cCst‚dS)z;`data` is a Unicode string, to be encoded by `self.encode`.N)r#)rr4rrr
ÚwriteºszOutput.writecCsL|jr*|jjƒdkr*t|tƒs&tdƒ‚|St|tƒs8|S|j|j|jƒSdS)Nr%zFthe encoding given is "unicode" but the output is not a Unicode string)rr,r-r%r.Úencoder)rr4rrr
rR¾s
z
Output.encode)NNNr)
r	r
rrErFrPr r"rQrRrrrr
rL›s
rLc@sLeZdZdZdddddejdkr$dndfd	d
„Zdd„Zd
d„Zdd„Z	dS)Ú	FileInputz5
    Input for single, simple file-like objects.
    NrTrrÚrZrUc	Cstj|||||ƒ||_tƒ|_|dkrž|r”tjdkrF|j|jdœ}ni}yt	||f|Ž|_
Wqœtk
r}zt|j
|j|ƒ‚WYdd}~XqœXqÐtj|_
n2tjdkrÐt|j
|jƒdkrÐtd|j|j
jfƒ‚|süy|j
j|_Wntk
rúYnXdS)	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.
        Nrr)rÚerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr)rr)rr Ú	autocloserÚ_stderrÚsysÚversion_inforrÚopenrÚIOErrorrÚerrnoÚstrerrorÚstdinrr2Únamerr)	rrrrrrVÚmodeÚkwargsr9rrr
r Ðs.
$

zFileInput.__init__cCsÌz°yD|jtjkr:tjdkr:|jjjƒ}dj|jƒƒd}n
|jjƒ}Wnftt	fk
r¬}zF|j
rš|jršt|jdƒ}|jƒ}|j
ƒdj|jƒƒd}n‚WYdd}~XnXWd|jrÀ|j
ƒX|j|ƒS)zU
        Read and decode a single file and return the data (Unicode string).
        rró
ÚrbN)rr)rrXr^rYÚbufferr$r3r?r2rrrrZÚcloserVr:)rr4r8Zb_sourcerrr
r$ÿs
zFileInput.readcCs|jƒjdƒS)zK
        Return lines of a single file as list of Unicode strings.
        T)r$r?)rrrr
Ú	readlinesszFileInput.readlinescCs|jtjk	r|jjƒdS)N)rrXr^re)rrrr
re szFileInput.close)rr)
r	r
rrErXrYr r$rfrerrrr
rSËs,rSc@s6eZdZdZdZddd„Zdd	„Zd
d„Zdd
„ZdS)Ú
FileOutputz6
    Output for single, simple file-like objects.
    ÚwNrTcCs´tj|||||ƒd|_||_|dk	r,||_tƒ|_|dkrR|rHd|_qˆtj|_	n6|rˆt
|j	dƒrˆ||j	jkrˆtd|j	j|f|jd|s°y|j	j|_
Wntk
r®YnXdS)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.
        TNFr`z?Warning: Destination mode "%s" differs from specified mode "%s")Úfile)rLr ÚopenedrVr`rrWrXÚstdoutrNÚhasattrÚprintr_rOr)rrNrOrrrVZhandle_io_errorsr`rrr
r 1s*


zFileOutput.__init__cCs€tjdkr$d|jkr$|j|jdœ}ni}yt|j|jf|Ž|_Wn4tk
rt}zt	|j
|j|jƒ‚WYdd}~XnXd|_dS)NrrÚb)rrUT)rr)
rXrYr`rrrZrOrNr[rr\r]rj)rrar9rrr
rZ\s
zFileOutput.opencCsl|js|jƒd|jkr"tjdks4t|j|jƒdkrf|j|ƒ}tjd
krft	j
dkrf|jdtt	j
dƒƒ}zîy|jj
|ƒWnØtk
r}z~tjdkožt|tƒry|jjj
|ƒWnNtk
rt|j|jƒdkrútd|jpæd	|jj|jfƒ‚n|‚YnXWYd
d
}~Xn>ttfk
rP}ztd|jt|ƒfƒ‚WYd
d
}~XnXWd
|jrf|jƒ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.
        rnrrFÚ
rbr<z;Encoding of %s (%s) differs 
  from specified encoding (%s)rNNz:Unable to encode output data. output-encoding is: %s.
(%s))rr)rr)rr)rjrZr`rXrYrrNrrRÚosÚlinesepr1ÚbytesrQrr-rdrÚ
ValueErrorrOr2rrrVre)rr4r8rrr
rQjs6
(
zFileOutput.writecCs&|jtjtjfkr"|jjƒd|_dS)NF)rNrXrkÚstderrrerj)rrrr
re‘s
zFileOutput.close)NNNrTNN)	r	r
rrEr`r rZrQrerrrr
rg%s
)'rgc@seZdZdZdZdS)ÚBinaryFileOutputzL
    A version of docutils.io.FileOutput which writes to a binary file.
    ÚwbN)r	r
rrEr`rrrr
ru—sruc@seZdZdZdZdd„ZdS)ÚStringInputz
    Direct string input.
    z<string>cCs|j|jƒS)z$Decode and return the source string.)r:r)rrrr
r$¨szStringInput.readN)r	r
rrErr$rrrr
rw srwc@seZdZdZdZdd„ZdS)ÚStringOutputz
    Direct string output.
    z<string>cCs|j|ƒ|_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rRrN)rr4rrr
rQµszStringOutput.writeN)r	r
rrErPrQrrrr
rx­srxc@seZdZdZdZdd„ZdS)Ú	NullInputz)
    Degenerate input: read nothing.
    z
null inputcCsdS)zReturn a null string.r'r)rrrr
r$ÃszNullInput.readN)r	r
rrErr$rrrr
ry»sryc@seZdZdZdZdd„ZdS)Ú
NullOutputz+
    Degenerate output: write nothing.
    znull outputcCsdS)z6Do nothing ([don't even] send data to the bit bucket).Nr)rr4rrr
rQÐszNullOutput.writeN)r	r
rrErPrQrrrr
rzÈsrzc@seZdZdZdZdd„ZdS)ÚDocTreeInputzm
    Adapter for document tree input.

    The document tree must be passed in the ``source`` parameter.
    z
doctree inputcCs|jS)zReturn the document tree.)r)rrrr
r$ßszDocTreeInput.readN)r	r
rrErr$rrrr
r{Õsr{)rr)rEÚ
__future__rZ
__docformat__rXrprGrZdocutilsrZdocutils.utils.error_reportingrrrrYÚstrr%r[rrrrrLrSrgrurwrxryrzr{rrrr
Ú<module>s.
p0Zr