3 E\)D@s dZdZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z GdddeZGd d d eZd d ZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd 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. ZreStructuredTextN) TransformSpec)b)locale_encoding ErrorString ErrorOutputc@s eZdZdS) InputErrorN)__name__ __module__ __qualname__r r e/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/docutils/docutils/io.pyrsrc@s eZdZdS) OutputErrorN)rr r r r r r r sr c Cs6ytj|jtj|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@sleZdZdZdZdZdddZddZd d Zd d Z e j e d Z ejdfejdfejdffZddZdS)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__2szInput.__init__cCsd|j|j|jfS)Nz%s: source=%r, source_path=%r) __class__rr)rr r r __repr__Fs zInput.__repr__cCstdS)N)NotImplementedError)rr r r readJsz Input.readcCs|jr&|jjdkr&t|ts&tdt|tr4|S|jrD|jg}n.|j|}|rZ|g}nddg}trr|jdtxX|D]P}y t|||j}||_ |j ddSt t fk r}z |}WYdd}~XqxXqxWt d d j 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 zsz Input.decode..)rlower isinstancestrAssertionErrordetermine_encoding_from_datarinsertrrreplace UnicodeErrorrjoinr)rdata encodings data_encodingr)decodederrerrorr r r decodeMs0        z Input.decodezcoding[:=]\s*([-\w.]+)zutf-8z utf-16-bez utf-16-lecCs`x |jD]\}}|j|r|SqWx8|jddD]$}|jj|}|r4|jdjdSq4WdS)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:)rr4Z start_bytesrlinematchr r r r/s  z"Input.determine_encoding_from_data)NNNr)rr r __doc__component_typerrr!r#r:recompilerr@rBOM_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)rrNrOrrr r r rs  zOutput.__init__cCsd|j|j|jfS)Nz'%s: destination=%r, destination_path=%r)r rNrO)rr r r r!szOutput.__repr__cCstdS)z;`data` is a Unicode string, to be encoded by `self.encode`.N)r")rr4r r r writesz Output.writecCsL|jr*|jjdkr*t|ts&td|St|ts8|S|j|j|jSdS)Nr$zFthe encoding given is "unicode" but the output is not a Unicode string)rr+r,r-r.encoder)rr4r r r rRs  z Output.encode)NNNr) rr r rErFrPrr!rQrRr r r r rLs rLc@s2eZdZdZdddZdd Zd d Zd d ZdS) FileInputz5 Input for single, simple file-like objects. NrTrUc Ks2tj|||||||_t|_x.|D]&}|dkr@tjjdq&td|q&W|dkr|rtj d krv|j |j d}ni}yt ||f||_ Wqtk r} zt| j| j|WYdd} ~ XqXntj|_ n4tj d kot|j |j dkrtd |j |j j f|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. 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'Nr)rerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rVr)rVr)rr autocloser_stderrsysstderrrQr version_inforropenrIOErrorrerrnostrerrorstdinrr2namerr) rrrrrrXmodekwargskeyr9r r r rs8    $  zFileInput.__init__cCszyL|jtjkrBtjdkrB|jjj}tdj|jtd}n |jj}Wnnt t fk r}zN|j r|j rt |j d}|j}|jtdj|jtd}nWYdd}~XnXWd|jr|jX|j|S)zU Read and decode a single file and return the data (Unicode string). rVr rbN)rVr)rrZrar\bufferr#rr3r?r2rrrr]closerXr:)rr4r8Zb_sourcer r r r#s    zFileInput.readcCs|jjdS)zK Return lines of a single file as list of Unicode strings. T)r#r?)rr r r readlines"szFileInput.readlinescCs|jtjk r|jjdS)N)rrZrari)rr r r ri(s zFileInput.close)NNNrTrT)rr r rErr#rjrir r r r rSs 7rSc@s6eZdZdZdZdddZdd Zd d Zd d ZdS) FileOutputz6 Output for single, simple file-like objects. wNrTc Cstj|||||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)rLropenedrXrcrrYrZstdoutrNhasattrprintrbrOr)rrNrOrrrXrUrcr r r r9s(   zFileOutput.__init__cCstjdkr$d|jkr$|j|jd}ni}yt|j|jf||_Wn4tk rt}zt |j |j |jWYdd}~XnXd|_ dS)NrVrr)rrWT)rVr) rZr\rcrrr]rOrNr^r r_r`rn)rrdr9r r r r]ds  zFileOutput.opencCsn|js|jd|jkr"tjd ks4t|j|jdkrh|j|}tjd krht j dkrh|j t dt t j }zy|jj |Wntk r}z~tjd kot|try|jjj |WnNtk rt|j|jdkrtd|jpd|jj|jfn|YnXWYdd}~Xn>ttfk rR}ztd |jt|fWYdd}~XnXWd|jrh|jX|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. rrVrFrfz;Encoding of %s (%s) differs from specified encoding (%s)rNNz:Unable to encode output data. output-encoding is: %s. (%s))rVr)rVr)rVr)rnr]rcrZr\rrNrrRoslinesepr1rrQrr,bytesrhr ValueErrorrOr2rrrXri)rr4er8r r r rQrs8 ( zFileOutput.writecCs&|jtjtjfkr"|jjd|_dS)NF)rNrZror[rirn)rr r r ris zFileOutput.close)NNNrTNN) rr r rErcrr]rQrir r r r rk-s )(rkc@seZdZdZdZdS)BinaryFileOutputzL A version of docutils.io.FileOutput which writes to a binary file. wbN)rr r rErcr r r r rwsrwc@seZdZdZdZddZdS) StringInputz Direct string input. zcCs |j|jS)z$Decode and return the source string.)r:r)rr r r r#szStringInput.readN)rr r rErr#r r r r rysryc@seZdZdZdZddZdS) StringOutputz Direct string output. zcCs|j||_|jS)z=Encode `data`, store it in `self.destination`, and return it.)rRrN)rr4r r r rQs zStringOutput.writeN)rr r rErPrQr r r r rzsrzc@seZdZdZdZddZdS) NullInputz) Degenerate input: read nothing. z null inputcCsdS)zReturn a null string.r&r )rr r r r#szNullInput.readN)rr r rErr#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 )rr4r r r rQszNullOutput.writeN)rr r rErPrQr 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)rr r rErr#r r r r r}sr})rEZ __docformat__rZrrrGrZdocutilsrZdocutils._compatrZdocutils.utils.error_reportingrrrr^rr rrrLrSrkrwryrzr{r|r}r r r r s*  p0es