LۚvߙdZgdZdZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z Gdde Zd ZGd d Zifd ZdCd ZdCdZeZGddZGddZe j.dZdCdZdZhdZdZdZeeedZdZdddddd d!d"Z e e_ d#Z!d$Z"d%Z#d&Z$dDddd'd(d)Z%Gd*d+ejLZ'dDddd'd(d,Z(d-Z)dEd.Z*dCd/Z+dDd0Z,Gd1d2Z-dCd3Z.dCd4Z/e.Z0dCd5Z1Gd6d7Z2Gd8d9Z3dCddd:d;Z4ejjdZ9d?Z:d@Z; eZZ>e>eey#e?$rYywxYw)FaLightweight XML support for Python. XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. This module has two classes for this purpose: 1. ElementTree represents the whole XML document as a tree and 2. Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level. Element is a flexible container object designed to store hierarchical data structures in memory. It can be described as a cross between a list and a dictionary. Each Element has a number of properties associated with it: 'tag' - a string containing the element's name. 'attributes' - a Python dictionary storing the element's attributes. 'text' - a string containing the element's text content. 'tail' - an optional string containing text after the element's end tag. And a number of child elements stored in a Python sequence. To create an element instance, use the Element constructor, or the SubElement factory function. You can also use the ElementTree class to wrap an element structure and convert it to and from XML. )CommentdumpElement ElementTree fromstringfromstringlistindent iselement iterparseparse ParseErrorPIProcessingInstructionQName SubElementtostring tostringlist TreeBuilderVERSIONXMLXMLID XMLParser XMLPullParserregister_namespace canonicalizeC14NWriterTargetz1.3.0N) ElementPathceZdZdZy)r zAn error when parsing an XML document. In addition to its exception value, a ParseError contains two extra attributes: 'code' - the specific exception code 'position' - the line and column of the error N)__name__ __module__ __qualname____doc__text...tail Nc t|ts"td|jj||_i|||_g|_y)Nzattrib must be dict, not ) isinstancedict TypeError __class__r r(attrib _children)selfr(r1extras r&__init__zElement.__init__sL&$'  )),- -))5) r%c`d|jj|jt|fzS)Nz<%s %r at %#x>)r0r r(idr3s r&__repr__zElement.__repr__s&4>>#:#:DHHbh"OOOr%c&|j||S)zCreate a new element with the same type. *tag* is a string containing the element name. *attrib* is a dictionary containing the element attributes. Do not call this method, use the SubElement factory function instead. )r0)r3r(r1s r& makeelementzElement.makeelements~~c6**r%c|j|j|j}|j|_|j|_||dd|SN)r;r(r1texttail)r3elems r&__copy__zElement.__copy__s@$++6II II Q r%c,t|jSr=)lenr2r8s r&__len__zElement.__len__s4>>""r%cjtjdtdt|jdk7S)NzTesting an element's truth value will always return True in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. stacklevelr)warningswarnDeprecationWarningrCr2r8s r&__bool__zElement.__bool__s1  K 1   4>>"a''r%c |j|Sr=r2r3indexs r& __getitem__zElement.__getitem__s~~e$$r%ct|tr|D]}|j|n|j|||j|<yr=)r-slice_assert_is_elementr2)r3rPr*elts r& __setitem__zElement.__setitem__sB eU #'',  # #G , 'ur%c|j|=yr=rNrOs r& __delitem__zElement.__delitem__s NN5 !r%c\|j||jj|y)aAdd *subelement* to the end of this element. The new element will appear in document order after the last existing subelement (or directly after the text, if it's the first subelement), but before the end tag for this element. NrTr2appendr3 subelements r&r[zElement.appends$  + j)r%cj|D].}|j||jj|0y)zkAppend subelements from a sequence. *elements* is a sequence with zero or more elements. NrZ)r3elementsr*s r&extendzElement.extends.  G  # #G , NN ! !' * r%c^|j||jj||y)z(Insert *subelement* at position *index*.N)rTr2insert)r3rPr]s r&rbzElement.inserts$  + eZ0r%cft|ts!tdt|jzy)Nzexpected an Element, not %s)r- _Element_Pyr/typer )r3es r&rTzElement._assert_is_elements.![)9DGr?r8s r&rwz Element.clear?s)  $$ DIr%c:|jj||S)agGet element attribute. Equivalent to attrib.get, but some implementations may handle this a bit more efficiently. *key* is what attribute to look for, and *default* is what to return if the attribute was not found. Returns a string containing the attribute value, or the default if attribute was not found. )r1get)r3keyrqs r&ryz Element.getJs{{sG,,r%c"||j|<y)zSet element attribute. Equivalent to attrib[key] = value, but some implementations may handle this a bit more efficiently. *key* is what attribute to set, and *value* is the attribute value to set it to. N)r1)r3rzvalues r&setz Element.setWs! Cr%c6|jjS)zGet list of attribute names. Names are returned in an arbitrary order, just like an ordinary Python dict. Equivalent to attrib.keys() )r1keysr8s r&rz Element.keysas{{!!r%c6|jjS)zGet element attributes as a sequence. The attributes are returned in arbitrary order. Equivalent to attrib.items(). Return a list of (name, value) tuples. )r1itemsr8s r&rz Element.itemsjs{{  ""r%c#K|dk(rd}||j|k(r||jD]}|j|Ed{y7w)aCreate tree iterator. The iterator loops over the element and all subelements in document order, returning all elements with a matching tag. If the tree structure is modified during iteration, new or removed elements may or may not be included. To get a stable set, use the list() function on the iterator, and loop over the resulting list. *tag* is what tags to look for (default is to return all elements) Return an iterator containing all the matching elements. *N)r(r2iter)r3r(rfs r&rz Element.iterusJ #:C ;$((c/JAvvc{ " " "sAA A A c#K|j}t|ts|y|j}|r||D]-}|j Ed{|j }|s*|/y7w)zCreate text iterator. The iterator loops over the element and all subelements in document order, returning all inner text. N)r(r-strr>itertextr?)r3r(trfs r&rzElement.itertexts`hh#s#  II GAzz| # #A  #sA A) A' A) A)r=NN)!r r!r"r#r(r1r>r?r5r9r;rArDrLrQrVrXr[r`rbrTrhrjrorsrurwryr}rrrrr$r%r&rr~s( C F1 D D$&P +#(%(" *+1 N * 8 E ; < % -!" ##,r%rc Xi||}|j||}|j||S)aSubelement factory which creates an element instance, and appends it to an existing parent. The element tag, attribute names, and attribute values can be either bytes or Unicode strings. *parent* is the parent element, *tag* is the subelements name, *attrib* is an optional directory containing element attributes, *extra* are additional attributes given as keyword arguments. )r;r[)parentr(r1r4r*s r&rrs6! % F  f-G MM' Nr%c2tt}||_|S)zComment element factory. This function creates a special element which the standard serializer serializes as an XML comment. *text* is a string containing the comment string. )rrr>)r>r*s r&rrsgGGL Nr%cdtt}||_|r|jdz|z|_|S)a*Processing Instruction element factory. This function creates a special element which the standard serializer serializes as an XML comment. *target* is a string containing the processing instruction, *text* is a string containing the processing instruction contents, if any.  )rrr>)targetr>r*s r&rrs3+,GGL ||c)D0 Nr%cHeZdZdZd dZdZdZdZdZdZ d Z d Z d Z y) raQualified name wrapper. This class can be used to wrap a QName attribute value in order to get proper namespace handing on output. *text_or_uri* is a string containing the QName value either in the form {uri}local, or if the tag argument is given, the URI part of a QName. *tag* is an optional argument which if given, will make the first argument (text_or_uri) be interpreted as a URI, and this argument (tag) be interpreted as a local name. Nc&|rd|d|}||_y)N{}r>)r3 text_or_urir(s r&r5zQName.__init__s &137K r%c|jSr=rr8s r&__str__z QName.__str__s yyr%cPd|jjd|jdS)N)r0r r>r8s r&r9zQName.__repr__s NN33TYY??r%c,t|jSr=)hashr>r8s r&__hash__zQName.__hash__sDIIr%crt|tr|j|jkS|j|kSr=r-rr>r3others r&__le__z QName.__le__. eU #99 * *yyE!!r%crt|tr|j|jkS|j|kSr=rrs r&__lt__z QName.__lt__. eU #99uzz) )yy5  r%crt|tr|j|jk\S|j|k\Sr=rrs r&__ge__z QName.__ge__rr%crt|tr|j|jkDS|j|kDSr=rrs r&__gt__z QName.__gt__rr%crt|tr|j|jk(S|j|k(Sr=rrs r&__eq__z QName.__eq__rr%r=) r r!r"r#r5rr9rrrrrrr$r%r&rrs5  @"!"!"r%rcpeZdZdZddZdZdZddZddZddZ dd Z dd Z dd Z dd d dZ dZy)ra%An XML element hierarchy. This class also provides support for serialization to and from standard XML. *element* is an optional root element node, *file* is an optional file handle or file name of an XML file whose contents will be used to initialize the tree with. Nc:||_|r|j|yyr=)_rootr )r3r*files r&r5zElementTree.__init__ s  JJt  r%c|jS)z!Return root element of this tree.rr8s r&getrootzElementTree.getroots zzr%c||_y)zReplace root element of this tree. This will discard the current contents of the tree and replace it with the given element. Use with care! Nr)r3r*s r&_setrootzElementTree._setroots  r%cd}t|dst|d}d} |Kt}t|dr5|j||_|j|r|j SS|j dx}r%|j||j dx}r%|j |_|j|r|j SS#|r|j wwxYw)a=Load external XML document into element tree. *source* is a file name or file object, *parser* is an optional parser instance that defaults to XMLParser. ParseError is raised if the parser fails to parse the document. Returns the root element of the given source document. FreadrbT _parse_wholei)r)openrrrcloserfeed)r3sourceparser close_sourcedatas r&r zElementTree.parse!s vv&&$'FL ~"6>2 "(!4!4V! r>r) _serialize ValueError _get_writerlower_serialize_textr _namespaces) r3file_or_filenameencodingxml_declarationdefault_namespacemethodrwritedeclared_encodingqnamesrm serializes r&rzElementTree.writes:F : %069: :"% )8 48R@QO$,^^%2&,,.6KK%()tzz2%0=N%O" &v. %VZ/CE5 4 4s BCCc(|j|dS)Nr)r)r)r3rs r& write_c14nzElementTree.write_c14nszz$vz..r%rr=)NNNN)r r!r"r#r5rrr rrjrorsrurrr$r%r&rrsZ   D $1,>,4,5." $ 3E $( 3Ej/r%rc#~K |j}|jdk(r|t|ddxsdfytj5}t |t jr|}nt |t jr1t j|}|j|jnFt j}d|_ ||_ |j|_ |j|_t j ||dd}|j|j|j|fdddy#t$rYXwxYw#1swYyxYw#t$rM|jdk(rd}t#|d|d 5}|j|fdddYy#1swYYyxYwwxYww) NrrrcyNTr$r$r%r&z_get_writer..sDr%xmlcharrefreplace )rerrorsnewlinew)rr)rrgetattr contextlib ExitStackr-ioBufferedIOBase RawIOBaseBufferedWritercallbackdetachwritableseekabletellAttributeError TextIOWrapperr)rrrstackrs r&rrs-+ && >> y (!1:tDOO O%%'5.0A0AB+D 0",,?,,-=>DNN4;;/,,.D$0DM!&DJ)9(A(A $4$9$9 ''19/B046 t{{+jj(**9('&*'(' ' >> y (H "C(,.15**h& &... 'sF= E$=F= B E"E .add_qname s .RayC 9++C3S#,>+//4F~!'#j/!9*0 3/5s$;F5M$'F5M$$3!&u  . &u - .sA.B 2B 8B B"!B") rr(r-rr>rrr rr) r@rrr(rzr|r>rmrs ` @@r&rrs(D\FJ(* $%.8 hh c5 !xxv%#((# S !& # _G!32 &s +**,JC#u%hh& #%'EJJf,D%**% 'yy dE "tyy'> dii '( : r%c |j}|j}|tur |d|znp|tur |d|zn[||}|,|r|t ||D]}t |||d|n(|d|zt |j} | s|r|rCt|jdD]$\} } | rd| z} |d| d t| d &| D]^\} } t| tr | j} t| tr|| j} n t| } |d || d | d `|s t|s|sA|d |r|t ||D]}t |||d||d |zd zn|d|jr|t |jyy)N rrc |dSNrr$xs r&rz _serialize_xml..hQqTr%rzr xmlns=""rr)r(r>rr _escape_cdata_serialize_xmllistrsorted_escape_attribr-rrCr?) rr@rrmrkwargsr(r>rfrvks r&rrRs ((C 99D g~ kD ! % % hoSk ;mD)*ua4HJ #) &E  &z'7'7'9+9!;1 #aA*1- !;"DAq!!U+FF!!U+"166N*1-&)Q78"s4y(<c --.A"5!VT8LNdSj3&'e  yy mDII&'r%>brhrcolimgwbrareabaselinkmetaembedframeinputparamtrackrisindexbasefontc |j}|j}|tur|dt|zn|tur|dt|znj||}|*|r|t||D]}t |||dn9|d|zt |j}|s|r|rCt|jdD]$\} } | rd| z} |d| dt| d &|D]^\} } t| tr | j} t| tr|| j} n t| } |d || d| d `|d |j} |r$| d k(s| d k(r ||n|t||D]}t |||d| tvr|d|zd z|jr|t|jyy)Nrr rc |dSr r$r s r&rz!_serialize_html..rr%rrrrrrrscriptstyler)r(r>rrr_serialize_htmlrrrrr-r_escape_attrib_htmlr HTML_EMPTYr?) rr@rrmrr(r>rfrrrltags r&r0r0s ((C 99D g~ kM$//0 % % ht,,-Sk ;mD)*q&$7 #) &E  &z'7'7'9+9!;1 #aA*1- !;"DAq!!U+FF!!U+"166N/2&)Q78" #J99;D8#tw$K--.q&$7:%dSj3&' yy mDII&'r%c||jD] }|| |jr||jyyr=)rr?)rr@parts r&rrs1  d   yy diir%)rhtmlr>ctjd|r tdttj D]\}}||k(s||k(st|=|t|<y)atRegister a namespace prefix. The registry is global, and any existing mapping for either the given prefix or the namespace URI will be removed. *prefix* is the namespace prefix, *uri* is a namespace uri. Tags and attributes in this namespace will be serialized with prefix if possible. ValueError is raised if prefix is reserved or is invalid. zns\d+$z'Prefix format reserved for internal useN)rematchrrrr)rrrrs r&rrs[ xx 6"BCC^))+,1 8qF{q!-!N3r%rr6rdfwsdlxsxsidc)$http://www.w3.org/XML/1998/namespacezhttp://www.w3.org/1999/xhtmlz+http://www.w3.org/1999/02/22-rdf-syntax-ns#z http://schemas.xmlsoap.org/wsdl/z http://www.w3.org/2001/XMLSchemaz)http://www.w3.org/2001/XMLSchema-instancez http://purl.org/dc/elements/1.1/cLtd|dt|jd)Nzcannot serialize z (type ))r/rer rs r&rrs! +/d1D1DE  r%c d|vr|jdd}d|vr|jdd}d|vr|jdd}|S#ttf$rt|YywxYw)N&&r<r>replacer/rrrs r&rrso ) $;<<W-D $;<<V,D $;<<V,D ~ &)"4()AAA#"A#c| d|vr|jdd}d|vr|jdd}d|vr|jdd}d|vr|jdd}d |vr|jd d }d |vr|jd d }d |vr|jd d}|S#ttf$rt|YywxYw)NrCrDrrErrFr" z rz  z rGrs r&rrs) $;<<W-D $;<<V,D $;<<V,D 4<<<h/D 4<<<g.D 4<<<g.D 4<<<g.D ~ &)"4()sBBB;:B;c d|vr|jdd}d|vr|jdd}d|vr|jdd}|S#ttf$rt|YywxYw)NrCrDrrFrrKrGrs r&r1r1sm ) $;<<W-D $;<<V,D 4<<<h/D ~ &)"4()rIT)rrrc|dk(rtjntj}t|j |||||||j S)a Generate string representation of XML element. All subelements are included. If encoding is "unicode", a string is returned. Otherwise a bytestring is returned. *element* is an Element instance, *encoding* is an optional output encoding defaulting to US-ASCII, *method* is an optional output which can be one of "xml" (default), "html", "text" or "c14n", *default_namespace* sets the default XML namespace (for "xmlns"). Returns an (optionally) encoded string containing the XML data. rrrrr)rStringIOBytesIOrrgetvalue)r*rrrrrstreams r&rr+sV ')3R[[]Fvx/>1B&,4H J ?? r%c.eZdZdZdZdZdZdZdZy)_ListDataStreamz7An auxiliary stream accumulating into a list reference.c||_yr=)lst)r3rXs r&r5z_ListDataStream.__init__Es r%cyrr$r8s r&rz_ListDataStream.writableHr%cyrr$r8s r&rz_ListDataStream.seekableKrZr%c:|jj|yr=)rXr[)r3bs r&rz_ListDataStream.writeNs r%c,t|jSr=)rCrXr8s r&rz_ListDataStream.tellQs488}r%N) r r!r"r#r5rrrrr$r%r&rVrVCsAr%rVc`g}t|}t|j|||||||S)NrP)rVrr)r*rrrrrrXrTs r&rrTsC C S !Fvx/>1B&,4H J Jr%ct|ts t|}|jtjd|j j }|r|ddk7r tjjdyy)a#Write element tree or element structure to sys.stdout. This function should be used for debugging only. *elem* is either an ElementTree, or a single Element. The exact output format is implementation dependent. In this version, it's written as an ordinary XML file. r)rrN)r-rrsysstdoutrr?)r@r?s r&rrasb dK (4 JJszzIJ. <<>  D 48t# $r%ct|tr|j}|dkrtd|t |syd|zzgfd|dy)a&Indent an XML document by inserting newlines and indentation space after elements. *tree* is the ElementTree or Element to modify. The (root) element itself will not be changed, but the tail text of all elements in its subtree will be adapted. *space* is the whitespace to insert for each indentation level, two space characters by default. *level* is the initial indentation level. Setting this to a higher value than 0 can be used for indenting subtrees that are more deeply nested inside of a document. rz,Initial indentation level must be >= 0, got Nrc|dz} |}|jr|jjs||_|D]D}t |r |||j r|j jr>||_Fj js ||_yy#t$r|z}j|YwxYwr ) IndexErrorr[r>striprCr?)r@level child_levelchild_indentationchild_indent_children indentationsspaces r&rlz indent.._indent_childrensai  3 ,[ 9  yy  1)DIE5z  4::UZZ%5%5%7. zz!%e,EJ" 3 ,U 3e ;     1 2 3sB))"C C)r-rrrrC)treernrhrlrms ` @@r&rrtsb$ $||~ qyGwOPP t955=()L-,T1r%c>t}|j|||S)zParse XML document into element tree. *source* is a filename or file object containing XML data, *parser* is an optional parser instance defaulting to XMLParser. Return an ElementTree instance. )rr )rrros r&r r s =DJJvv Kr%ct||tdstddndfdGfddtjj }|}d |_tj||S) aJIncrementally parse XML document into ElementTree. This class also reports what's going on to the user based on the *events* it is initialized with. The supported events are the strings "start", "end", "start-ns" and "end-ns" (the "ns" events are used to get detailed namespace information). If *events* is omitted, only "end" events are reported. *source* is a filename or file object containing XML data, *events* is a list of events to report back, *parser* is an optional parser instance. Returns an iterator providing (event, elem) pairs. )events_parserrrTFc3\K jEd{|jd}|snj|>j}jEd{}|||_r|j yy7v7*#r|j wwxYww)Ni@) read_eventsrr_close_and_return_rootrootr)rrrwitr pullparserwrs r&iteratorziterparse..iterators %11333{{9-% 446D!--/ / /B~ 4 0  s?B,BBA B'B(B<B,BBB))B,cBeZdZWWjZfdZy)$iterparse..IterParseIteratorc,rjyyr=)r)r3rrs r&__del__z,iterparse..IterParseIterator.__del__s r%N)r r!r"__next__r)rr{rsr&IterParseIteratorr}sF#,, r%rN) rr)r collectionsabcIteratorrwweakrefref) rrrrrrxrr{ryrzs ` @@@@r&r r st"ff=J 66 "fd#  $KOO44  BBG RB Ir%c8eZdZd dddZdZdZdZdZdZy) rN)rsctj|_|xstt |_|d}|j j |j|y)Nr)end)rdeque _events_queuerrrs _setevents)r3rrrss r&r5zXMLPullParser.__init__sL )..0A);="A >F  2 2F;r%c|j td|r |jj|yy#t$r%}|jj |Yd}~yd}~wwxYw)Feed encoded data to parser.Nz!feed() called after end of stream)rsrr SyntaxErrorrr[)r3rexcs r&rzXMLPullParser.feeds` << @A A  / !!$'  /""))#.. /s8 A&A!!A&cH|jj}d|_|Sr=)rsr)r3rws r&rvz$XMLPullParser._close_and_return_roots ||!!#  r%c$|jy)zFinish feeding data to parser. Unlike XMLParser, does not return the root element. Use read_events() to consume elements from XMLPullParser. N)rvr8s r&rzXMLPullParser.closes ##%r%c#|K|j}|r*|j}t|tr|||r)yyw)zReturn an iterator over currently available (event, elem) pairs. Events are consumed from the internal event queue as they are retrieved from the iterator. N)rpopleftr- Exception)r3rrevents r&ruzXMLPullParser.read_eventss; ##NN$E%+  s7<<cf|j td|jjy)Nz"flush() called after end of stream)rsrflushr8s r&rzXMLPullParser.flush$s( << AB B r%r=) r r!r"r5rrvrrurr$r%r&rrs' rs r&rr*s+ +-0 KK <<>r%c|stt}|j||j}i}|j D]}|j d}|s|||<||fS)aParse XML document from string constant for its IDs. *text* is a string containing XML data, *parser* is an optional parser instance, defaulting to the standard XMLParser. Returns an (Element, dict) tuple, in which the dict maps element id:s to elements. rr7)rrrrrry)r>rroidsr@r7s r&rr;sd +-0 KK <<>D C  XXd^ CG 9r%c~|stt}|D]}|j||jS)zParse XML document from sequence of string fragments. *sequence* is a list of other sequence, *parser* is an optional parser instance, defaulting to the standard XMLParser. Returns an Element instance. rr)sequencerr>s r&rrSs5 +-0 D <<>r%cVeZdZdZdddddddZdZdZdZd Zd Z d Z dd Z d Z y)ra8Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. *element_factory* is an optional element factory which is called to create new Element instances, as necessary. *comment_factory* is a factory to create comments to be used instead of the standard factory. If *insert_comments* is false (the default), comments will not be inserted into the tree. *pi_factory* is a factory to create processing instructions to be used instead of the standard factory. If *insert_pis* is false (the default), processing instructions will not be inserted into the tree. NF)comment_factory pi_factoryinsert_comments insert_piscg|_g|_d|_d|_d|_|t }||_||_|t}||_ ||_ |t}||_ yr=) _data_elem_lastr_tailr_comment_factoryrr _pi_factoryrr_factory)r3element_factoryrrrrs r&r5zTreeBuilder.__init__ysn      "%O /.  .J%$  "%O' r%c~t|jdk(sJd|jJd|jS)z;Flush builder buffers and return toplevel document Element.rzmissing end tagszmissing toplevel element)rCrrr8s r&rzTreeBuilder.closes>4::!#7%77#zz%A'AA%zzr%cL|jr|jdj|j}|jr/|jjJd||j_n.|jj Jd||j_g|_yy)Nrzinternal error (tail)zinternal error (text))rrjoinrr?r>r3r>s r&_flushzTreeBuilder._flushs ::zz%wwtzz*::::??2K4KK2&*DJJO::??2K4KK2&*DJJODJ r%c:|jj|y)zAdd text to current element.N)rr[r3rs r&rzTreeBuilder.datas $r%c|j|j||x|_}|jr|jdj |n|j ||_|jj |d|_|S)zOpen new element and return it. *tag* is the element name, *attrs* is a dict containing element attributes. rar)rrrrr[rr)r3r(attrsr@s r&startzTreeBuilder.startsp  MM#u55 T :: JJrN ! !$ ' ZZ DJ $  r%c|j|jj|_|jj|k(s"Jd|jjd|dd|_|jS)zOClose and return current Element. *tag* is the element name. zend tag mismatch (expected z, got rAr)rrpoprr(rrs r&rzTreeBuilder.endsa ZZ^^% zz~~$ (::>>3( ($ zzr%cP|j|j|j|S)z`Create a comment using the comment_factory. *text* is the text of the comment. )_handle_singlerrrs r&commentzTreeBuilder.comments* ""  ! !4#7#7? ?r%cR|j|j|j||S)zCreate a processing instruction using the pi_factory. *target* is the target name of the processing instruction. *text* is the data of the processing instruction, or ''. )rrr)r3rr>s r&pizTreeBuilder.pis* ""   doovt= =r%c||}|rH|j||_|jr|jdj|d|_|S)Nrar)rrrr[r)r3factoryrbargsr@s r&rzTreeBuilder._handle_singlesG~  KKMDJzz 2%%d+DJ r%r=) r r!r"r#r5rrrrrrrrr$r%r&rresC&(!%$!&5((  " ?=r%rc`eZdZdZddddZdZdZdZdZd Z d Z d Z d Z d Z dZdZy)raaElement structure builder for XML source data based on the expat parser. *target* is an optional target object which defaults to an instance of the standard TreeBuilder class, *encoding* is an optional encoding string which if given, overrides the encoding specified in the XML file: http://www.iana.org/assignments/character-sets N)rrcJ ddlm}|j |d}| t }|x|_|_|x|_|_ |j|_ i|_ |j|_t|dr|j |_t|dr|j$|_t|dr|j(|_t|dr|j,|_t|d r|j0|_t|d r|j4|_t|d r|j8|_d |_d |_d|_ i|_! d |jDz|_#y#t$r" ddl}n#t$r tdwxYwYwxYw#tH$rYywxYw)Nrexpatz7No module named expat; use SimpleXMLTreeBuilder insteadrrrstart_nsend_nsrrrrzExpat %d.%d.%d)% xml.parsersr ImportErrorpyexpat ParserCreaterrrsr_targeterror_error_names_defaultDefaultHandlerExpandr)_startStartElementHandler_endEndElementHandler _start_nsStartNamespaceDeclHandler_end_nsEndNamespaceDeclHandlerrCharacterDataHandlerrCommentHandlerrProcessingInstructionHandler buffer_textordered_attributes_doctypeentity version_infoversionr)r3rrrrs r&r5zXMLParser.__init__s  )##Hc2 > ]F%++ dl%++ dlkk  &*mm# 67 #)-F & 65 !'+yyF $ 6: &/3~~F , 68 $-1\\F * 66 "*0++F ' 69 %$*NNF ! 64 28))F /$%!   +e.@.@@DLM  ' !M ( N   s;E(F( F2E76F7F  FF F"!F"c|j}|j}|D]}|dk(r d|_|||jfd}||_(|dk(r|||j fd}||_F|dk(r6t|jdr|||jfd}n||fd }||_ |d k(r6t|jd r|||jfd }n||fd }||_ |dk(r|||fd}||_ |dk(r|||fd}||_td|zy)Nrrc&|||||fyr=r$)r( attrib_inrr[rs r&handlerz%XMLParser._setevents..handler"sE5i#89:r%rc$||||fyr=r$)r(rr[rs r&rz%XMLParser._setevents..handler'sE3s8,-r%zstart-nsrc&|||||fyr=r$)rrrr[rs r&rz%XMLParser._setevents..handler.sx'<=>r%c,|||xsd|xsdffyNrr$)rrrr[s r&rz%XMLParser._setevents..handler2s "ciR'@ABr%zend-nsrc$||||fyr=r$)rrr[rs r&rz%XMLParser._setevents..handler8svf~67r%c||dfyr=r$)rrr[s r&rz%XMLParser._setevents..handler<st}-r%rcJ|||jj|fyr=)rr)r>rr[r3s r&rz%XMLParser._setevents..handler@sE4;;#6#6t#<=>r%rcL|||jj||fyr=)rr) pi_targetrrr[r3s r&rz%XMLParser._setevents..handlerDsE4;;>>)T#BCDr%zunknown event %r)rsr[rrrrrr)rrrrrrrr)r3 events_queueevents_to_reportrr[ event_namers r&rzXMLParser._seteventss5$$*JW$,-)2fC3:0x'4;;1.8'+||8/9.18.y((26?(/%t#3=f!%E7>3 !3j!@AAU+r%cxt|}|j|_|j|jf|_|r=)r codelinenooffsetposition)r3r|errs r& _raiseerrorzXMLParser._raiseerrorKs0::||U\\1  r%cz |j|}|S#t$r|}d|vrd|z}||j|<Y|SwxYw)Nrr)rKeyError)r3rznames r&_fixnamezXMLParser._fixnameQsU $;;s#D   $Dd{Tz#DKK   $s #::cJ|jj|xsd|xsdSr)rrr3rrs r&rzXMLParser._start_ns\s!{{##FLb#)<r data_handlerrrnrerpubidsystems r&rzXMLParser._defaultqsbq S= #{{//  T[[a45s]tBQx;6DM ]] &} $ ::{{   Q    KK 1 1+ >s)7A//B>BBBBB9)r r!r"r#r5rrrrrrrrrrrr$r%r&rrsN"&+Z3Bj =0 .34%l *"?r%r)out from_filec "| | tdd}|tjx}}tt |j fi|}|"|j ||jn| t||||jSdS)a3Convert XML to its C14N 2.0 serialised form. If *out* is provided, it must be a file or file-like object that receives the serialised canonical XML output (text, not bytes) through its ``.write()`` method. To write to a file, open it in text mode with encoding "utf-8". If *out* is not provided, this function returns the output as text string. Either *xml_data* (an XML string) or *from_file* (a file path or file-like object) must be provided as input. The configuration options are the same as for the ``C14NWriterTarget``. Nz:Either 'xml_data' or 'from_file' must be provided as inputr)r) rrrQrrrrrr rS)xml_datarroptionssiors r&rrsI-UVV C {KKM!c .syyDGD EF H    i' _3<<>6$6r%z ^\w+:\w+$ceZdZdZdddddddddZefdZdZddZd Z d jfd Z d Z d Z ddZdZdZdZy)ra Canonicalization writer target for the XMLParser. Serialises parse events to XML C14N 2.0. The *write* function is used for writing out the resulting data stream as text (not bytes). To write to a file, open it in text mode with encoding "utf-8" and pass its ``.write`` method. Configuration options: - *with_comments*: set to true to include comments - *strip_text*: set to true to strip whitespace before and after text content - *rewrite_prefixes*: set to true to replace namespace prefixes by "n{number}" - *qname_aware_tags*: a set of qname aware tag names in which prefixes should be replaced in text content - *qname_aware_attrs*: a set of qname aware attribute names in which prefixes should be replaced in text content - *exclude_attrs*: a set of attribute names that should not be serialised - *exclude_tags*: a set of tag names that should not be serialised FN) with_comments strip_textrewrite_prefixesqname_aware_tagsqname_aware_attrs exclude_attrs exclude_tagsc2||_g|_||_||_|r t |nd|_|r t |nd|_||_|rt ||_nd|_|rt |j|_ nd|_ dgg|_ g|_ |s6|jjttj!|jjgi|_dg|_d|_d|_d|_d|_y)N)r?rFr)_writer_with_comments _strip_textr}_exclude_attrs _exclude_tags_rewrite_prefixes_qname_aware_tags intersection_find_qname_aware_attrs_declared_ns_stack _ns_stackr[rrr _prefix_map_preserve_space_pending_start _root_seen _root_done_ignored_depth) r3rr%r&r'r(r)r*r+s r&r5zC14NWriterTarget.__init__s  +%4Ac-0t2>S.D!1 %()9%:D "%)D " +./@+A+N+ND (+/D ( <$ #  NN ! !$~';';'="> ? b! %w"r%c#DK||D]}|s|Ed{y7wr=r$)r3ns_stack _reversedrms r&_iter_namespacesz!C14NWriterTarget._iter_namespaces(s$#H-J%%%.%s   c|jdd\}}|j|jD]\}}||k(s d|d|cStd|d|d)NrrrrzPrefix z of QName "" is not declared in scope)splitrAr7r)r3 prefixed_namerrrps r&_resolve_prefix_namez%C14NWriterTarget._resolve_prefix_name-sl$**32 ++DNN;FCF{C54&))<76(+m_D^_``r%c|%|dddk(r|ddjddnd|f\}}n|}t}|j|jD]/\}}||k(r||vr|r|d|n|||fcS|j |1|j ro||j vr|j |}n'dt|j x}|j |<|jdj||f|d|||fS|s d|vr|||fS|j|jD]:\}}||k(s |jdj||f|r|d|n|||fcS|s|||fStd|d ) Nrrrrrrraz Namespace "rC) rr}rAr6addr2r8rCr[r7r)r3rrr( prefixes_seenurs r&_qnamezC14NWriterTarget._qname4s ;38!93CuQRy''Q/"eHCC ..t/F/FGIAvCxF-7,2&3%(S#EE   f %H  ! !d&&&))#.34S9I9I5J4K1LL))#.  # #B ' . .V} =XQse$c3. .r.S= ..t~~>IAvCx''+22C=A,2&3%(S#EE? S= ;se+EFGGr%cT|js|jj|yyr=)r=rr[rs r&rzC14NWriterTarget.dataYs""" JJ  d ##r%rc||j}|jdd=|jr|jds|j}|j<|jdc}|_|r t |r|nd}|j g|||y|r(|jr|jt|yyyNra) rr/r9rgr:_looks_like_prefix_namerr;r-_escape_cdata_c14n)r3 _join_textrr qname_texts r&rzC14NWriterTarget._flush]s$**% JJqM   D$8$8$<::D   C/r%c |j4|r2|jDcic]\}}||jvs||}}}|h|}i}|'|j|x} ||<|j| |jX|rV|j |} | r@| D]:} || } t | s|j| x} || <|j| <nd} nd} |j } t|dDcic] }|| | }}|r.|Dcgc]\}}|rd|znd|f}}}|jng}|rVt|jD]:\}}| || vr||vr |||d}||\}} }|j|r|n| |f<|jd}|jj|r|dk(n|jd|j}|d ||dz|r:|d j|Dcgc]\}}d |d t|d c}}|d||t|||dd|_|j"jgycc}}wcc}wcc}}wcc}}w)Nc&|jddS)Nrr)rD)rs r&rz)C14NWriterTarget._start..s!''#q/r%rzxmlns:xmlnsrz+{http://www.w3.org/XML/1998/namespace}spacepreserverarrrrrrT)r0rrGrIr5rPrLrsortr[ryr9r-r_escape_attrib_c14nrQr;r7)r3r(rrVrSrrrresolved_namesrqattrs attr_namer| parse_qnamer parsed_qnamesrrr attr_qnamespace_behaviourrs r&rzC14NWriterTarget._starts    *u&+kkmTmdaq@S@S7SQTmET  !151J1J:1V VEN:. JJu   ' ' 311%8F!'I!),E.u58<8Q8QRW8XXu 5 5) "( Fkk 4: 153453qKN*53 4 $2#1KC'-F"'3?#1  NN I u{{}-1%!v+!~:M%nQ&78;A-:1-=* Is  *A!FG . ))$QR ##-*3M%Nq%QR S b!CU04 8UsI/I/3I5 I:Jc|jr|xjdzc_y|jr|j|jd|j |dd|j j t|j dk(|_|jj |jj y)Nrrrr) r=rrr-rLr9rrCr<r6r7rs r&rzC14NWriterTarget.ends      1 $   :: KKM bS)!,-Q/0   "d223q8 ##% r%c<|jsy|jry|jr|jdn(|jr|j r|j |jdt|d|js|jdyy)Nrz)r.r=r<r-r;rrrQrs r&rzC14NWriterTarget.commentsw""      ?? KK  __ KKM d-d34C89 KK r%c8|jry|jr|jdn(|jr|jr|j |j|rd|dt |dnd|d|js|jdyy)Nrz)r=r<r-r;rrrQ)r3rrs r&rzC14NWriterTarget.pis     ?? KK  __ KKM :>b,T232 6bPRO U KK r%r=)r r!r"r#r5reversedrArGrLrrrrrrrrrr$r%r&rrsj, %"&$#$# J4<& a#HJ$!# 210"C"J   r%rc d|vr|jdd}d|vr|jdd}d|vr|jdd}d|vr|jdd}|S#ttf$rt|YywxYw) NrCrDrrErrFrL rGrs r&rQrQs) $;<<W-D $;<<V,D $;<<V,D 4<<<g.D ~ &)"4()sAAA98A9cP d|vr|jdd}d|vr|jdd}d|vr|jdd}d|vr|jdd}d |vr|jd d }d |vr|jd d }|S#ttf$rt|YywxYw) NrCrDrrErrKrMz rz rLrirGrs r&r\r\s) $;<<W-D $;<<V,D $;<<X.D 4<<<g.D 4<<<g.D 4<<<g.D ~ &)"4()sBBB%$B%)r)_set_factoriesr=r)z r)@r#__all__rrbr8rIrrcollections.abcrrrrrr r rrrrr rrcontextmanagerrrrr2r0rrrrrrrr1rrrVrrrr r rrrrrrrrcompileUNICODEr9rPrrQr\rd _elementtreerkrr$r%r&rrs!P (       # ^^B $&$  +"+"`\/\/B /+/+b;z0(d 0(d    !*-2$*38(.(,16(, %3! ) )8 )!T"&0b''" !%&* &/l 5p77t",  $vvth?h?Z7tt7<%"**\2::>DDD)&). 3K+712  s E,,E43E4