B 0`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 f/private/var/folders/sf/wxz_36012wxg_prv29r6623x5vlqx8/T/pip-target-tft40_m7/lib/python/docutils/io.pyrsrc@s eZdZdS) OutputErrorN)rrr r r r r r sr c 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 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|_dS)N)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__rr)rr r r __repr__Es zInput.__repr__cCstdS)N)NotImplementedError)rr r r readIsz 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 ysz Input.decode..)rlower isinstancestrAssertionErrordetermine_encoding_from_datarinsertrrreplace UnicodeErrorrjoinr)rdata encodings data_encodingr(decodederrerrorr r r decodeLs0        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_slugsearchgroupr9)rr3Z start_bytesrlinematchr r r r.s  z"Input.determine_encoding_from_data)NNNr)rrr __doc__component_typerrr r"r9recompiler?r BOM_UTF8 BOM_UTF16_BE BOM_UTF16_LEr<r.r 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)rrMrNrrr r r rs  zOutput.__init__cCsd|j|j|jfS)Nz'%s: destination=%r, destination_path=%r)rrMrN)rr r r r szOutput.__repr__cCstdS)z;`data` is a Unicode string, to be encoded by `self.encode`.N)r!)rr3r 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*r+r,r-encoder)rr3r r r rQs  z Output.encode)NNNr) rrr rDrErOrr rPrQr r r r rKs rKc@sLeZdZdZdddddejdkr$dndfdd Zd d Zd d ZddZ dS) FileInputz5 Input for single, simple file-like objects. NrT)rZrUc Ks4t|||||||_t|_x.|D]&}|dkr@tjdq&td|q&W|dkr|rtj dkrv|j |j d}ni}yt ||f||_ Wqtk r} zt| j| j|Wdd} ~ XYqXntj|_ n6tj dkrt|j |j dkrtd|j |j j f|s0y|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'N)rSr)rerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr autocloser_stderrsysstderrrPr version_inforropenrIOErrorrerrnostrerrorstdinrr1namerr) rrrrrrXmodekwargskeyr8r r r rs8    $  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). )rSr rbN)rrZrar\bufferr"r2r>r1rrrr]closerXr9)rr3r7Zb_sourcer r r r"s    zFileInput.readcCs|dS)zK Return lines of a single file as list of Unicode strings. T)r"r>)rr r r readlines!szFileInput.readlinescCs|jtjk r|jdS)N)rrZrari)rr r r ri's zFileInput.close) rrr rDrZr\rr"rjrir r r r rRs7rRc@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. TNFrcz?Warning: Destination mode "%s" differs from specified mode "%s")file)rKropenedrXrcrrYrZstdoutrMhasattrprintrbrNr)rrMrNrrrXrVrcr r r r8s(   zFileOutput.__init__c Cstjdkr$d|jkr$|j|jd}ni}yt|j|jf||_Wn4tk rt}zt |j |j |jWdd}~XYnXd|_ dS)N)rSrb)rrWT) rZr\rcrrr]rNrMr^r r_r`rn)rrdr8r r r r]cs  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. rr)rSrF rfr;z;Encoding of %s (%s) differs from specified encoding (%s)rMNz:Unable to encode output data. output-encoding is: %s. (%s))rnr]rcrZr\rrMrrQoslinesepr0bytesrPrr+rhr ValueErrorrNr1rrrXri)rr3er7r r r rPqs6  ( zFileOutput.writecCs&|jtjtjfkr"|jd|_dS)NF)rMrZror[rirn)rr r r ris zFileOutput.close)NNNrTNN) rrr rDrcrr]rPrir r r r rk,s )'rkc@seZdZdZdZdS)BinaryFileOutputzL A version of docutils.io.FileOutput which writes to a binary file. wbN)rrr rDrcr r r r rysryc@seZdZdZdZddZdS) StringInputz Direct string input. zcCs ||jS)z$Decode and return the source string.)r9r)rr r r r"szStringInput.readN)rrr rDrr"r r r r r{sr{c@seZdZdZdZddZdS) StringOutputz Direct string output. zcCs|||_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rQrM)rr3r r r rPs zStringOutput.writeN)rrr rDrOrPr r r r r|sr|c@seZdZdZdZddZdS) NullInputz) Degenerate input: read nothing. z null inputcCsdS)zReturn a null string.r%r )rr r r r"szNullInput.readN)rrr rDrr"r r r r r}sr}c@seZdZdZdZddZdS) NullOutputz+ Degenerate output: write nothing. z null outputcCsdS)z6Do nothing ([don't even] send data to the bit bucket).Nr )rr3r r r rPszNullOutput.writeN)rrr rDrOrPr r r r r~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)rr r r r"szDocTreeInput.readN)rrr rDrr"r r r r rsr)rDZ __docformat__rZrtrFr ZdocutilsrZdocutils.utils.error_reportingrrrr^rr rrrKrRrkryr{r|r}r~rr r r r s( p0er