U C^C@sdZdZddlZddlZddlZddlZddlmZddlm Z m Z m Z Gddde Z Gdd d e Zd d ZGd d d eZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZdS) z I/O classes provide a uniform API for low-level input and output. Subclasses exist for a variety of input/output mechanisms. ZreStructuredTextN) TransformSpec)locale_encoding ErrorString ErrorOutputc@s eZdZdS) InputErrorN__name__ __module__ __qualname__r r 1/tmp/pip-install-6_kvzl1k/docutils/docutils/io.pyrsrc@s eZdZdS) OutputErrorNrr r r r r sr c Cs:zt|jt|kWStttfk r4YdSXdS)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 r check_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|_dSN)r error_handlersource source_pathdefault_source_pathsuccessful_encoding)selfrrrrr r r __init__1szInput.__init__cCsd|j|j|jfS)Nz%s: source=%r, source_path=%r) __class__rrrr r r __repr__Es zInput.__repr__cCstdSrNotImplementedErrorr"r r r readIsz Input.readc Cs|jr&|jdkr&t|ts&tdt|tr4|S|jrD|jg}n.||}|rZ|g}nddg}trr|dt|D]V}z&t|||j}||_ | ddWSt t fk r}z|}W5d}~XYqvXqvt 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 objectutf-8zlatin-1uNzEUnable to decode input data. Tried the following encodings: %s. (%s)z, cSsg|] }t|qSr )repr).0encr r r ysz Input.decode..)rlower isinstancestrAssertionErrordetermine_encoding_from_datarinsertrrreplace UnicodeErrorrjoinr)rdata encodings data_encodingr-decodederrerrorr r r decodeLs8      z Input.decodescoding[:=]\s*([-\w.]+)r(z utf-16-bez utf-16-lecCs`|jD]\}}||r|Sq|ddD](}|j|}|r2|ddSq2dS)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>)rr8Z start_bytesrlinematchr r r r3s   z"Input.determine_encoding_from_data)NNNr)rr r __doc__component_typerr r#r&r>recompilerDrBOM_UTF8 BOM_UTF16_BE BOM_UTF16_LErAr3r r r r r'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)rrRrSrrr r r r s  zOutput.__init__cCsd|j|j|jfS)Nz'%s: destination=%r, destination_path=%r)r!rRrSr"r r r r#szOutput.__repr__cCstdS)z;`data` is a Unicode string, to be encoded by `self.encode`.Nr$rr8r r r writesz 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/r0r1r2encoderrUr r r rWs  z Output.encode)NNNr) rr r rIrJrTr r#rVrWr r r r rPs rPc@sLeZdZdZdddddejdkr$dndfdd Zd d Zd d ZddZ dS) FileInputz5 Input for single, simple file-like objects. NrT)rZrUc Ks,t|||||||_t|_|D]&}|dkr>tjdq$td|q$|dkr|rtj dkrr|j |j d}ni}zt ||f||_ Wqtk r} zt| j| j|W5d} ~ XYqXqtj|_ n2tj dkrt|j |j dkrtd|j |j j f|s(z|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 on Python < 3.4. handle_io_errorszdeprecation warning: io.FileInput() argument `handle_io_errors` is ignored since Docutils 0.10 (2012-12-16) and will soon be removed.z2__init__() got an unexpected keyword argument '%s'NrYrrerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr autocloser_stderrsysstderrrVr version_inforropenrIOErrorrerrnostrerrorstdinrr6namerr) rrrrrr`modekwargskeyr=r r r r s@ $   zFileInput.__init__c CszzD|jtjkr:tjdkr:|jj}d| d}n |j}Wndt t fk r}zB|j s|j rt|j d}|}|d| d}nW5d}~XYnXW5|jr|X||S)zU Read and decode a single file and return the data (Unicode string). r] rbN)r`closerrbrirdbufferr&r7rCr6rrrrer>)rr8r<Zb_sourcer r r r&s    zFileInput.readcCs|dS)zK Return lines of a single file as list of Unicode strings. T)r&rCr"r r r readlines!szFileInput.readlinescCs|jtjk r|jdSr)rrbrirpr"r r r rp's zFileInput.close) rr r rIrbrdr r&rrrpr r r r rXs :rXc@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|sz|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. TNFrkz?Warning: Destination mode "%s" differs from specified mode "%s")file)rPr openedr`rkrrarbstdoutrRhasattrprintrjrSr)rrRrSrrr`r\rkr r r r 8s:     zFileOutput.__init__c Cstjdkr$d|jkr$|j|jd}ni}zt|j|jf||_Wn4tk rt}zt |j |j |jW5d}~XYnXd|_ dS)Nr]br^T) rbrdrkrrrerSrRrfr rgrhrv)rrlr=r r r recs zFileOutput.openc Csf|js|d|jkr"tjdks4t|j|jdkrf||}tjdkrft j dkrf| dt t j d}zz|j|Wntk r}zvtjdkrt|t rz|jj|WnJtk rt|j|jdkrtd|jpd|jj|jfn|YnXW5d }~XYn>ttfk rJ}ztd |jt|fW5d }~XYnXW5|j r`|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. rzr]F rnr@z;Encoding of %s (%s) differs from specified encoding (%s)rRNz:Unable to encode output data. output-encoding is: %s. (%s))rvrerkrbrdrrRrrWoslinesepr5bytesr`rprVrr0rqr ValueErrorrSr6rr)rr8er<r r r rVqsH   zFileOutput.writecCs&|jtjtjfkr"|jd|_dS)NF)rRrbrwrcrprvr"r r r rps zFileOutput.close)NNNrTNN) rr r rIrkr rerVrpr r r r rs,s +'rsc@seZdZdZdZdS)BinaryFileOutputzL A version of docutils.io.FileOutput which writes to a binary file. wbN)rr r rIrkr r r r rsrc@seZdZdZdZddZdS) StringInputz Direct string input. cCs ||jS)z$Decode and return the source string.)r>rr"r r r r&szStringInput.readNrr r rIrr&r r r r rsrc@seZdZdZdZddZdS) StringOutputz Direct string output. rcCs|||_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rWrRrUr r r rVs zStringOutput.writeNrr r rIrTrVr r r r rsrc@seZdZdZdZddZdS) NullInputz) Degenerate input: read nothing. z null inputcCsdS)zReturn a null string.r*r r"r r r r&szNullInput.readNrr r r r rsrc@seZdZdZdZddZdS) NullOutputz+ Degenerate output: write nothing. z null outputcCsdS)z6Do nothing ([don't even] send data to the bit bucket).Nr rUr r r rVszNullOutput.writeNrr r r r rsrc@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.)rr"r r r r&szDocTreeInput.readNrr r r r rsr)rIZ __docformat__rbr|rKrZdocutilsrZdocutils.utils.error_reportingrrrrfrr rrrPrXrsrrrrrrr r r r s( p0er