Qc@sdZdZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z defdYZd efd YZd Zd efd YZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdS( s I/O classes provide a uniform API for low-level input and output. Subclasses exist for a variety of input/output mechanisms. treStructuredTextiN(t TransformSpec(tb(tlocale_encodingt ErrorStringt ErrorOutputt InputErrorcBseZRS((t__name__t __module__(((s//usr/lib/python2.7/site-packages/docutils/io.pyRst OutputErrorcBseZRS((RR(((s//usr/lib/python2.7/site-packages/docutils/io.pyR scCsEy#tj|jtj|kSWntttfk r@dSXdS(sNTest, 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(tcodecstlookuptencodingt LookupErrortAttributeErrort TypeErrortNone(tstreamR ((s//usr/lib/python2.7/site-packages/docutils/io.pytcheck_encodings #tInputcBseZdZdZd Zd d d ddZdZdZdZ e j e dZ ejdfejd fejd ffZd ZRS( s1 Abstract base class for input wrappers. tinputtstrictcCsF||_||_||_||_|s9|j|_nd|_dS(N(R t error_handlertsourcet source_pathtdefault_source_pathRtsuccessful_encoding(tselfRRR R((s//usr/lib/python2.7/site-packages/docutils/io.pyt__init__2s     cCsd|j|j|jfS(Ns%s: source=%r, source_path=%r(t __class__RR(R((s//usr/lib/python2.7/site-packages/docutils/io.pyt__repr__FscCs tdS(N(tNotImplementedError(R((s//usr/lib/python2.7/site-packages/docutils/io.pytreadJscCsT|jr<|jjdkr<t|ts<tdnt|trO|S|jrg|jg}nF|j|}|r|g}n%ddg}tr|jdtnxb|D]Z}y2t|||j}||_ |j ddSWqt t fk r }|}qXqWt dd j g|D]}t|^q%t|fd S( s 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, '') tunicodes=input encoding is "unicode" but input is not a unicode objectsutf-8slatin-1iuusEUnable to decode input data. Tried the following encodings: %s. (%s)s, N(R tlowert isinstanceR!tAssertionErrortdetermine_encoding_from_dataRtinsertRRtreplacet UnicodeErrorR tjointreprR(Rtdatat encodingst data_encodingtenctdecodedterrterror((s//usr/lib/python2.7/site-packages/docutils/io.pytdecodeMs0       %scoding[:=]\s*([-\w.]+)sutf-8s utf-16-bes utf-16-lecCszx*|jD]\}}|j|r |Sq WxF|jd D]4}|jj|}|r>|jdjdSq>WdS(s Try to determine the encoding of `data` by looking *in* `data`. Check for a byte order mark (BOM) or an encoding declaration. iitasciiN(tbyte_order_markst startswitht splitlinest coding_slugtsearchtgroupR2R(RR+t start_bytesR tlinetmatch((s//usr/lib/python2.7/site-packages/docutils/io.pyR%sN(RRt__doc__tcomponent_typeRRRRR R2tretcompileRR7R tBOM_UTF8t BOM_UTF16_BEt BOM_UTF16_LER4R%(((s//usr/lib/python2.7/site-packages/docutils/io.pyR(s     0  tOutputcBsJeZdZdZdZdddddZdZdZdZ RS(s2 Abstract base class for output wrappers. toutputRcCsC||_|pd|_||_||_|s?|j|_ndS(NR(R Rt destinationtdestination_pathtdefault_destination_path(RRFRGR R((s//usr/lib/python2.7/site-packages/docutils/io.pyRs    cCsd|j|j|jfS(Ns'%s: destination=%r, destination_path=%r(RRFRG(R((s//usr/lib/python2.7/site-packages/docutils/io.pyRscCs tdS(s;`data` is a Unicode string, to be encoded by `self.encode`.N(R(RR+((s//usr/lib/python2.7/site-packages/docutils/io.pytwritescCsj|jr=|jjdkr=t|ts9td|St|tsP|S|j|j|jSdS(NR!sFthe encoding given is "unicode" but the output is not a Unicode string(R R"R#R!R$tencodeR(RR+((s//usr/lib/python2.7/site-packages/docutils/io.pyRJs N( RRR=R>RRHRRRIRJ(((s//usr/lib/python2.7/site-packages/docutils/io.pyRDs  t FileInputcBsGeZdZddddedddZdZdZdZRS(s5 Input for single, simple file-like objects. RtrUc CsKtj|||||||_t|_|dkr|rtjdkrli|jd6|j d6}ni}yt ||||_ Wqt k r} t | j| j|qXqtj|_ nLtjdkrt|j |jtkrtd|j|j jfn|sGy|j j|_WqGtk rCqGXndS( s :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). - `handle_io_errors`: ignored, deprecated, will be removed. - `mode`: how the file is to be opened (see standard function `open`). The default 'rU' provides universal newline support for text files. iiR terrorssOEncoding clash: encoding given is "%s" but source is opened with encoding "%s".N(ii(ii(RRt autocloseRt_stderrRtsyst version_infoR RtopenRtIOErrorRterrnotstrerrortstdinRtFalseR(tnameRR( RRRR RRNthandle_io_errorstmodetkwargsR1((s//usr/lib/python2.7/site-packages/docutils/io.pyRs.     cCszyn|jtjkratjdkra|jjj}tdj|jtd}n|jj}Wnt t fk r}|j r|j rt |j d}|j}|jtdj|jtd}qnXWd|jr |jnX|j|S(sU Read and decode a single file and return the data (Unicode string). iis trbN(ii(RRPRVRQtbufferR RR)R6R(R R RRRtcloseRNR2(RR+R0tb_source((s//usr/lib/python2.7/site-packages/docutils/io.pyR s !(  (  cCs|jjtS(sK Return lines of a single file as list of Unicode strings. (R R6tTrue(R((s//usr/lib/python2.7/site-packages/docutils/io.pyt readlinesscCs&|jtjk r"|jjndS(N(RRPRVR^(R((s//usr/lib/python2.7/site-packages/docutils/io.pyR^sN( RRR=RR`RR RaR^(((s//usr/lib/python2.7/site-packages/docutils/io.pyRKs.  t FileOutputcBsMeZdZdZddddedddZdZdZdZ RS(s6 Output for single, simple file-like objects. twRcCstj|||||t|_||_|dk rC||_nt|_|dkr||rmt |_qt j |_ nJ|rt |j dr||j jkr|jd|j j|fIJn|sy|j j|_Wqtk rqXndS(sA :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. RZs?Warning: Destination mode "%s" differs from specified mode "%s"N(RDRR`topenedRNRRZRRORWRPtstdoutRFthasattrRXRGR(RRFRGR RRNRYRZ((s//usr/lib/python2.7/site-packages/docutils/io.pyR0s(          cCstjdkr;d|jkr;i|jd6|jd6}ni}yt|j|j||_Wn.tk r}t |j |j |jnXt |_ dS(NiiRR RM(ii(RPRQRZR RRRRGRFRSR RTRUR`Rd(RR[R1((s//usr/lib/python2.7/site-packages/docutils/io.pyRR[s c Cs|js|jnd|jkr4tjd ksOt|j|jtkrtjd krt j dkr|j dt j }n|j |}nzy|jj |Wntk r^}tjd krt|try|jjj |Wq[tk rWt|j|jtkrNtd|jp2d|jj|jfqX|q[Xqn8ttfk r}td|jt|fnXWd|jr|jnX|S( sEncode `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. Riis s;Encoding of %s (%s) differs from specified encoding (%s)RFs:Unable to encode output data. output-encoding is: %s. (%s)N(ii(ii(ii(RdRRRZRPRQRRFR RWtostlinesepR'RJRIRR#tbytesR]Rt ValueErrorRGR(R RRNR^(RR+teR0((s//usr/lib/python2.7/site-packages/docutils/io.pyRIis8     ! cCs8|jtjtjfkr4|jjt|_ndS(N(RFRPRetstderrR^RWRd(R((s//usr/lib/python2.7/site-packages/docutils/io.pyR^s N( RRR=RZRR`RRRRIR^(((s//usr/lib/python2.7/site-packages/docutils/io.pyRb$s )  (tBinaryFileOutputcBseZdZdZRS(sL A version of docutils.io.FileOutput which writes to a binary file. twb(RRR=RZ(((s//usr/lib/python2.7/site-packages/docutils/io.pyRmst StringInputcBseZdZdZdZRS(s Direct string input. scCs|j|jS(s$Decode and return the source string.(R2R(R((s//usr/lib/python2.7/site-packages/docutils/io.pyR s(RRR=RR (((s//usr/lib/python2.7/site-packages/docutils/io.pyRost StringOutputcBseZdZdZdZRS(s Direct string output. scCs|j||_|jS(s=Encode `data`, store it in `self.destination`, and return it.(RJRF(RR+((s//usr/lib/python2.7/site-packages/docutils/io.pyRIs(RRR=RHRI(((s//usr/lib/python2.7/site-packages/docutils/io.pyRpst NullInputcBseZdZdZdZRS(s) Degenerate input: read nothing. s null inputcCsdS(sReturn a null string.u((R((s//usr/lib/python2.7/site-packages/docutils/io.pyR s(RRR=RR (((s//usr/lib/python2.7/site-packages/docutils/io.pyRqst NullOutputcBseZdZdZdZRS(s+ Degenerate output: write nothing. s null outputcCsdS(s6Do nothing ([don't even] send data to the bit bucket).N((RR+((s//usr/lib/python2.7/site-packages/docutils/io.pyRIs(RRR=RHRI(((s//usr/lib/python2.7/site-packages/docutils/io.pyRrst DocTreeInputcBseZdZdZdZRS(sm Adapter for document tree input. The document tree must be passed in the ``source`` parameter. s doctree inputcCs|jS(sReturn the document tree.(R(R((s//usr/lib/python2.7/site-packages/docutils/io.pyR s(RRR=RR (((s//usr/lib/python2.7/site-packages/docutils/io.pyRss(R=t __docformat__RPRgR?R tdocutilsRtdocutils._compatRtdocutils.utils.error_reportingRRRRSRR RRRDRKRbRmRoRpRqRrRs(((s//usr/lib/python2.7/site-packages/docutils/io.pyts*     p0\s