tHc@s{dZddklZddklZyeWn#ej oddklZnXddk Z ddk Z ddk l Z ddk lZlZlZlZddk lZlZlZlZlZlZlZlZlZlZlZd d d d d dgZdZdZd e fdYZ!d e"fdYZ#dZ$d e i e"fdYZ ddZ%dZ&dS(sOSupport for constructing markup streams from files, strings, or other sources. i(tchain(texpat(t ImmutableSetN(tStringIO(tAttrstQNametStreamt stripentities( tSTARTtENDtXML_DECLtDOCTYPEtTEXTtSTART_NStEND_NSt START_CDATAt END_CDATAtPItCOMMENTtETt ParseErrort XMLParsertXMLt HTMLParsertHTMLsrestructuredtext enc cst|iid}tg}|iD](\}}|t|id|fq,~}t||fdfV|iot|idfVnx0|i D]"}xt |D] }|VqWqWt |dfV|i ot|i dfVndS(sConvert a given ElementTree element to a markup stream. :param element: an ElementTree element :return: a markup stream t{iN(Nii(Nii(Nii(Nii( RttagtlstripRtitemsRtNonettextR t getchildrenRR ttail(telementttag_namet_[1]tattrtvaluetattrstchildtitem((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR#s >     cBs eZdZddddZRS(sWException raised when fatal syntax errors are found in the input being parsed. icCsX||_|o|d|7}nti|||pd|_||_||_dS(s:Exception initializer. :param message: the error message from the parser :param filename: the path to the file that was parsed :param lineno: the number of the line on which the error was encountered :param offset: the column number where the error was encountered s, in sN(tmsgt Exceptiont__init__tfilenametlinenotoffset(tselftmessageR,R-R.((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR+=s  N(t__name__t __module__t__doc__RR+(((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR8scBseZdZgZeiiD]\ZZedeefq [Z di e Z dddZ dZdZdZdddZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZRS(sGenerator-based XML parser based on roughly equivalent code in Kid/ElementTree. The parsing is initiated by iterating over the parser object: >>> parser = XMLParser(StringIO('Foo')) >>> for kind, data, pos in parser: ... print kind, data START (QName(u'root'), Attrs([(QName(u'id'), u'2')])) START (QName(u'child'), Attrs()) TEXT Foo END child END root ss cCs+||_||_ti|d}t|_t|_t|_|i|_ |i |_ |i |_ |i|_|i|_|i|_|i|_|i|_|i|_|i|_|i|_|i|_|i ti!|i"|i#|_$t%|dp|i&|_'n||_g|_(dS(sInitialize the parser for the given XML input. :param source: the XML text as a file-like object :param filename: the name of the file, if appropriate :param encoding: the encoding of the file; if not specified, the encoding is assumed to be ASCII, UTF-8, or UTF-16, or whatever the encoding specified in the XML declaration (if any) t}tCurrentLineNumberN()tsourceR,Rt ParserCreatetTruet buffer_texttreturns_unicodetordered_attributest _handle_starttStartElementHandlert _handle_endtEndElementHandlert _handle_datatCharacterDataHandlert_handle_doctypetStartDoctypeDeclHandlert_handle_start_nstStartNamespaceDeclHandlert_handle_end_nstEndNamespaceDeclHandlert_handle_start_cdatatStartCdataSectionHandlert_handle_end_cdatatEndCdataSectionHandlert _handle_pitProcessingInstructionHandlert_handle_xml_decltXmlDeclHandlert_handle_commenttCommentHandlert _handle_othertDefaultHandlertSetParamEntityParsingtXML_PARAM_ENTITY_PARSING_ALWAYSt UseForeignDTDt_build_foreigntExternalEntityRefHandlerthasattrt_getpos_unknownt_getpost_queue(R/R6R,tencodingtparser((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR+bs2                    cs%fd}t|itS(sGenerator that parses the XML source, yielding markup events. :return: a markup event stream :raises ParseError: if the XML text is not well formed c3s?yd}t}xx| otidjoii|}|djo7tdoiidt`nt}qt |t o|i d}nii|tqWxiD] }|VqWg_|oPqqWnBti j o3}t |}t|i|i|inXdS(NiiitRsutf-8i(tFalsetlenR\R6treadRYRtParseR8t isinstancetunicodetencodet ExpatErrortstrRR,R-R.(tbufsizetdonetdatateventteR)(R/(s2/usr/lib64/python2.6/site-packages/genshi/input.pyt _generates0        (Rtfiltert _coalesce(R/Rn((R/s2/usr/lib64/python2.6/site-packages/genshi/input.pytparsescCst|iS(N(titerRq(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyt__iter__scCs,|ii|}|it|idS(Ni(RtExternalEntityParserCreatet ParseFileRt _external_dtd(R/tcontexttbasetsysidtpubidR^((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRWscCs|djo|i}n|tjopd|jo.|i}|dt|d}d}n|d}|dt|}|d||f}n|ii|||fdS(Ns iiii(RR[R t splitlinesRaR\tappend(R/tkindRktpostlinesR-R.((s2/usr/lib64/python2.6/site-packages/genshi/input.pyt_enqueues      cCs|iddfS(Ni(R,(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRZscCs|i|ii|iifS(N(R,RR5tCurrentColumnNumber(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR[scCsltg}tt|gdD]\}}|t||fq!~}|itt||fdS(Ni(RtzipRrRRR(R/RtattribR#tnameR%R&((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR<s BcCs|itt|dS(N(RR R(R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR>scCs|it|dS(N(RR (R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR@scCs|it|||fdS(N(RR (R/tversionR]t standalone((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRNscCs|it|||fdS(N(RR (R/RRyRzthas_internal_subset((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRBscCs!|it|pd|fdS(NR_(RR (R/tprefixturi((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRDscCs|it|pddS(NR_(RR(R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRFscCs|itdS(N(RR(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRHscCs|itdS(N(RR(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRJscCs|it||fdS(N(RR(R/ttargetRk((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRLscCs|it|dS(N(RR(R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRPscCs|idoy.tti|dd!}|it|Wqtj o_|i\}}}ti d|||f}ti i |_ ||_ ||_|qXndS(Nt&iis)undefined entity "%s": line %d, column %d(t startswithtunichrthtmlentitydefstname2codepointRR tKeyErrorR[RterrorterrorstXML_ERROR_UNDEFINED_ENTITYtcodeR-R.(R/RR,R-R.R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRRs   N( R1R2R3R#RRRRR%t _entitydefstjoinRvRR+RqRsRWRRZR[R<R>R@RNRBRDRFRHRJRLRPRR(((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRNs.3-               cCstttt|S(s<Parse the given XML source and return a markup stream. Unlike with `XMLParser`, the returned stream is reusable, meaning it can be iterated over multiple times: >>> xml = XML('FooBar') >>> print xml FooBar >>> print xml.select('elem') FooBar >>> print xml.select('elem/text()') FooBar :param text: the XML source :return: the parsed XML event stream :raises ParseError: if the XML text is not well-formed (RtlistRR(R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRscBseZdZeddddddddd d d d d g ZdddZdZdZddZ dZ dZ dZ dZ dZdZdZdZRS(s*Parser for HTML input based on the Python `HTMLParser` module. This class provides the same interface for generating stream events as `XMLParser`, and attempts to automatically balance tags. The parsing is initiated by iterating over the parser object: >>> parser = HTMLParser(StringIO('')) >>> for kind, data, pos in parser: ... print kind, data START (QName(u'ul'), Attrs([(QName(u'compact'), u'compact')])) START (QName(u'li'), Attrs()) TEXT Foo END li END ul tareaRxtbasefonttbrtcoltframethrtimgtinputtisindextlinktmetatparamsutf-8cCsAtii|||_||_||_g|_g|_dS(sInitialize the parser for the given HTML input. :param source: the HTML text as a file-like object :param filename: the name of the file, if known :param filename: encoding of the file; ignored if the input is unicode N(thtmlRR+R6R,R]R\t _open_tags(R/R6R,R]((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR+/s     cs%fd}t|itS(sGenerator that parses the HTML source, yielding markup events. :return: a markup event stream :raises ParseError: if the HTML text is not well formed c 3sMyd}t}xxc| oZtidjoDii|}|djoit}qi|qWx(iD]\}}}|||fVqWg_|o=i}|i x"|D]}t t ||fVqWPqqWnRt i j oC}d|i|i|if}t|i|i|inXdS(NiiiR_s%s: line %d, column %di(R`RaR\R6RbtcloseR8tfeedRtreverseR RRtHTMLParseErrorR)R-R.RR,( RiRjRkR}R~t open_tagsRRmR)(R/(s2/usr/lib64/python2.6/site-packages/genshi/input.pyRnCs2        (RRoRp(R/Rn((R/s2/usr/lib64/python2.6/site-packages/genshi/input.pyRq=scCst|iS(N(RrRq(R/((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRs]scCs:|djo|i}n|ii|||fdS(N(RR[R\R|(R/R}RkR~((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR`s cCs"|i\}}|i||fS(N(tgetposR,(R/R-tcolumn((s2/usr/lib64/python2.6/site-packages/genshi/input.pyR[escCsg}xy|D]q\}}|djot|}n*t|tp|i|id}n|it|t|fq W|it t|t |f||i jo|it t|n|i i|dS(Ntreplace(RReRdtdecodeR]R|RRRRRt _EMPTY_ELEMSR R(R/RRt fixed_attribRR%((s2/usr/lib64/python2.6/site-packages/genshi/input.pythandle_starttagis  #"cCsm||ijoYxV|ioG|ii}|itt||i|ijoPqqWndS(N(RRtpopRR Rtlower(R/Rtopen_tag((s2/usr/lib64/python2.6/site-packages/genshi/input.pyt handle_endtagxs cCs=t|tp|i|id}n|it|dS(NR(RdReRR]RR (R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pyt handle_datascCsY|iidott|dd}ntt|}|it|dS(Ntxii(RRRtintRR (R/RR((s2/usr/lib64/python2.6/site-packages/genshi/input.pythandle_charrefscCsKytti|}Wntj od|}nX|it|dS(Ns&%s;(RRRRRR (R/RR((s2/usr/lib64/python2.6/site-packages/genshi/input.pythandle_entityrefs cCs\|idd\}}|ido|d }n|it|i|ifdS(Nit?i(tsplitRtendswithRRtstrip(R/RkR((s2/usr/lib64/python2.6/site-packages/genshi/input.pyt handle_piscCs|it|dS(N(RR(R/R((s2/usr/lib64/python2.6/site-packages/genshi/input.pythandle_commentsN(R1R2R3t frozensetRRR+RqRsRR[RRRRRRR(((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRs           sutf-8cCs"tttt|d|S(s8Parse the given HTML source and return a markup stream. Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be iterated over multiple times: >>> html = HTML('

Foo

') >>> print html

Foo

>>> print html.select('h1')

Foo

>>> print html.select('h1/text()') Foo :param text: the HTML source :return: the parsed XML event stream :raises ParseError: if the HTML text is not well-formed, and error recovery fails R](RRRR(RR]((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRsccsg}d}xt|dgD]\}}}|tjo(|i||djo |}qq|o%tdi||fV|2d}n|o|||fVqqWdS(s3Coalesces adjacent TEXT events into a single event.uN(NNN(RRR R|R(tstreamttextbufttextposR}RkR~((s2/usr/lib64/python2.6/site-packages/genshi/input.pyRps    ('R3t itertoolsRt xml.parsersRRt NameErrortsetsRRRRRt genshi.coreRRRRRR R R R R RRRRRt__all__t __docformat__RR*RtobjectRRRRp(((s2/usr/lib64/python2.6/site-packages/genshi/input.pyts(  "L