3 \@sHdZddddddddd d d d d ddddddddddgZdZddlZddlZddlZddlZddlZddlZddl m Z Gdd d e Z ddZ GdddZifd dZd^d!dZd_d"d ZeZGd#d d ZGd$ddZejd%d&Zd`d'd(Zd)d*ZdaZy eeZWnek r"YnXd8d9Zd:d;Zeeed<Zd=dZd>d?d@dAdBdCdDdEZ e e_ dFdGZ!dHdIZ"dJdKZ#dLdMZ$dbdNdOdPdZ%GdQdRdRej&Z'dcdNdOdSdZ(dTdZ)dddUd Z*dedVdZ+GdWddZ,dfdXdZ-dgdYdZ.e-Z/dhdZdZ0Gd[ddZ1Gd\ddZ2yeZ3dd]l4TWne5k rBYnXdS)iaLightweight 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 fromstringfromstringlist iselement iterparseparse ParseErrorPIProcessingInstructionQName SubElementtostring tostringlist TreeBuilderVERSIONXMLXMLID XMLParser XMLPullParserregister_namespacez1.3.0N) ElementPathc@seZdZdZdS)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__rr-/usr/lib64/python3.6/xml/etree/ElementTree.pyr hscCs t|dS)z2Return True if *element* appears to be an Element.tag)hasattr)elementrrr rvsc@s eZdZdZdZdZdZdZifddZddZ ddZ d d Z d d Z d dZ ddZddZddZddZddZddZddZddZdd Zd9d!d"Zd:d#d$Zd;d%d&Zdd3d4Z d?d5d6Z!d7d8Z"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 NcKsDt|tstd|jjf|j}|j|||_||_g|_ dS)Nzattrib must be dict, not %s) isinstancedict TypeError __class__rcopyupdater!attrib _children)selfr!r*extrarrr __init__s  zElement.__init__cCsd|jj|jt|fS)Nz<%s %r at %#x>)r'rr!id)r,rrr __repr__szElement.__repr__cCs |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. )r')r,r!r*rrr makeelements zElement.makeelementcCs0|j|j|j}|j|_|j|_||dd<|S)zReturn copy of current element. This creates a shallow copy. Subelements will be shared with the original tree. N)r1r!r*texttail)r,elemrrr r(s  z Element.copycCs t|jS)N)lenr+)r,rrr __len__szElement.__len__cCstjdtddt|jdkS)NzyThe behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.) stacklevelr)warningswarn FutureWarningr5r+)r,rrr __bool__s zElement.__bool__cCs |j|S)N)r+)r,indexrrr __getitem__szElement.__getitem__cCs||j|<dS)N)r+)r,r=r#rrr __setitem__szElement.__setitem__cCs |j|=dS)N)r+)r,r=rrr __delitem__szElement.__delitem__cCs|j||jj|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. N)_assert_is_elementr+append)r, subelementrrr rBs zElement.appendcCs(x|D]}|j|qW|jj|dS)zkAppend subelements from a sequence. *elements* is a sequence with zero or more elements. N)rAr+extend)r,elementsr#rrr rDs zElement.extendcCs|j||jj||dS)z(Insert *subelement* at position *index*.N)rAr+insert)r,r=rCrrr rFs zElement.insertcCs t|tstdt|jdS)Nzexpected an Element, not %s)r$ _Element_Pyr&typer)r,errr rAs zElement._assert_is_elementcCs|jj|dS)aRemove matching subelement. Unlike the find methods, this method compares elements based on identity, NOT ON tag value or contents. To remove subelements by other means, the easiest way is to use a list comprehension to select what elements to keep, and then use slice assignment to update the parent element. ValueError is raised if a matching element could not be found. N)r+remove)r,rCrrr rJs zElement.removecCstjdtdd|jS)z`(Deprecated) Return all subelements. Elements are returned in document order. zaThis method will be removed in future versions. Use 'list(elem)' or iteration over elem instead.r7)r8)r9r:DeprecationWarningr+)r,rrr getchildrens zElement.getchildrencCstj|||S)aFind first matching element by tag name or path. *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. )rfind)r,path namespacesrrr rM!s z Element.findcCstj||||S)aFind text for first matching element by tag name or path. *path* is a string having either an element tag or an XPath, *default* is the value to return if the element was not found, *namespaces* is an optional mapping from namespace prefix to full name. Return text content of first matching element, or default value if none was found. Note that if an element is found having no text content, the empty string is returned. )rfindtext)r,rNdefaultrOrrr rP,s zElement.findtextcCstj|||S)aFind all matching subelements by tag name or path. *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Returns list containing all matching elements in document order. )rfindall)r,rNrOrrr rR:s zElement.findallcCstj|||S)a Find all matching subelements by tag name or path. *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. )riterfind)r,rNrOrrr rSEs zElement.iterfindcCs |jjg|_d|_|_dS)zReset element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to None. N)r*clearr+r2r3)r,rrr rTPs z Element.clearcCs|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. )r*get)r,keyrQrrr rU[s z Element.getcCs||j|<dS)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)r*)r,rVvaluerrr sethsz Element.setcCs |jjS)zGet list of attribute names. Names are returned in an arbitrary order, just like an ordinary Python dict. Equivalent to attrib.keys() )r*keys)r,rrr rYrsz Element.keyscCs |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. )r*items)r,rrr rZ{s z Element.itemsccsH|dkr d}|dks|j|kr$|Vx|jD]}|j|EdHq,WdS)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!r+iter)r,r!rIrrr r\s  z Element.itercCstjdtddt|j|S)NzbThis method will be removed in future versions. Use 'elem.iter()' or 'list(elem.iter())' instead.r7)r8)r9r:PendingDeprecationWarninglistr\)r,r!rrr getiterators zElement.getiteratorccs^|j}t|t r|dk rdS|j}|r.|Vx*|D]"}|jEdH|j}|r4|Vq4WdS)zCreate text iterator. The iterator loops over the element and all subelements in document order, returning all inner text. N)r!r$strr2itertextr3)r,r!trIrrr ras zElement.itertext)N)NN)N)N)N)N)N)#rrrrr!r*r2r3r.r0r1r(r6r<r>r?r@rBrDrFrArJrLrMrPrRrSrTrUrXrYrZr\r_rarrrr r{s@            cKs,|j}|j||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)r1rB)parentr!r*r-r#rrr rs    cCstt}||_|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. )rrr2)r2r#rrr rs cCs&tt}||_|r"|jd||_|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.  )rr r2)targetr2r#rrr r s c@sZeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)r aQualified 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. NcCs|rd||f}||_dS)Nz{%s}%s)r2)r,Z text_or_urir!rrr r.s zQName.__init__cCs|jS)N)r2)r,rrr __str__sz QName.__str__cCsd|jj|jfS)Nz<%s %r>)r'rr2)r,rrr r0szQName.__repr__cCs t|jS)N)hashr2)r,rrr __hash__szQName.__hash__cCs t|tr|j|jkS|j|kS)N)r$r r2)r,otherrrr __le__s  z QName.__le__cCs t|tr|j|jkS|j|kS)N)r$r r2)r,rirrr __lt__ s  z QName.__lt__cCs t|tr|j|jkS|j|kS)N)r$r r2)r,rirrr __ge__s  z QName.__ge__cCs t|tr|j|jkS|j|kS)N)r$r r2)r,rirrr __gt__s  z QName.__gt__cCs t|tr|j|jkS|j|kS)N)r$r r2)r,rirrr __eq__s  z QName.__eq__)N) rrrrr.rfr0rhrjrkrlrmrnrrrr r s  c@seZdZdZdddZddZddZdd d Zdd d Zd d dZ d!ddZ d"ddZ d#ddZ d$ddZ d%ddddZddZdS)&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. NcCs||_|r|j|dS)N)_rootr )r,r#filerrr r.)szElementTree.__init__cCs|jS)z!Return root element of this tree.)ro)r,rrr getroot/szElementTree.getrootcCs ||_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! N)ro)r,r#rrr _setroot3szElementTree._setrootc Csd}t|dst|d}d}zZ|dkrHt}t|drH|j||_|jSx|jd}|sZP|j|qJW|j|_|jS|r|jXdS)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"openrrurorsfeedclose)r,sourceparser close_sourcedatarrr r =s&       zElementTree.parsecCs |jj|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). )ror\)r,r!rrr r\bs zElementTree.itercCstjdtddt|j|S)NzbThis method will be removed in future versions. Use 'tree.iter()' or 'list(tree.iter())' instead.r7)r8)r9r:r]r^r\)r,r!rrr r_os zElementTree.getiteratorcCs:|dddkr,d|}tjd|tdd|jj||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/.zThis 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 %rr7)r8)r9r:r;rorM)r,rNrOrrr rMxs  zElementTree.findcCs<|dddkr,d|}tjd|tdd|jj|||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. Nrr}r~zThis 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 %rr7)r8)r9r:r;rorP)r,rNrQrOrrr rPs  zElementTree.findtextcCs:|dddkr,d|}tjd|tdd|jj||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. Nrr}r~zThis 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 %rr7)r8)r9r:r;rorR)r,rNrOrrr rRs  zElementTree.findallcCs:|dddkr,d|}tjd|tdd|jj||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. Nrr}r~zThis 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 %rr7)r8)r9r:r;rorS)r,rNrOrrr rSs  zElementTree.iterfindT)short_empty_elementscCs|s d}n|tkrtd||s4|dkr0d}nd}|j}t||}|dkr|sd|dkr|d kr|} |dkrddl} | j} |d | f|d krt||jn,t|j|\} } t|} | ||j| | |d WdQRXdS) 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-asciiNunicoderz$ r2)r)rrr) _serialize ValueErrorlower _get_writerlocalegetpreferredencoding_serialize_textro _namespaces)r,file_or_filenameencodingZxml_declarationdefault_namespacemethodrZ enc_lowerwriteZdeclared_encodingrqnamesrOZ serializerrr rs2     zElementTree.writecCs|j|ddS)Nr)r)r)r,rprrr write_c14n szElementTree.write_c14n)NN)N)N)N)N)NN)N)N)NNNN)rrrrr.rqrrr r\r_rMrPrRrSrrrrrr rs"   %    5ccs"y |j}WnPtk rZ|dkr.t|d}nt|d|dd}||jVWdQRXYnX|dkrl|Vntj}t|tjr|}nft|tjrtj |}|j |j nBtj}dd|_ ||_y|j |_ |j|_Wntk rYnXtj||ddd}|j |j |jVWdQRXdS) Nrwxmlcharrefreplace)rerrorscSsdS)NTrrrrr 3sz_get_writer.. )rrnewline)rAttributeErrorrv contextlib ExitStackr$ioBufferedIOBase RawIOBaseBufferedWritercallbackdetachwritableseekabletell TextIOWrapper)rrrrpstackrrr rs>         rcsddiird<fdd}x|jD]}|j}t|tr\|jkr||jn.add_qname) r\r!r$r r2r`rr rrZ)r4rrr!rVrWr2r)rrOrr rEs4      rc Ks|j}|j}|tkr$|d|n|tkr<|d|nv||}|dkr|r\|t|x|D]}t|||d|dqbWn2|d|t|j} | s|rD|rx@t|jdddD](\} } | rd| } |d | t | fqWxZt| D]N\} } t | t r | j} t | t r$|| j} nt | } |d || | fqW|s\t |s\| r|d |rv|t|x |D]}t|||d|dq|W|d |d n|d |j r|t|j dS)Nz z)r.)rV:z xmlns%s="%s"z %s="%s">z)r!r2rr _escape_cdata_serialize_xmlr^rZsorted_escape_attribr$r r5r3) rr4rrOrkwargsr!r2rIrZvkrrr rsT             rareabasebasefontbrcolframehrimginputisindexlinkmetaparamc Ks|j}|j}|tkr(|dt|n|tkrD|dt|n|||}|dkr|rd|t|x|D]}t|||dqjWn<|d|t|j}|s|rH|rx@t|jdddD](\} } | rd| } |d| t | fqWxZt|D]N\} } t | t r| j} t | t r(|| j} nt | } |d || | fqW|d |j } |r| d ksr| d kr|||n |t|x|D]}t|||dqW| tkr|d |d |jr|t|jdS)Nz zrcSs|dS)Nrr)rrrr rsz!_serialize_html..)rVrz xmlns%s="%s"z %s="%s"rZscriptZstylez|jdd}|Sttfk r`t|YnXdS)N&z&rz<rz>)replacer&rr)r2rrr r$s   rc Csyd|kr|jdd}d|kr*|jdd}d|kr>|jdd}d|krR|jdd}d |krf|jd d }d |krz|jd d }d |kr|jd d }d |kr|jd d}|Sttfk rt|YnXdS)Nrz&rz<rz>"z"z r z  z )rr&rr)r2rrr r4s(        rc Csfy@d|kr|jdd}d|kr*|jdd}d|kr>|jdd}|Sttfk r`t|YnXdS)Nrz&rz>rz")rr&rr)r2rrr rPs   rT)rcCs6|dkrtjntj}t|j||||d|jS)aGenerate 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". Returns an (optionally) encoded string containing the XML data. r)rr)rStringIOBytesIOrrgetvalue)r#rrrstreamrrr r_sc@s8eZdZdZddZddZddZdd Zd d Zd S) _ListDataStreamz7An auxiliary stream accumulating into a list reference.cCs ||_dS)N)lst)r,rrrr r.tsz_ListDataStream.__init__cCsdS)NTr)r,rrr rwsz_ListDataStream.writablecCsdS)NTr)r,rrr rzsz_ListDataStream.seekablecCs|jj|dS)N)rrB)r,brrr r}sz_ListDataStream.writecCs t|jS)N)r5r)r,rrr rsz_ListDataStream.tellN) rrrrr.rrrrrrrr rrs rcCs&g}t|}t|j||||d|S)N)rr)rrr)r#rrrrrrrr rs cCsNt|tst|}|jtjdd|jj}| s>|ddkrJtjjddS)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)rrrN)r$rrsysstdoutrqr3)r4r3rrr rs  cCst}|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 )ryrztreerrr r s  csdt||dfddGfdddtj}|d_~dtds`td d 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_parserc 3sbzNx,jEdHjd}|s"Pj|qWj}jEdH|_Wdr\jXdS)Nii@) read_eventsrsrw_close_and_return_rootrootrx)r|r)r{it pullparserryrr iterators  ziterparse..iteratorcseZdZjZdS)z$iterparse..IterParseIteratorN)rrr__next__r)rrr IterParseIteratorsrNFrsrtT)r collectionsIteratorrr"rv)ryrrzrr)r{rrrryr rs   c@s<eZdZd ddddZddZddZd d Zd d ZdS)rN)rcCs<tj|_|pttd|_|dkr(d}|jj|j|dS)N)reend)r)rdeque _events_queuerrr _setevents)r,rrrrr r.s  zXMLPullParser.__init__cCsZ|jdkrtd|rVy|jj|Wn.tk rT}z|jj|WYdd}~XnXdS)zFeed encoded data to parser.Nz!feed() called after end of stream)rrrw SyntaxErrorrrB)r,r|excrrr rws zXMLPullParser.feedcCs|jj}d|_|S)N)rrx)r,rrrr rs z$XMLPullParser._close_and_return_rootcCs |jdS)zFinish feeding data to parser. Unlike XMLParser, does not return the root element. Use read_events() to consume elements from XMLPullParser. N)r)r,rrr rxszXMLPullParser.closeccs2|j}x&|r,|j}t|tr$|q|VqWdS)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)r,reventrrr rs  zXMLPullParser.read_events)N)rrrr.rwrrxrrrrr rs   cCs"|sttd}|j||jS)aParse XML document from string constant. This function can be used to embed "XML Literals" in Python code. *text* is a string containing XML data, *parser* is an optional parser instance, defaulting to the standard XMLParser. Returns an Element instance. )re)rrrwrx)r2rzrrr rs   cCsV|sttd}|j||j}i}x&|jD]}|jd}|r0|||<q0W||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. )rer/)rrrwrxr\rU)r2rzrZidsr4r/rrr r&s     cCs0|sttd}x|D]}|j|qW|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. )re)rrrwrx)Zsequencerzr2rrr r>s   c@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)raGeneric 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. NcCs.g|_g|_d|_d|_|dkr$t}||_dS)N)_data_elem_last_tailr_factory)r,Zelement_factoryrrr r.]szTreeBuilder.__init__cCs|jS)z;Flush builder buffers and return toplevel document Element.)r)r,rrr rxfszTreeBuilder.closecCs>|jr:|jdk r4dj|j}|jr,||j_n||j_g|_dS)Nr)rrjoinrr3r2)r,r2rrr _flushls   zTreeBuilder._flushcCs|jj|dS)zAdd text to current element.N)rrB)r,r|rrr r|xszTreeBuilder.datacCsF|j|j|||_}|jr0|jdj||jj|d|_|S)zOpen new element and return it. *tag* is the element name, *attrs* is a dict containing element attributes. rrr)rrrrrBr)r,r!Zattrsr4rrr start|s zTreeBuilder.startcCs |j|jj|_d|_|jS)zOClose and return current Element. *tag* is the element name. r)rrpoprr)r,r!rrr rs zTreeBuilder.end)N) rrrrr.rxrr|rrrrrr rPs   c@sfeZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ e Z ddZ ddZdS)raElement structure builder for XML source data based on the expat parser. *html* are predefined HTML entities (deprecated and not supported), *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 rNcCs<yddlm}Wn>tk rNy ddl}Wntk rHtdYnXYnX|j|d}|dkrjt}||_|_||_|_ |j |_ i|_ |j |_t|dr|j|_t|dr|j|_t|dr|j|_t|dr|j|_t|d r|j|_d |_d |_d |_d|_i|_yd |j|_ Wnt!k r6YnXdS) Nr)expatz7No module named expat; use SimpleXMLTreeBuilder insteadrrrr|commentpirzExpat %d.%d.%d)" xml.parsersr ImportErrorZpyexpatZ ParserCreaterrzrre_targeterror_error_names_defaultZDefaultHandlerExpandr"_startStartElementHandler_endEndElementHandlerr|ZCharacterDataHandlerrZCommentHandlerrZProcessingInstructionHandlerZ buffer_textordered_attributesspecified_attributes_doctypeentity version_infoversionr)r,rrerrrzrrr r.sF         zXMLParser.__init__cCs|j}|j}x|D]}|dkrDd|_d|_|||jfdd}||_q|dkrf|||jfdd}||_q|dkr||fdd}||_q|d kr||fd d}||_ qt d |qWdS) NrrcSs|||||fdS)Nr)r!Z attrib_inrrBrrrr handlersz%XMLParser._setevents..handlerrcSs||||fdS)Nr)r!rrBrrrr rszstart-nscSs|||p d|pdffdS)Nrr)rrrrBrrr rszend-nscSs||dfdS)Nr)rrrBrrr rszunknown event %r) rrBr rr r r r ZStartNamespaceDeclHandlerZEndNamespaceDeclHandlerr)r,Z events_queueZevents_to_reportrzrBZ event_namerrrr rs( zXMLParser._seteventscCs&t|}|j|_|j|jf|_|dS)N)r codelinenooffsetZposition)r,rWerrrrr _raiseerrorszXMLParser._raiseerrorc CsFy|j|}Wn2tk r@|}d|kr2d|}||j|<YnX|S)Nrr)rKeyError)r,rVnamerrr _fixnameszXMLParser._fixnamecCsV|j}||}i}|rHx0tdt|dD]}||d||||<q(W|jj||S)Nrr7r)rranger5rer)r,r!Z attr_listZfixnamer*irrr r szXMLParser._startcCs|jj|j|S)N)rerr)r,r!rrr r szXMLParser._endc Cs|dd}|dkry |jj}Wntk r4dSXy||j|ddWnZtk rddlm}|jd||jj |jj f}d|_ |jj |_ |jj |_ |YnXnD|dkr|ddd krg|_n"|jdk r|d krd|_dS|j}|sdS|jj|t|j}|d kr|jd}|d krb|d krb|j\}}} } | r| dd} n*|dkr|dkr|j\}}} d} ndSt|jdr|jj|| | ddn:|j|jkr|j|| | dd|j|| | ddd|_dS)Nrrr)rz'undefined entity %s: line %d, column %d r z "s) A   0t4 =2 2  05   KU