B Ðñ¢d}Aã@s.dZddlmZdZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ejdkrbeZGdd „d eƒZGd d „d eƒ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_functionÚreStructuredTextN)Ú TransformSpec)Úlocale_encodingÚ ErrorStringÚ ErrorOutput)érc@s eZdZdS)Ú InputErrorN)Ú__name__Ú __module__Ú __qualname__©r r ú2/tmp/pip-target-jj3kjtpb/lib/python/docutils/io.pyr sr c@s eZdZdS)Ú OutputErrorN)r r r r r r rrsrc Cs6yt |j¡t |¡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 „Zd d „Z e   d ¡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)r r r rÚ__repr__Is zInput.__repr__cCst‚dS)N)ÚNotImplementedError)r r r rÚreadMsz Input.readc Csô|jr&|j ¡dkr&t|tƒs&tdƒ‚t|tƒr4|S|jrD|jg}n.| |¡}|rZ|g}nddg}trr| dt¡xX|D]P}y t|||jƒ}||_ |  dd¡St t fk rÆ}z|}Wdd}~XYqxXqxWt d d   d d „|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ú }sz Input.decode..)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]\}}| |¡r|SqWx8| ¡dd…D]$}|j |¡}|r4| d¡ d¡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;)r r5Z start_bytesrÚlineÚmatchr r rr0Šs  z"Input.determine_encoding_from_data)NNNr)r r r Ú__doc__Úcomponent_typerr!r#r%r;ÚreÚcompilerArÚBOM_UTF8Ú BOM_UTF16_BEÚ BOM_UTF16_LEr>r0r r r rr+s 0  rc@s:eZdZdZdZdZd dd„Zdd„Zd d „Zd d „Z dS)ÚOutputz2 Abstract base class for output wrappers. ÚoutputNrcCs,||_|p d|_||_||_|s(|j|_dS)Nr)rrÚ destinationÚdestination_pathÚdefault_destination_path)r rOrPrrr r rr!¥s  zOutput.__init__cCsd|j|j|jfS)Nz'%s: destination=%r, destination_path=%r)r"rOrP)r r r rr#¶szOutput.__repr__cCst‚dS)z;`data` is a Unicode string, to be encoded by `self.encode`.N)r$)r r5r r rÚwriteºsz Output.writecCsL|jr*|j ¡dkr*t|tƒs&tdƒ‚|St|tƒs8|S| |j|j¡SdS)Nr&zFthe encoding given is "unicode" but the output is not a Unicode string)rr-r.r&r/Úencoder)r r5r r rrS¾s  z Output.encode)NNNr) r r r rFrGrQr!r#rRrSr r r rrM›s rMc@sLeZdZdZdddddejdkr$dndfdd „Zd d „Zd d „Zdd„Z dS)Ú FileInputz5 Input for single, simple file-like objects. NrT)rrÚrZrUc Csüt |||||¡||_tƒ|_|dkrž|r”tjdkrF|j|jdœ}ni}yt ||f|Ž|_ Wqœt k r�}zt |j |j|ƒ‚Wdd}~XYqœ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. N)rr)rÚerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr!Ú autocloserÚ_stderrÚsysÚ version_inforrÚopenrÚIOErrorr ÚerrnoÚstrerrorÚstdinrr3Únamerr) r rrrrrWÚmodeÚkwargsr:r r rr!Ðs.  $  zFileInput.__init__c CsÊz®yD|jtjkr:tjdkr:|jj ¡}d | ¡¡d}n |j ¡}Wndtt fk rª}zB|j s˜|j r˜t |j dƒ}| ¡}|  ¡d | ¡¡d}n‚Wdd}~XYnXWd|jr¾|  ¡X| |¡S)zU Read and decode a single file and return the data (Unicode string). )rró ÚrbN)rrYr_rZÚbufferr%r4r@r3rrrr[ÚcloserWr;)r r5r9Zb_sourcer r rr%ÿs    zFileInput.readcCs| ¡ d¡S)zK Return lines of a single file as list of Unicode strings. T)r%r@)r r r rÚ readlinesszFileInput.readlinescCs|jtjk r|j ¡dS)N)rrYr_rf)r r r rrf s zFileInput.close) r r r rFrYrZr!r%rgrfr r r rrTËs,rTc@s6eZdZdZdZddd„Zdd „Zd d „Zd d „ZdS)Ú FileOutputz6 Output for single, simple file-like objects. ÚwNrTcCs´t |||||¡d|_||_|dk r,||_tƒ|_|dkrR|rHd|_qˆtj|_ n6|rˆt |j dƒrˆ||j jkrˆt d|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. TNFraz?Warning: Destination mode "%s" differs from specified mode "%s")Úfile)rMr!ÚopenedrWrarrXrYÚstdoutrOÚhasattrÚprintr`rPr)r rOrPrrrWZhandle_io_errorsrar r rr!1s*     zFileOutput.__init__c Cs€tjdkr$d|jkr$|j|jdœ}ni}yt|j|jf|Ž|_Wn4tk rt}zt |j |j |jƒ‚Wdd}~XYnXd|_ dS)N)rrÚb)rrVT) rYrZrarrr[rPrOr\rr]r^rk)r rbr:r r rr[\s  zFileOutput.openc Csf|js| ¡d|jkr"tjdks4t|j|jƒdkrf| |¡}tjdkrft j dkrf|  dt t j dƒ¡}zèy|j  |¡WnÒtk �r}zvtjdkrþt|t ƒrþy|jj  |¡WnJtk rüt|j|jƒdkrôtd|jpàd|jj|jfƒ‚n|‚YnXWd d }~XYn>ttfk �rJ}ztd |jt|ƒfƒ‚Wd d }~XYnXWd |j�r`| ¡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. ro)rrFÚ rcr=z;Encoding of %s (%s) differs from specified encoding (%s)rONz:Unable to encode output data. output-encoding is: %s. (%s))rkr[rarYrZrrOrrSÚosÚlinesepr2ÚbytesrRrr.rerÚ ValueErrorrPr3rrrWrf)r r5Úer9r r rrRjs6  ( zFileOutput.writecCs&|jtjtjfkr"|j ¡d|_dS)NF)rOrYrlÚstderrrfrk)r r r rrf‘s zFileOutput.close)NNNrTNN) r r r rFrar!r[rRrfr r r rrh%s )'rhc@seZdZdZdZdS)ÚBinaryFileOutputzL A version of docutils.io.FileOutput which writes to a binary file. ÚwbN)r r r rFrar r r rrw—srwc@seZdZdZdZdd„ZdS)Ú StringInputz Direct string input. zcCs | |j¡S)z$Decode and return the source string.)r;r)r r r rr%¨szStringInput.readN)r r r rFrr%r r r rry sryc@seZdZdZdZdd„ZdS)Ú StringOutputz Direct string output. zcCs| |¡|_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rSrO)r r5r r rrRµs zStringOutput.writeN)r r r rFrQrRr r r rrz­srzc@seZdZdZdZdd„ZdS)Ú NullInputz) Degenerate input: read nothing. z null inputcCsdS)zReturn a null string.r(r )r r r rr%ÃszNullInput.readN)r r r rFrr%r r r rr{»sr{c@seZdZdZdZdd„ZdS)Ú NullOutputz+ Degenerate output: write nothing. z null outputcCsdS)z6Do nothing ([don't even] send data to the bit bucket).Nr )r r5r r rrRÐszNullOutput.writeN)r r r rFrQrRr r r rr|Èsr|c@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)r r r rr%ßszDocTreeInput.readN)r r r rFrr%r r r rr}Õsr})rFÚ __future__rÚ __docformat__rYrqrHrZdocutilsrZdocutils.utils.error_reportingrrrrZÚstrr&r\r rrrrMrTrhrwryrzr{r|r}r r r rÚs.   p0Zr