o !5p`A@s.dZddlmZdZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ejdkr1eZGdd 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 ErrorOutputrc@ eZdZdS) InputErrorN__name__ __module__ __qualname__rr-/usr/lib/python3/dist-packages/docutils/io.pyr  r c@r ) OutputErrorNr rrrrrrrc Cs4z t|jt|kWStttfyYdSw)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)streamrrrrcheck_encodings  rc@speZdZdZdZdZ  dddZddZd d Zd d Z e d Z e jdfe jdfe jdffZ ddZdS)Inputz1 Abstract base class for input wrappers. inputNstrictcCs6||_ ||_ ||_ ||_ |s|j|_d|_dSN)r error_handlersource source_pathdefault_source_pathsuccessful_encoding)selfr!r"rr rrr__init__5szInput.__init__cCd|j|j|jfS)Nz%s: source=%r, source_path=%r) __class__r!r"r%rrr__repr__Is zInput.__repr__cCtrNotImplementedErrorr)rrrreadMsz Input.readc Cs|jr|jdkrt|tsJdt|tr|S|jr"|jg}n||}|r-|g}n ddg}tr9|dt|D]+}zt|||j}||_| ddWSt t fyf}z|}WYd}~q;d}~wwt d d d d |Dt |f) 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).0encrrr }sz Input.decode..)rlower isinstancer/determine_encoding_from_datarinsertr r$replace UnicodeErrorrjoinr)r%data encodings data_encodingr5decodederrerrorrrrdecodePs<     z Input.decodescoding[:=]\s*([-\w.]+)r0z utf-16-bez utf-16-lecCs`|jD] \}}||r|Sq|ddD]}|j|}|r-|ddSqdS)z Try to determine the encoding of `data` by looking *in* `data`. Check for a byte order mark (BOM) or an encoding declaration. Nr1ascii)byte_order_marks startswith splitlines coding_slugsearchgrouprD)r%r> start_bytesrlinematchrrrr9s  z"Input.determine_encoding_from_dataNNNr)r rr__doc__component_typer#r&r*r.rDrecompilerJrBOM_UTF8 BOM_UTF16_BE BOM_UTF16_LErGr9rrrrr+s$  0 rc@s>eZdZdZdZdZ  d ddZddZd d Zd d Z dS)Outputz2 Abstract base class for output wrappers. outputNrcCs8||_ |pd|_ ||_ ||_ |s|j|_dSdS)Nr)rr destinationdestination_pathdefault_destination_path)r%rZr[rr rrrr&s  zOutput.__init__cCr')Nz'%s: destination=%r, destination_path=%r)r(rZr[r)rrrr*szOutput.__repr__cCr+)z;`data` is a Unicode string, to be encoded by `self.encode`.r,r%r>rrrwritez Output.writecCsH|jr|jdkrt|tsJd|St|ts|S||j|jS)Nr/zFthe encoding given is "unicode" but the output is not a Unicode string)rr7r8r/encoder r]rrrr`s  z Output.encoderP) r rrrQrRr\r&r*r^r`rrrrrXs  rXc@sLeZdZdZdddddejdkrdndfdd Zd d Zd d ZddZ dS) FileInputz5 Input for single, simple file-like objects. NrTrrrUc Cst|||||||_t|_|durK|rFtjdkr#|j|jd}ni}z t ||fi||_ Wn2t yE}zt |j |j|d}~wwtj|_ ntjdkrdt|j |jdurdtd|j|j jf|syz|j j|_WdStyxYdSwdS)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. NrrerrorsFzOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".)rr& autocloser_stderrsys version_inforr openr!IOErrorr errnostrerrorstdinrr<namer"r) r%r!r"rr rfmodekwargsrCrrrr&s:     zFileInput.__init__c Csz_z"|jtjurtjdkr|jj}d|d}n|j}Wn3tt fyV}z%|j sK|j rKt |j d}|}| d|d}nWYd}~nd}~wwW|jr_| n |jrh| ww||S)zU Read and decode a single file and return the data (Unicode string). r rbN)r!rhrnribufferr.r=rIr<rrr"rjcloserfrD)r%r>rBb_sourcerrrr.s0        zFileInput.readcCs|dS)zK Return lines of a single file as list of Unicode strings. T)r.rIr)rrr readlinesszFileInput.readlinescCs|jtjur |jdSdSr)r!rhrnrur)rrrru s zFileInput.close) r rrrQrhrir&r.rwrurrrrras / rac@s>eZdZdZdZ   dddZdd Zd d Zd d ZdS) FileOutputz6 Output for single, simple file-like objects. wNrTcCst|||||d|_||_|dur||_t|_|dur)|r$d|_n tj|_ n|rDt |j drD||j jkrDt d|j j|f|jd|sYz|j j |_ WdStyXYdSwdS)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. TNFrpz?Warning: Destination mode "%s" differs from specified mode "%s")file)rXr&openedrfrprrgrhstdoutrZhasattrprintror[r)r%rZr[rr rfhandle_io_errorsrprrrr&1s:      zFileOutput.__init__c Csztjdkrd|jvr|j|jd}ni}zt|j|jfi||_Wnty7}z t |j |j |jd}~wwd|_ dS)NrbrdT) rhrirprr rjr[rZrkrrlrmr{)r%rqrCrrrrj\s  zFileOutput.openc Cs^|js|d|jvrtjdkst|j|jdur3||}tjdkr3t j dkr3| dt t j d}zrz|j |Wn_ty}z;tjdkryt|t ryz |jj |Wn!tyxt|j|jdurvtd|jpmd|jj|jf|wWYd }~nd }~wttfy}z td |jt|fd }~wwW|jr||S|jr|ww) 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. rrF rrrFz;Encoding of %s (%s) differs from specified encoding (%s)rZNz:Unable to encode output data. output-encoding is: %s. (%s))r{rjrprhrirrZrr`oslinesepr;bytesr^rr8rtr ValueErrorr[r<rrrfru)r%r>rBrrrr^jsT      zFileOutput.writecCs*|jtjtjfvr|jd|_dSdS)NF)rZrhr|stderrrur{r)rrrrus  zFileOutput.close)NNNrTNN) r rrrQrpr&rjr^rurrrrrx%s + 'rxc@seZdZdZdZdS)BinaryFileOutputzL A version of docutils.io.FileOutput which writes to a binary file. wbN)r rrrQrprrrrrsrc@eZdZdZdZddZdS) StringInputz Direct string input. cCs ||jS)z$Decode and return the source string.)rDr!r)rrrr.s zStringInput.readNr rrrQr#r.rrrrr rc@r) StringOutputz Direct string output. rcCs|||_|jS)z=Encode `data`, store it in `self.destination`, and return it.)r`rZr]rrrr^s zStringOutput.writeNr rrrQr\r^rrrrrrrc@r) NullInputz) Degenerate input: read nothing. z null inputcCdS)zReturn a null string.r2rr)rrrr.r_zNullInput.readNrrrrrrrrc@r) NullOutputz+ Degenerate output: write nothing. z null outputcCr)z6Do nothing ([don't even] send data to the bit bucket).Nrr]rrrr^r_zNullOutput.writeNrrrrrrrrc@r) 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)rrrr.szDocTreeInput.readNrrrrrrs r)rQ __future__r __docformat__rhrrSrdocutilsrdocutils.utils.error_reportingrrrristrr/rkr rrrrXrarxrrrrrrrrrrs0   p0Zr