B d}A@s.dZddlmZdZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ejdkrbeZGdd d eZGd d d eZd d ZGddde ZGddde ZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGd d!d!eZdS)"z I/O classes provide a uniform API for low-level input and output. Subclasses exist for a variety of input/output mechanisms. )print_functionreStructuredTextN) 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|jt|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)codecslookupencoding LookupErrorAttributeError TypeError)streamrr r rcheck_encodings rc@sheZdZdZdZdZdddZddZd d Zd d Z e d Z e jdfe jdfe jdffZddZdS)Inputz1 Abstract base class for input wrappers. inputNstrictcCs.||_||_||_||_|s$|j|_d|_dS)N)r error_handlersource source_pathdefault_source_pathsuccessful_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__cCstdS)N)NotImplementedError)r r r rreadMsz Input.readc Cs|jr&|jdkr&t|ts&tdt|tr4|S|jrD|jg}n.||}|rZ|g}nddg}trr|dtxX|D]P}y t|||j}||_ | ddSt t fk r}z|}Wdd}~XYqxXqxWt d d d d |Dt|fdS) 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-1uNzEUnable to decode input data. Tried the following encodings: %s. (%s)z, cSsg|] }t|qSr )repr).0encr r r }sz Input.decode..)rlower isinstancer&AssertionErrordetermine_encoding_from_datarinsertrrreplace UnicodeErrorrjoinr)r data encodings data_encodingr+decodederrerrorr r rdecodePs0        z Input.decodescoding[:=]\s*([-\w.]+)zutf-8z utf-16-bez utf-16-lecCs`x |jD]\}}||r|SqWx8|ddD]$}|j|}|r4|ddSq4WdS)z Try to determine the encoding of `data` by looking *in* `data`. Check for a byte order mark (BOM) or an encoding declaration. Nr'ascii)byte_order_marks startswith splitlines coding_slugsearchgroupr;)r r5Z start_bytesrlinematchr r rr0s  z"Input.determine_encoding_from_data)NNNr)r r r __doc__component_typerr!r#r%r;recompilerArBOM_UTF8 BOM_UTF16_BE BOM_UTF16_LEr>r0r r r rr+s 0  rc@s:eZdZdZdZdZd ddZddZd d Zd d Z dS)Outputz2 Abstract base class for output wrappers. outputNrcCs,||_|p d|_||_||_|s(|j|_dS)Nr)rr destinationdestination_pathdefault_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__cCstdS)z;`data` is a Unicode string, to be encoded by `self.encode`.N)r$)r r5r r rwritesz Output.writecCsL|jr*|jdkr*t|ts&td|St|ts8|S||j|jSdS)Nr&zFthe encoding given is "unicode" but the output is not a Unicode string)rr-r.r&r/encoder)r r5r r rrSs  z Output.encode)NNNr) r r r rFrGrQr!r#rRrSr r r rrMs rMc@sLeZdZdZdddddejdkr$dndfdd Zd d Zd d ZddZ dS) FileInputz5 Input for single, simple file-like objects. NrT)rrrZrUc Cst|||||||_t|_|dkr|rtjdkrF|j|jd}ni}yt ||f||_ Wqt k r}zt |j |j|Wdd}~XYqXqtj|_ n2tjdkrt|j |jdkrtd|j|j jf|sy|j j|_Wntk rYnXdS)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)rerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr! autocloser_stderrsys version_inforropenrIOErrorr errnostrerrorstdinrr3namerr) r rrrrrWmodekwargsr:r r rr!s.  $  zFileInput.__init__c CszyD|jtjkr:tjdkr:|jj}d|d}n |j}Wndtt fk r}zB|j s|j rt |j d}|}| d|d}nWdd}~XYnXWd|jr| X||S)zU Read and decode a single file and return the data (Unicode string). )rr rbN)rrYr_rZbufferr%r4r@r3rrrr[closerWr;)r r5r9Zb_sourcer r rr%s    zFileInput.readcCs|dS)zK Return lines of a single file as list of Unicode strings. T)r%r@)r r r r readlinesszFileInput.readlinescCs|jtjk r|jdS)N)rrYr_rf)r r r rrf s zFileInput.close) r r r rFrYrZr!r%rgrfr r r rrTs,rTc@s6eZdZdZdZdddZdd Zd d Zd d ZdS) FileOutputz6 Output for single, simple file-like objects. wNrTcCst|||||d|_||_|dk r,||_t|_|dkrR|rHd|_qtj|_ n6|rt |j dr||j jkrt d|j j|f|jd|sy|j j |_ Wntk rYnXdS)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!openedrWrarrXrYstdoutrOhasattrprintr`rPr)r rOrPrrrWZhandle_io_errorsrar r rr!1s*     zFileOutput.__init__c Cstjdkr$d|jkr$|j|jd}ni}yt|j|jf||_Wn4tk rt}zt |j |j |jWdd}~XYnXd|_ dS)N)rrb)rrVT) rYrZrarrr[rPrOr\rr]r^rk)r rbr:r r rr[\s  zFileOutput.openc Csf|js|d|jkr"tjdks4t|j|jdkrf||}tjdkrft j dkrf| dt t j d}zy|j |Wntk r}zvtjdkrt|t ry|jj |WnJtk rt|j|jdkrtd|jpd|jj|jfn|YnXWd d }~XYn>ttfk rJ}ztd |jt|fWd d }~XYnXWd |jr`|X|S) 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. 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[rarYrZrrOrrSoslinesepr2bytesrRrr.rer ValueErrorrPr3rrrWrf)r r5er9r r rrRjs6  ( zFileOutput.writecCs&|jtjtjfkr"|jd|_dS)NF)rOrYrlstderrrfrk)r r r rrfs 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 rrwsrwc@seZdZdZdZddZdS) StringInputz Direct string input. zcCs ||jS)z$Decode and return the source string.)r;r)r r r rr%szStringInput.readN)r r r rFrr%r r r rrysryc@seZdZdZdZddZdS) StringOutputz Direct string output. zcCs|||_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rSrO)r r5r r rrRs zStringOutput.writeN)r r r rFrQrRr r r rrzsrzc@seZdZdZdZddZdS) 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@seZdZdZdZddZdS) NullOutputz+ Degenerate output: write nothing. z null outputcCsdS)z6Do nothing ([don't even] send data to the bit bucket).Nr )r r5r r rrRszNullOutput.writeN)r r r rFrQrRr r r rr|sr|c@seZdZdZdZddZdS) 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_reportingrrrrZstrr&r\r rrrrMrTrhrwryrzr{r|r}r r r rs.   p0Zr