\dP dZgdZdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z Gdde Z d ZGd d Zifd ZdDd ZdDdZeZGddZGddZe jdZdDdZdZhdZdZdZeeedZdZdddddd d!d"Zee_d#Z d$Z!d%Z"d&Z#dEddd'd(d)Z$Gd*d+ej%Z&dEddd'd(d,Z'd-Z(dFd/Z)dDd0Z*dEd1Z+Gd2d3Z,dDd4Z-dDd5Z.e-Z/dDd6Z0Gd7d8Z1Gd9d:Z2dDddd;d<Z3ej4d=ej5j6Z7Gd>d?Z8d@Z9dAZ: eZ;ddBl$rYdSwxYw)GaLightweight 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) ElementPathceZdZdZdS)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__&..\python\lib\xml\etree\ElementTree.pyr r js Dr%r c"t|dS)z2Return True if *element* appears to be an Element.tag)hasattr)elements r&r r xs 7E " ""r%ceZdZdZdZ dZ dZ dZ ifdZdZ dZ dZ dZ dZ d Zd Zd Zd Zd ZdZdZdZdZddZddZddZddZdZddZdZdZdZddZ dZ!dS)rahAn XML element. This class is the reference implementation of the Element interface. An element's length is its number of subelements. That means if you want to check if an element is truly empty, you should check BOTH its length AND its text attribute. The element tag, attribute names, and attribute values can be either bytes or strings. *tag* is the element name. *attrib* is an optional dictionary containing element attributes. *extra* are additional element attributes given as keyword arguments. Example form: text...tail Nc t|tstd|jj||_i|||_g|_dS)Nzattrib must be dict, not ) isinstancedict TypeError __class__r r(attrib _children)selfr(r1extras r&__init__zElement.__init__s\&$'' -) )),-- -))5) r%cJd|jj|jt|fzS)Nz<%s %r at %#x>)r0r r(idr3s r&__repr__zElement.__repr__s!4>#:DHbhh"OOOr%c.|||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^tjdt|S)zReturn copy of current element. This creates a shallow copy. Subelements will be shared with the original tree. z7elem.copy() is deprecated. Use copy.copy(elem) instead.)warningswarnDeprecationWarning__copy__r8s r&copyz Element.copys.  E    }}r%c||j|j}|j|_|j|_||dd<|SN)r;r(r1texttail)r3elems r&r@zElement.__copy__s?$+66I I QQQ r%c*t|jSrC)lenr2r8s r&__len__zElement.__len__s4>"""r%cjtjdtdt|jdkS)NzyThe behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. stacklevelr)r=r> FutureWarningrHr2r8s r&__bool__zElement.__bool__s=  K a     4>""a''r%c|j|SrCr2r3indexs r& __getitem__zElement.__getitem__s~e$$r%ct|tr|D]}||n||||j|<dSrC)r-slice_assert_is_elementr2)r3rSr*elts r& __setitem__zElement.__setitem__sg eU # # - - -'',,,, -  # #G , , , 'ur%c|j|=dSrCrQrRs r& __delitem__zElement.__delitem__s N5 ! ! !r%cd|||j|dS)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. NrWr2appendr3 subelements r&r^zElement.appends4  +++ j)))))r%cn|D]1}|||j|2dS)zkAppend subelements from a sequence. *elements* is a sequence with zero or more elements. Nr])r3elementsr*s r&extendzElement.extendsJ   + +G  # #G , , , N ! !' * * * * + +r%cf|||j||dS)z(Insert *subelement* at position *index*.N)rWr2insert)r3rSr`s r&rezElement.inserts4  +++ eZ00000r%cxt|ts$tdt|jzdS)Nzexpected an Element, not %s)r- _Element_Pyr/typer )r3es r&rWzElement._assert_is_elements@![)) N9DGG)r0r rDr8s r&r9zQName.__repr__s  N333TYYY??r%c*t|jSrC)hashrDr8s r&__hash__zQName.__hash__sDIr%cbt|tr|j|jkS|j|kSrCr-rrDr3others r&__le__z QName.__le__0 eU # # +9 * *yE!!r%cbt|tr|j|jkS|j|kSrCrrs r&__lt__z QName.__lt__0 eU # # *9uz) )y5  r%cbt|tr|j|jkS|j|kSrCrrs r&__ge__z QName.__ge__rr%cbt|tr|j|jkS|j|kSrCrrs r&__gt__z QName.__gt__rr%cbt|tr|j|jkS|j|kSrCrrs r&__eq__z QName.__eq__rr%rC) r r!r"r#r5rr9rrrrrrr$r%r&rrs      @@@"""!!!"""!!!"""""r%rcreZdZdZddZdZdZddZddZddZ dd Z dd Z dd Z dd d dZ dZdS)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. NcF||_|r||dSdSrC)_rootr )r3r*files r&r5zElementTree.__init__s3   JJt       r%c|jS)z!Return root element of this tree.rr8s r&getrootzElementTree.getroots zr%c||_dS)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._setroot"s r%cd}t|dst|d}d} |Vt}t|dr8|||_|j|r|SS |d}|sn||.||_|j|r|SS#|r|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. FreadrbTN _parse_wholei)r)openrrrcloserfeed)r3sourceparser close_sourcedatas r&r zElementTree.parse,s vv&& &$''FL ~"6>22& "(!4!4V!AC$$C=c6|j|S)zCreate and return tree iterator for the root element. The iterator loops over all elements in this tree, in document order. *tag* is a string with the tag name to iterate over (default is to return all elements). )rrr3r(s r&rzElementTree.iterQszs###r%c|dddkr$d|z}tjd|ztd|j||S)a\Find first matching element by tag name or path. Same as getroot().find(path), which is Element.find() *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return the first matching element, or None if no element was found. Nr/.This search is broken in 1.3 and earlier, and will be fixed in a future version. If you rely on the current behaviour, change it to %rrKrL)r=r>rNrrmrns r&rmzElementTree.find]sc 8s??:D M-/34!      ztZ000r%c|dddkr$d|z}tjd|ztd|j|||S)aeFind first matching element by tag name or path. Same as getroot().findtext(path), which is Element.findtext() *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return the first matching element, or None if no element was found. NrrrrrKrL)r=r>rNrrrrss r&rrzElementTree.findtextssg 8s??:D M-/34!      z""4*===r%c|dddkr$d|z}tjd|ztd|j||S)aaFind all matching subelements by tag name or path. Same as getroot().findall(path), which is Element.findall(). *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return list containing all matching elements in document order. NrrrrrKrL)r=r>rNrrvrns r&rvzElementTree.findallse 8s??:D M-/34!      z!!$ 333r%c|dddkr$d|z}tjd|ztd|j||S)agFind all matching subelements by tag name or path. Same as getroot().iterfind(path), which is element.iterfind() *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return an iterable yielding all matching elements in document order. NrrrrrKrL)r=r>rNrrxrns r&rxzElementTree.iterfindse 8s??:D M-/34!      z""4444r%Tshort_empty_elementsc|sd}n|tvrtd|z|s |dkrd}nd}t||5\}}|dkrA|s0|=|dkr%|dvr|d |d |d krt ||jn:t |j|\} } t|} | ||j| | | ddddS#1swxYwYdS) aWrite element tree to a file as XML. Arguments: *file_or_filename* -- file name or a file object opened for writing *encoding* -- the output encoding (default: US-ASCII) *xml_declaration* -- bool indicating if an XML declaration should be added to the output. If None, an XML declaration is added if encoding IS NOT either of: US-ASCII, UTF-8, or Unicode *default_namespace* -- sets the default XML namespace (for "xmlns") *method* -- either "xml" (default), "html, "text", or "c14n" *short_empty_elements* -- controls the formatting of elements that contain no content. If True (default) they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags xmlzunknown method %rc14nutf-8us-asciiNunicode)rrz rDr) _serialize ValueError _get_writerlower_serialize_textr _namespaces) r3file_or_filenameencodingxml_declarationdefault_namespacemethodrwritedeclared_encodingqnamesrp serializes r&rzElementTree.writes: ;FF : % %069:: : &"% )8 4 4 E8R@QO$,^^%%22&,,..6KKK%%%()))tz2222%0=N%O%O" &v.  %VZ/CEEEE E E E E E E E E E E E E E E E E E EsB!C,,C03C0c0||dS)Nr)r)r)r3rs r& write_c14nzElementTree.write_c14nszz$vz...r%rrC)NNNN)r r!r"r#r5rrr rrmrrrvrxrrr$r%r&rr s   ####J $ $ $ $1111,>>>>,4444,5555." $ 3E $( 3E3E3E3E3Ej/////r%rc#xK |j}|dkr|t|ddpdfVdStj5}t |t jr|}nt |t jr/t j |}| |j nLt j}d|_ ||_ |j |_ |j|_n#t$rYnwxYwt j||dd}| |j |j|fVddddS#1swxYwYdS#t$rV|dkrd}t#|d|d 5}|j|fVdddYdS#1swxYwYYdSwxYw) NrrrcdSNTr$r$r%r&z_get_writer..sDr%xmlcharrefreplace )rerrorsnewlinew)rr)rrgetattr contextlib ExitStackr-ioBufferedIOBase RawIOBaseBufferedWritercallbackdetachwritableseekabletellAttributeError TextIOWrapperr)rrrstackrs r&rrs-+ & >>  y ( (!1:tDDOO O O O O O%'' +5.0ABB+DD 0",??,-=>>DNN4;////,..D$0LDM!&DJ)9(A $4$9 )'19/B04666 t{+++j(****9 + + + + + + + + + + + + + + + + + + ''' >>  y ( (H "C(,... '15*h& & & & ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'smEB E C21E 2 C?<E >C??AE  EE7F9 F* F9*F. .F91F. 2F98F9cddiird<fd}|D])}|j}t|tr|jvr||jnHt|t r|vr ||n#|!|t ur|turt|| D]^\}}t|tr|j}|vr ||t|tr|jvr||j_|j}t|tr|jvr||j+fS)Nc |dddkr|dddd\}}|}|9t|}|dtz}|dkr||<|r |d||<dS||<dSrt d||<dS#t $rt |YdSwxYw)Nrrrzns%dr:z.add_qname.s .RaRyC 9++C33S#,,>+//44F~!'#j//!9*0 3(/5vvss$;F5MMM$'F5MMM$$3!&u  . . . &u - - - - - - .sB B/B/B//C  C ) rr(r-rrDrrr rr) rFrrr(r}rrDrprs ` @@r&rr#sD\FJ+(* $%.......8 !!h c5 ! ! ,xv%% #(### S ! ! ,&   # _G!3!32 &s + + +**,, & &JC#u%% h&   #%'' &EJf,D,D %*%%%y dE " " !ty'>'> Idi : r%c |j}|j}|tur|d|zn|tur|d|zn||}|5|r|t ||D]}t |||d|na|d|zt |} | s|r|rPt|dD]+\} } | rd| z} |d| d t| d ,| D]k\} } t| tr| j} t| tr|| j} nt| } |d || d | d l|st|s|sP|d |r|t ||D]}t |||d||d |zd zn |d|j r|t |j dSdS)N rrc|dSNrr$xs r&rz _serialize_xml..v QqTr%r}r xmlns=""rr)r(rDrr _escape_cdata_serialize_xmllistrsorted_escape_attribr-rrHrE) rrFrrprkwargsr(rDrirvks r&rr`s (C 9D g~~ kD !!!! % % % hoSk ; +mD))*** J Jua4HJJJJJ J E#)   &&E 9  9 &z'7'7'9'9+9>!;!;!;1( #aAAA*1----"99DAq!!U++#F!!U++."16N*1--EE&)))QQQ78888 s4yy (< c /E---...NNA"5!VT8LNNNNNdSj3&''''e  y( mDI&&'''''((r%>brhrcolimgwbrareabaselinkmetaembedframeinputparamtrackrisindexbasefontc j|j}|j}|tur|dt|zn|tur|dt|zn||}|3|r|t||D]}t |||dnu|d|zt |}|s|r|rPt|dD]+\} } | rd| z} |d| dt| d ,|D]k\} } t| tr| j} t| tr|| j} nt| } |d || d| d l|d | } |r0| d ks| d kr ||n|t||D]}t |||d| tvr|d|zd z|jr|t|jdSdS)Nr r rc|dSr r$rs r&rz!_serialize_html..rr%rrrrrrrscriptstyler)r(rDrrr_serialize_htmlrrrrr-r_escape_attrib_htmlr HTML_EMPTYrE) rrFrrprr(rDrirrrltags r&r2r2s (C 9D g~~ kM$///0000 % % % ht,,,----Sk ; +mD))*** 8 8q&$7777 8 E#)   &&E 9  9 &z'7'7'9'9+9>!;!;!;1( #aAAA*1----"99DAq!!U++#F!!U++3"16N/22EE&)))QQQ78888 E#JJJ99;;D /8##twE$KKKKE---... 8 8q&$7777:%%dSj3&''' y( mDI&&'''''((r%c||D] }|||jr||jdSdSrC)rrE)rrFparts r&rrsT  d  y dir%)rhtmlrDctjd|rtdttD]\}}||ks||krt|=|t|<dS)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&rrsy x 6""DBCCC^))++,,""1 88qF{{q! N3r%rr8rdfwsdlxsxsidc)$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/cRtd|dt|jd)Nzcannot serialize z (type ))r/rhr rs r&rrs1 )+/44d1D1D1DE   r%c d|vr|dd}d|vr|dd}d|vr|dd}|S#ttf$rt|YdSwxYw)N&&r<r>replacer/rrrs r&rrs ) $;;<<W--D $;;<<V,,D $;;<<V,,D ~ &)))"4(((((()AA A65A6c d|vr|dd}d|vr|dd}d|vr|dd}d|vr|dd}d |vr|d d }d |vr|d d }d |vr|d d}|S#ttf$rt|YdSwxYw)NrErFrrGrrHr" z rz  z rIrs r&rrs ) $;;<<W--D $;;<<V,,D $;;<<V,,D 4<<<<h//D 4<<<<g..D 4<<<<g..D 4<<<<g..D ~ &)))"4(((((()sB7B:: CCc d|vr|dd}d|vr|dd}d|vr|dd}|S#ttf$rt|YdSwxYw)NrErFrrHrrMrIrs r&r3r3*s ) $;;<<W--D $;;<<V,,D 4<<<<h//D ~ &)))"4(((((()rKT)rrrc|dkrtjntj}t||||||||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&rr9sm ')33R[]]]Fvx/>1B&,4H JJJ ??  r%c0eZdZdZdZdZdZdZdZdS)_ListDataStreamz7An auxiliary stream accumulating into a list reference.c||_dSrC)lst)r3rZs r&r5z_ListDataStream.__init__Ss r%cdSrr$r8s r&rz_ListDataStream.writableVtr%cdSrr$r8s r&rz_ListDataStream.seekableYr\r%c:|j|dSrC)rZr^)r3bs r&rz_ListDataStream.write\s r%c*t|jSrC)rHrZr8s r&rz_ListDataStream.tell_s48}}r%N) r r!r"r#r5rrrrr$r%r&rXrXQseAAr%rXcxg}t|}t||||||||S)NrR)rXrr)r*rrrrrrZrVs r&rrbsU C S ! !Fvx/>1B&,4H JJJ Jr%c t|tst|}|tjd|j}|r |ddkr!tjddSdS)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-rrsysstdoutrrE)rFrEs r&rros dK ( (!4  JJszIJ... <<>> D 48t## $#r% ct|tr|}|dkrtd|t |sdSd|zzgfd|ddS)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} |}n0#t$r#|z}|YnwxYw|jr|js||_|D]D}t |r |||jr|js||_E|js||_dSdSr ) IndexErrorr^rDstriprHrE)rFlevel child_levelchild_indentationchild_indent_children indentationsspaces r&roz indent.._indent_childrensai  3 ,[ 9   3 3 3 ,U 3e ;     1 2 2 2 2 2 3y *  1 1 *)DI / /E5zz 5   444: /UZ%5%5%7%7 /. z!! -%e,EJJJ - -s *>>)r-rrrrH)treerqrkrorps ` @@r&rrs$ $$||~~ qyyOOOPPP t9955=()L-------,T1r%cNt}||||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 )rrrrs r&r r s& ==DJJvv Kr%ct||fdGfddtjj}|d_~t 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_parserc3Kd} t|dst|d}d}dV Ed{V|d}|sn|H}Ed{V|_|r|dSdS#|r|wwxYw)NFrrTi@)r)r read_eventsrr_close_and_return_rootrootr)rrrrzit pullparsers r&iteratorziterparse..iterators  66** $fd++# JJJ &%11333333333{{9--%%%  &4466D!--// / / / / / / /BG    |   s B$CCc0eZdZjZdS)$iterparse..IterParseIteratorN)r r!r"__next__)r}rsr&IterParseIteratorrs!8F##,r%rN)r collectionsabcIteratorrznext)rrurrr{r}r|s` @@@r&r r s"ff===J*--------KO4---    BBG#HHH Ir%c4eZdZddddZdZdZdZdZdS) rN)rvctj|_|ptt |_|d}|j|j|dS)Nr)end)rdeque _events_queuerrrv _setevents)r3rurvs r&r5zXMLPullParser.__init__s[ ).00A);=="A"A"A >F  2F;;;;;r%c|jtd|rO |j|dS#t$r%}|j|Yd}~dSd}~wwxYwdS)Feed encoded data to parser.Nz!feed() called after end of stream)rvrr SyntaxErrorrr^)r3rexcs r&rzXMLPullParser.feeds < @AA A  / / !!$''''' / / /"))#......... / / /s6 A%A  A%cF|j}d|_|SrC)rvr)r3rzs r&ryz$XMLPullParser._close_and_return_roots!|!!##  r%c.|dS)zFinish feeding data to parser. Unlike XMLParser, does not return the root element. Use read_events() to consume elements from XMLPullParser. N)ryr8s r&rzXMLPullParser.closes ##%%%%%r%c#K|j}|r3|}t|tr||V|1dSdS)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)r3ruevents r&rxzXMLPullParser.read_eventsse # NN$$E%++        r%rC)r r!r"r5rryrrxr$r%r&rrsp >r%c|stt}|||}i}|D]}|d}|r|||<||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)rrrrrr|)rDrrridsrFr7s r&rr>s 1+--000 KK <<>>D C  XXd^^  CG 9r%c|stt}|D]}|||S)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)sequencerrDs r&rrVsR 1+--000 D <<>>r%cXeZdZdZdddddddZdZdZdZd Zd Z d Z dd Z d Z dS)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}||_ dSrC) _data_elem_lastr_tailr_comment_factoryrr _pi_factoryrr_factory)r3element_factoryrrrrs r&r5zTreeBuilder.__init__|sr      "%O /.  .J%$  "%O' r%cvt|jdks Jd|j Jd|jS)z;Flush builder buffers and return toplevel document Element.rzmissing end tagsNzmissing toplevel element)rHrrr8s r&rzTreeBuilder.closesC4:!###%7###z%%'A%%%zr%c|jrv|jfd|j}|jr#|jj Jd||j_n"|jj Jd||j_g|_dSdS)Nrzinternal error (tail)zinternal error (text))rrjoinrrErDr3rDs r&_flushzTreeBuilder._flushs : z%wwtz**:+:?224K222&*DJOO:?224K222&*DJODJJJ  r%c:|j|dS)zAdd text to current element.N)rr^r3rs r&rzTreeBuilder.datas $r%c||||x|_}|jr!|jd|n|j||_|j|d|_|S)zOpen new element and return it. *tag* is the element name, *attrs* is a dict containing element attributes. rcNr)rrrrr^rr)r3r(attrsrFs r&startzTreeBuilder.starts  MM#u555 T :  JrN ! !$ ' ' ' ' Z DJ $  r%c||j|_|jj|ksJd|jjd|dd|_|jS)zOClose and return current Element. *tag* is the element name. zend tag mismatch (expected z, got rCr)rrpoprr(rrs r&rzTreeBuilder.endsk Z^^%% z~$$$$:>>>333(%$$ zr%cD||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?? ?r%cF||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)r3rrDs r&pizTreeBuilder.pis* ""  dovt== =r%c||}|rI|||_|jr |jd|d|_|S)Nrcr)rrrr^r)r3factoryreargsrFs r&rzTreeBuilder._handle_singlesYw~   KKMMMDJz , 2%%d+++DJ r%rC) r r!r"r#r5rrrrrrrrr$r%r&rrhs&(!%$!&5((((((      "   ???====r%rc\eZdZdZddddZdZdZdZdZd Z d Z d Z d Z d Z dZdS)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)rrc@ ddlm}n3#t$r& ddl}n#t$rtdwxYwYnwxYw||d}|t }|x|_|_|x|_|_ |j |_ i|_ |j |_t|dr |j|_t|dr |j|_t|dr |j|_t|dr |j|_t|d r |j|_t|d r |j|_t|d r |j|_d |_d |_d|_ i|_! d |j"z|_#dS#tH$rYdSwxYw)Nrexpatz7No module named expat; use SimpleXMLTreeBuilder insteadrrrstart_nsend_nsrrrrzExpat %d.%d.%d)% xml.parsersr ImportErrorpyexpat ParserCreaterrrvr_targeterror_error_names_defaultDefaultHandlerExpandr)_startStartElementHandler_endEndElementHandler _start_nsStartNamespaceDeclHandler_end_nsEndNamespaceDeclHandlerrCharacterDataHandlerrCommentHandlerrProcessingInstructionHandler buffer_textordered_attributes_doctypeentity version_infoversionr)r3rrrrs r&r5zXMLParser.__init__s  ) ) ) ) ) ) )    '''''   !M (' ##Hc22 > ]]F%++ dl%++ dlk  &*m# 67 # # 5)-F & 65 ! ! 1'+yF $ 6: & & >/3~F , 68 $ $ :-1\F * 66 " " 6*0+F ' 69 % % 3$*NF ! 64  <28)F /$%!   +e.@@DLLL    DD s. 99399>F FFc|j}|j}|D]}|dkrd|_|||jfd}||_#|dkr|||jfd}||_=|dkr0t|jdr |||j fd}n||fd }||_ s|d kr0t|jd r |||j fd }n||fd }||_ |dkr|||fd}||_ |dkr|||fd}||_td|zdS)Nrrc4|||||fdSrCr$)r( attrib_inrr^rs r&handlerz%XMLParser._setevents..handler%s)FE55i#8#89:::::r%rc2||||fdSrCr$)r(rr^rs r&rz%XMLParser._setevents..handler*s%FE33s88,-----r%zstart-nsrc4|||||fdSrCr$)rrrr^rs r&rz%XMLParser._setevents..handler1s)xx'<'<=>>>>>r%c,|||pd|pdffdSNrr$)rrrr^s r&rz%XMLParser._setevents..handler5s* "ciR'@ABBBBBr%zend-nsrc2||||fdSrCr$)rrr^rs r&rz%XMLParser._setevents..handler;s%vvf~~677777r%c ||dfdSrCr$)rrr^s r&rz%XMLParser._setevents..handler?st}-----r%rcP|||j|fdSrC)rr)rDrr^r3s r&rz%XMLParser._setevents..handlerCs-FE4;#6#6t#<#<=>>>>>r%rcR|||j||fdSrC)rr) pi_targetrrr^r3s r&rz%XMLParser._setevents..handlerGs-FE4;>>)T#B#BCDDDDDr%zunknown event %r)rvr^rrrrrr)rrrrrrrr)r3 events_queueevents_to_reportrr^ event_namers r&rzXMLParser._seteventss$** B* BJW$$,-)2fCCCC3:00x''4;11..8'+|88888/9....18..y(((26????(/%%t##3=f!%EEEE7>33 !3j!@AAAU* B* Br%cbt|}|j|_|j|jf|_|rC)r codelinenooffsetposition)r3rerrs r& _raiseerrorzXMLParser._raiseerrorNs-:|U\1  r%cn |j|}n%#t$r|}d|vrd|z}||j|<YnwxYw|S)Nrr)rKeyError)r3r}names r&_fixnamezXMLParser._fixnameTs` $;s#DD $ $ $Dd{{Tz#DK     $  s 22c@|j|pd|pdSr)rrr3rrs r&rzXMLParser._start_ns_s"{##FLb#)<<tdt|dD]}||dz||||< |j||S)NrrKr)rrangerHrr)r3r( attr_listfixnamer1is r&rzXMLParser._startes-gcll  ?1c)nna00 ? ?09!A#wwy|,,--{  f---r%c\|j||SrC)rrrrs r&rzXMLParser._endqs"{t}}S11222r%c|dd}|dkr |jj}n#t$rYdSwxYw ||j|dddS#t$raddlm}|d||jj |jj fz}d|_ |jj |_ |jj |_ |wxYw|dkr|dd d kr g|_dS|j|d kr d|_dS|}|sdS|j|t#|j}|d kr|jd}|d kr|dkr|j\}}} } | r | dd} n|dkr|dkr|j\}}} d} ndSt%|jdr%|j|| | ddn*t%|drt)jdt,d|_dSdSdS)NrrErcrrz'undefined entity %s: line %d, column %d r z RuntimeWarning) r3rDr data_handlerrrnrhrpubidsystems r&rzXMLParser._defaultts}bqb S== #{/ !      T[ad455555   ------kk=4;6K133 ![8 ![:   s]]tBQBx;66DMMM ] &}} $ ::< AAA A) B) A63B5A66B B)r r!r"r#r5rrrrrrrrrrr$r%r&rrs"&+++++Z3B3B3Bj   ===000 . . .3334%4%4%l   *****r%r)out from_filec H||tdd}|tjx}}tt |jfi|}|*|||n|t|||| ndS)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) rrrSrrrrrr rU)xml_datarroptionssiors r&rrsI-UVVV C {KMM!c .syDDGDD E E EF H    i'''' _3<<>>>$6r%z ^\w+:\w+$ceZdZdZdddddddddZefdZdZddZd Z d j fd Z d Z d Z ddZdZdZdZdS)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_tagscX||_g|_||_||_|rt |nd|_|rt |nd|_||_|rt ||_nd|_|rt |j |_ nd|_ dgg|_ g|_ |s>|j tt|j gi|_dg|_d|_d|_d|_d|_dS)N)rArFr)_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) r3rrrr r!r"r#r$s r&r5zC14NWriterTarget.__init__sE  +%4AKc-000t2>HS...D!1  *%()9%:%:D " "%)D "  0+./@+A+A+ND ( (+/D ( <$ #  @ N ! !$~';';'='=">"> ? ? ? b!!! %w"r%c#:K||D] }|r|Ed{V dSrCr$)r3ns_stack _reversedrps r&_iter_namespacesz!C14NWriterTarget._iter_namespaces"sJ#)H-- & &J &%%%%%%%% & &r%c|dd\}}||jD]\}}||kr d|d|cStd|d|d)NrrrrzPrefix z of QName "" is not declared in scope)splitr:r0r)r3 prefixed_namerrrps r&_resolve_prefix_namez%C14NWriterTarget._resolve_prefix_name's$**322 ++DN;; * *FCF{{)C))4)))))_6__m___```r%c|4|dddkr|ddddnd|f\}}n|}t}||jD]4\}}||kr||vr|r|d|n|||fcS||5|jrd||jvr|j|}n!dt|jx}|j|<|jd||f|d|||fS|s d|vr|||fS||j D]=\}}||kr2|jd||f|r|d|n|||fcS>|s|||fStd|d ) Nrrrrrrrcz Namespace "r<) rrr:r/addr+r1rHr^r0r)r3rrr( prefixes_seenurs r&_qnamezC14NWriterTarget._qname.s$ ;38!93C3CuQRRy''Q///"eHCC ..t/FGG & &IAvCxxF-77,2;&((3(((S#EEEE   f % % % %  ! /d&&&)#.1LS9I5J5J1L1LL)#.  #B ' . .V} = = =$$s$$c3. . !r..S= ..t~>> F FIAvCxx'+22C=AAA,2;&((3(((S#EEEE !S= FsFFFGGGr%cL|js|j|dSdSrC)r6rr^rs r&rzC14NWriterTarget.dataSs3" $ J  d # # # # # $ $r%rcd||j}|jdd=|jr!|jds|}|j7|jdc}|_|rt |r|nd}|jg||R|dS|r+|jr&|t|dSdSdSNrc) rr(r2rjr3_looks_like_prefix_namerr4r&_escape_cdata_c14n)r3 _join_textrr qname_texts r&rzC14NWriterTarget._flushWsz$*%% JqqqM   D$8$< ::<D  F C/////r%cj"|r fd|D}|h|}i}|/|x}||<||jh|rf|}|rL|D]H} || } t | r/| x}|| <||Ind}nd}jfdt|dD} |r!d|D} | ng} |rlt|D]J\} }|| |vr||vr| ||d}| | \}} }| |r|n| |fK| d}j |r|dkn j d j }|d | |dz| r(|d d | D|d |*|t| ||dd_j gdS)Nc.i|]\}}|jv||Sr$)r)).0rrr3s r& z+C14NWriterTarget._start..s,TTTdaq@S7S7SQ7S7S7Sr%c(i|]}||Sr$r$)rRr parse_qnames r&rSz+C14NWriterTarget._start..s/444qKKNN444r%c.|ddS)Nrr)r=)rs r&rz)C14NWriterTarget._start..s!''#q//r%rc*g|]\}}|rd|znd|fS)zxmlns:xmlnsr$)rRrrs r& z+C14NWriterTarget._start..s@C'-9F""'3?r%rz+{http://www.w3.org/XML/1998/namespace}spacepreservercrrc@g|]\}}d|dt|dS)rrr)_escape_attrib_c14n)rRrrs r&rYz+C14NWriterTarget._start..s9TTT$!Q=q==$7$:$:===TTTr%rT)r)rr@rBr.rIrErsortr^r|r2r&rrJr4r0)r3r(rrOrLrresolved_namesrqattrs attr_namer parsed_qnamesrrr attr_qnamerspace_behaviourrrUs` @r&rzC14NWriterTarget._start~s6   *u *TTTTekkmmTTTE  !151J1J:1V1V VEN:. JJu     ' 3 311%88F !'**I!),E.u55*8<8Q8QRW8X8XXu 5 5))) * Fk 4444F 11535353444   #1I NN    I  Hu{{}}-- H H1%!v++!~:M:M%nQ&78;A-:1-=* Is  "B**A!FGGGG ))$QRR ##-< *Oz ) )%b) + + +   cM#&q))***  W E"''TT)TTTUU V V V c   ! E$]>*3M%Nq%QRR S S S b!!!!!r%c|jr|xjdzc_dS|jr||d||dd|jt|jdk|_|j |j dS)Nrrrr) r6rrr&rEr2rrHr5r/r0rs r&rzC14NWriterTarget.ends      1 $   F :  KKMMM /S))!,///000   """d233q8 ##%%% r%c0|jsdS|jrdS|jr|dn"|jr|jr||dt|d|js|ddSdS)Nrz)r'r6r5r&r4rrrJrs r&rzC14NWriterTarget.comments"  F    F ?  KK     _   KKMMM 8-d33888999  KK       r%c4|jrdS|jr|dn"|jr|jr|||rd|dt |dnd|d|js|ddSdS)Nrz)r6r5r&r4rrrJ)r3rrs r&rzC14NWriterTarget.pis    F ?  KK     _   KKMMM :> S 6 6 6,T22 6 6 6 6OOOO U U U  KK       r%rC)r r!r"r#r5reversedr:r@rErrrrrrrrrr$r%r&rrs, %"&$#$# # # # # J4<&&&& aaa#H#H#H#HJ$$$!# 2 2 2 2111000"C"C"C"C"J           r%rc& d|vr|dd}d|vr|dd}d|vr|dd}d|vr|dd}|S#ttf$rt|YdSwxYw) NrErFrrGrrHrN rIrs r&rJrJs) $;;<<W--D $;;<<V,,D $;;<<V,,D 4<<<<g..D ~ &)))"4(((((()sA)A,, BBc d|vr|dd}d|vr|dd}d|vr|dd}d|vr|dd}d |vr|d d }d |vr|d d }|S#ttf$rt|YdSwxYw) NrErFrrGrrMrOz rz rNrirIrs r&r\r\s) $;;<<W--D $;;<<V,,D $;;<<X..D 4<<<<g..D 4<<<<g..D 4<<<<g..D ~ &)))"4(((((()sBB CC)r)_set_factoriesrCr)rfr)?r#__all__rrdr:r=rrcollections.abcrrrrr r rrrrr rrcontextmanagerrrrr4r2rrrrrrrr3rrrXrrrr r rrrrrrrrcompileUNICODEr;rIrrJr\rg _elementtreerkrr$r%r&rrs!!P   (             ### jjjjjjjjZ $&$     +"+"+"+"+"+"+"+"`_/_/_/_/_/_/_/_/H /+/+/+b;;;;z0(0(0(d 0(0(0(d    !!!*-2$*38(.(,16(,  %3!   ))) )))8 ) ) )!T"&0b'" !%&*     &////l    ////d22222222j",     $vvvvvvvvt^*^*^*^*^*^*^*^*H7tt77777<%"*\2:>>DD)))&))). 3K++++++N7122222   DD s E33E<;E<