m zEc@s/dZdklZdkZdklZdklZlZdk l Z dk l Z dk lZlZdZd Zd5d6d7d8fZeeZeied:ZeeZeiedZedZdefdYZdefdYZdefdYZdefdYZdefdYZ d e fd!YZ!d"e fd#YZ"d$e fd%YZ#d&Z$e$i%Z&d'e fd(YZ'd)efd*YZ(d+Z)d,Z*d-efd.YZ+d/Z,d0Z-d1Z.d2Z/d3fd4YZ0dS(;sMicro Document Object Model: a partial DOM implementation with SUX. This is an implementation of what we consider to be the useful subset of the DOM. The chief advantage of this library is that, not being burdened with standards compliance, it can remain very stable between versions. We can also implement utility 'pythonic' ways to access and mutate the XML tree. Since this has not subjected to a serious trial by fire, it is not recommended to use this outside of Twisted applications. However, it seems to work just fine for the documentation generator, which parses a fairly representative sample of XML. Microdom mainly focuses on working with HTML and XHTML. (s nested_scopesN(sStringIO(s XMLParsers ParseError(sInsensitiveDict(sListType(s StringTypess UnicodeTypecCsqg}|i}|g}xRt|djo>|id}|i|jo||n|i |d*qW|S(Ni( tmatchestappendtmatches_appendtiNodetslicetlentpoptctnodeNametnamet childNodes(RR RRRR((tl/home/radix/Projects/Twisted/branches/releases/twisted-core-2.5.x-2329-2/Twisted.exp/twisted/web/microdom.pytgetElementsByTagName%s  cCs|i}g}|i}|g}xXt|djoD|id}|i i|jo||n|i |d*q'W|S(Ni( R tlowerRRRRRRRRRR (RR RRRR((R tgetElementsByTagNameNoCase0s   t&s&ts>t"s"t's'cCs-x&|D]\}}|i||}qW|S(s'Perform the exact opposite of 'escape'.N(tcharststhttexttreplace(RRRR((R tunescapeHs  cCs-x&|D]\}}|i||}qW|S(s1Escape a few XML special chars with XML entities.N(RRRRR(RRRR((R tescapeNs  tMismatchedTagscBstZdZdZRS(NcCsI|||||||f\|_|_|_|_|_|_|_dS(N(tfilenametexpecttgottbegLinetbegColtendLinetendColtself(R#RRRR!R"RR ((R t__init__WscCs,d|i|i|i|i|i|ifS(NsBexpected , got line: %s col: %s, began line: %s col: %s(R#RRR!R"RR (R#((R t__str__[s(t__name__t __module__R$R%(((R RUs tNodecBstZdZddZdZddddhddZddddhddZddd dd Zddd dd Z ddd Z d Z dZ dZ dZdZdZdZRS(NR(cCs||_g|_dS(N(t parentNodeR#R (R#R)((R R$ds cCsBx;t|i|iD]$\}}|i|pdSqqWdS(Nii(tzipR#R tntatbt isEqualToNode(R#R+R,R-((R R.hs   ticCs tdS(N(tNotImplementedError(R#tstreamtindentt addindenttnewltstript nsprefixest namespace((R twritexmlnsc Cs8t}|i||||||||i }|S(N( tStringIORR#R8R2R3R4R5R6R7tgetvaluetrv( R#R2R3R4R5R6R7R;R((R ttoxmlrs  t s cCs|i|||||S(N(R#R8R1R2R3R4R5(R#R1R2R3R4R5((R twriteprettyxmlyscCs|i||||S(N(R#R<R2R3R4R5(R#R2R3R4R5((R t toprettyxml|scCs tdS(N(R0(R#tdeeptparent((R t cloneNodescCs|iodSndSdS(Nii(R#R (R#((R t hasChildNodess cCs4t|tpt|ii|||_dS(N(t isinstancetchildR(tAssertionErrorR#R RR)(R#RE((R t appendChildscCs2|ii|}||_|ii|||S(N(R#R tindextreftitnewR)tinsert(R#RKRIRJ((R t insertBefores cCs1||ijo|ii|d|_n|S(N(RER#R tremovetNoneR)(R#RE((R t removeChilds cCsst|tpt|i|jd||i|ffpt||i|ii|R?RBRCRGRMRPRSRTRU(((R R(as       tDocumentcBstZddZdddZdZdZdZeeZ dZ ddddhddZ d Z d Z d Zd Zd ZRS(NcCs)ti||o|i|ndS(N(R(R$R#tdocumentElementRG(R#RW((R R$s icCsOt}|i|_|o|iid|}n |i}|i||S(Ni( RVtdR#tdoctypeR@RWRBtnewElRG(R#R@RARXRZ((R RBs    cCs |i|ijo |i|S(N(R#RYR+R.(R#R+((R tisEqualToDocumentscCs |idS(Ni(R#R (R#((R tget_documentElementscCs)|i p tdti||dS(NsOnly one element per document.(R#R RFR(RGR(R#R((R RGsR/cCsa|id||io |id|id|n|ii|||||||dS(Nss (R#RqtvalRDt UnicodeTypetencodeR1R]( R#R1R2R3R4R5R6R7Rw((R R8s cCst|i|S(N(RcR#RpRA(R#R@RA((R RBs(R&R't__doc__R8RORB(((R Rcs RacBsJtZdddZdddZddddhddZdZRS(NicCs ti|||||_dS(N(RsR$R#RqR)traw(R#RqR)R{((R R$%scCst|i||iS(N(RaR#RpRAR{(R#R@RA((R RB)sR/c Cs|io0|i} t| tpt|i} qnV|i}t|tpt|}n|odi |i }nt |} t| t o| i d} n|i| dS(NR=Rv(R#R{RpRwRDt StringTypeststrtvR5tjointsplitRRxRyR1R]( R#R1R2R3R4R5R6R7R~Rw((R R8,s    cCsdt|idS(NsText(%st)(treprR#Rp(R#((R t__repr__=s(R&R'ROR$RBR8R(((R Ra#st CDATASectioncBs2tZdddZddddhddZRS(NicCst|i|S(N(RR#RpRA(R#R@RA((R RBBsR/cCs.|id|i|i|iddS(Ns (R1R]R#Rp(R#R1R2R3R4R5R6R7((R R8Es (R&R'RORBR8(((R RAsccs1d}x$todt|V|d}q WdS(Nitpi(RJtTrueR}(RJ((R t _genprefixKs t_AttrcBstZdZRS(s#Support class for getAttributeNode.(R&R'Rz(((R RRs R^cBstZdZdZdZddddddddZdZdZdZ dddZ dZ d Z dd Z dd Zd Zd ZdZdZddddhddZdZdZRS(Niic Csti|||p| |_||_|p|i}n|djo h|_n=||_x0|ii D]\} } t | |i| st:txmlnsR/sxmlns:Rs( RRRsbasesmetaslinkRRsinputscolRRsframe(shtmlsheadsbodyRRsdelsh1sh2RRRRRRsolRspreRRsaddressRsdivRstableRsformsobjectRRsmap(RRsolshead(-tALLOWSINGLETONt BLOCKELEMENTSt FORMATNICELYR#RRRR1R]twR6tcopyt newprefixestkeysRtbeginR4R2RiRt writeattrR7RORtprefixRtjRt iteritemsRRwRDttupletkeyt genprefixRFRt downprefixesR R3t newindentRER8R5R (R#R1R2R3R4R5R6R7RRRRRwRRRRRRRRERRRR((RR R8s|                           cCsdt|i}|io|d|if7}n|io|d|if7}n|io|d|if7}n|dS(Ns Element(%ss, attributes=%rs , filename=%rs , markpos=%rR(RR#RtrepRRR(R#R((R Rs   cCsd|i}|ip |io|d7}n|io|t|i7}n|io|d|i7}n|ip |io|d7}nx%|iiD]}|d|7}qW|i o|d|i7}n |d7}|S(NRs (s line %s column %sRs %s=%rs >...s />( R#RRRRRRRtitemRC(R#RR((R R% s      (R&R'RReROR6R$RRRRBR RRfRRRRRR8RR%(((R R^Us&           Q cCs7h}x*|iD]\}}t|||g|_|o|iit|n|ii|qKndS(NiR/s %si(R#RRRR RURRDRaRRttoldvaluetCOMMENTtmatchtgroupt parseStringtet ParseErrorRRRcR(R#RRRRRR((R t_fixScriptElement`s,"      cCs ||_dS(N(RYR#R(R#RY((R t gotDoctypesc Cs|i} |iot| tou| i}|}|i o|i }|i }n||i i |gjo |i| i|i} qnt|}|idd} h}xv|iD]h\}}|idoL|idd}t|djo|||d|ip0t|i|i|f|i|iqmqqqun||jp|io|io|id} xtt|iD]@}|i|d i|jo|i|d i|PqqW|ii|| dj o|i i| ndS|i|d 3|ip|i i| dSqgqqt|i|i|f|i|in|i||ip|i i|n|io!|idjo|i!|ndS( NtNOTHINGiiiiRR(ii("R#RRRRR RRRRtpfxdixtnstupleROReRR ttntcnameRtnsplitRRtnewnameRRR7RtlastElRtidxRRRR ( R#R R,RRR)R'R*R(RR%R&R+((R RsZ  *     <       0  cCs~ti|||io`|io|ii|idqzt|i |iddf|i |idi ndS(Niit END_OF_FILE( t XMLParsertconnectionLostR#treasonRRRRRRRR(R#R0((R R/s   (R&R'RRRR$RRtretcompileRR R RRRRR!R#RR/(((R R)s     !  4      9c Ost|dpt|d}nt||}t|dd|_|i d t|do|i |i n;|i d}x(|o |i ||i d}qW|id |ipt|idddn|iot|id joD|id}t|tp#td }|i||}qyqtd }x/|iD]}|i|qbWn|id}t|}|i|_|S( sParse HTML or XML readable.treadtrbR s R:iisNo top-level Nodes in documentiRN(RjtreadabletopenRtargstkwargstmdptgetattrRtmakeConnectionROt dataReceivedR:R3trR/RR RRRXRDR^RRGRERVtdocRRY( R5R7R8RRXR>R=RER9((R tparses>             cOsIt|to#tt|id||Sntt|||S(NsUTF-16(RDtstRxR?R9RyR7R_(R@R7R_((R R6s#cCst|ddddS(sParse an XML readable object.ReiRiN(R?R5(R5((R tparseXML=scCst|ddddS(sParse an XML readable object.ReiRiN(RR@(R@((R tparseXMLStringBstlmxcBsJtZdZddZdZdZdZddZdZRS( sEasy creation of XML.RcCs-t|tot|}n||_dS(N(RDRhR|R^R#(R#Rh((R R$Lscs.ddjotdndS(Nit_sno private attrsc si|S(N(R#taddR R_(R_(R#R (R RTs(R tAttributeError(R#R ((R#R R t __getattr__QscCs|ii||dS(N(R#RhRRRw(R#RRw((R t __setitem__VscCs|ii|S(N(R#RhRfR(R#R((R t __getitem__YsicCs&t|d|}|ii||S(NR{(RattxtR{tnnR#RhRG(R#RJR{RK((R R\scKs~t|dddd}|ii|t|}xC|i D]5\}}|ddjo|d}n|||