B \)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 //tmp/pip-build-uw_ogi45/docutils/docutils/io.pyrsrc@s eZdZdS) OutputErrorN)rr r 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@sleZdZdZdZdZdddZddZd d Zd d Z e 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.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 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]\}}||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:)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*|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)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 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. 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)r)rerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr autocloser_stderrsysstderrrQr version_inforropenrIOErrorrerrnostrerrorstdinrr2namerr) rrrrrrXmodekwargskeyr9r r r rs8    $  zFileInput.__init__c CszyL|jtjkrBtjdkrB|jj}td|td}n |j}Wnlt t fk r}zJ|j s|j rt |j d}|}|td|td}nWdd}~XYnXWd|jr|X||S)zU Read and decode a single file and return the data (Unicode string). )rVr rbN)rrZrar\bufferr#rr3r?r2rrrr]closerXr:)rr4r8Zb_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)NNNrTrT)rr r rErr#rjrir r r r rSs 7rSc@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)rLropenedrXrcrrYrZstdoutrNhasattrprintrbrOr)rrNrOrrrXrUrcr r r r9s(   zFileOutput.__init__c Cstjdkr$d|jkr$|j|jd}ni}yt|j|jf||_Wn4tk rt}zt |j |j |jWdd}~XYnXd|_ dS)N)rVrr)rrWT) rZr\rcrrr]rOrNr^r r_r`rn)rrdr9r r r r]ds  zFileOutput.openc Csn|js|d|jkr"tjdks4t|j|jdkrh||}tjdkrht j dkrh| t dt t j }zy|j |Wntk r}z|tjdkrt|try|jj |WnLtk rt|j|jdkrtd|jpd|jj|jfn|YnXWdd}~XYn>ttfk rR}ztd|jt|fWdd}~XYnXWd|jrh|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. r)rVrFrfz;Encoding of %s (%s) differs from specified encoding (%s)rNNz:Unable to encode output data. output-encoding is: %s. (%s))rnr]rcrZr\rrNrrRoslinesepr1rrQrr,bytesrhr ValueErrorrOr2rrrXri)rr4er8r r r rQrs8  ( zFileOutput.writecCs&|jtjtjfkr"|jd|_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 ||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|||_|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