o ?OgL"@sdZgdZdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z Gddde Z d d ZGd d d Zifd dZd^ddZd^ddZeZGdddZGdddZe jddZd^ddZddZdZzeeZWn ey~YnwddZd d!Zeeed"Zd#d$Z d%d&d'd(d)d*d+d,Z!e!e _!d-d.Z"d/d0Z#d1d2Z$d3d4Z%d_ddd5d6d7d8Z&Gd9d:d:ej'Z(d_ddd5d6d;d<Z)d=d>Z*d`d@dAZ+d^dBdCZ,d_dDdEZ-GdFdGdGZ.d^dHdIZ/d^dJdKZ0e/Z1d^dLdMZ2GdNdOdOZ3GdPdQdQZ4d^dddRdSdTZ5e6dUej7j8Z9GdVdWdWZ:dXdYZ;dZd[ZTdd]l>m?Z?Wn e@y=YdSwe?eedS)aaLightweight 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) 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__r"r"text...tail NcKs:t|tstd|jjf||_i|||_g|_dS)Nzattrib must be dict, not %s) isinstancedict TypeError __class__rr$attrib _children)selfr$r+extrar"r"r#__init__s  zElement.__init__cCsd|jj|jt|fS)Nz<%s %r at %#x>)r*rr$idr-r"r"r#__repr__zElement.__repr__cCs |||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+r"r"r# makeelements zElement.makeelementcCstdt|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__r1r"r"r#copys z Element.copycCs0||j|j}|j|_|j|_||dd<|SN)r4r$r+texttail)r-elemr"r"r#r8s  zElement.__copy__cC t|jSr:)lenr,r1r"r"r#__len__ zElement.__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)r5r6 FutureWarningr?r,r1r"r"r#__bool__s zElement.__bool__cCs |j|Sr:r,r-indexr"r"r# __getitem__rAzElement.__getitem__cCs8t|tr|D]}||qn||||j|<dSr:)r'slice_assert_is_elementr,)r-rIr&eltr"r"r# __setitem__s   zElement.__setitem__cCs |j|=dSr:rGrHr"r"r# __delitem__s zElement.__delitem__cCs|||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. NrLr,appendr- subelementr"r"r#rQs zElement.appendcCs$|D] }|||j|qdS)zkAppend subelements from a sequence. *elements* is a sequence with zero or more elements. NrP)r-elementsr&r"r"r#extends zElement.extendcCs|||j||dS)z(Insert *subelement* at position *index*.N)rLr,insert)r-rIrSr"r"r#rVs zElement.insertcCs t|tstdt|jdS)Nzexpected an Element, not %s)r' _Element_Pyr)typer)r-er"r"r#rLs zElement._assert_is_elementcC|j|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,removerRr"r"r#r[ s zElement.removecCt|||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. )rfindr-path namespacesr"r"r#r] z Element.findcCst||||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. )rfindtextr-r_defaultr`r"r"r#rb&s zElement.findtextcCr\)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. )rfindallr^r"r"r#re4razElement.findallcCr\)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. )riterfindr^r"r"r#rf?razElement.iterfindcCs |jg|_d|_|_dS)zReset element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to None. N)r+clearr,r;r<r1r"r"r#rgJs z Element.clearcCs|j||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-keyrdr"r"r#rhUs 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-rivaluer"r"r#setbsz Element.setcC |jS)zGet list of attribute names. Names are returned in an arbitrary order, just like an ordinary Python dict. Equivalent to attrib.keys() )r+keysr1r"r"r#rmls z Element.keyscCrl)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+itemsr1r"r"r#rnus z Element.itemsccsF|dkrd}|dus|j|kr|V|jD] }||EdHqdS)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$rYr"r"r#rps z Element.iterccsZ|j}t|ts|durdS|j}|r|V|D]}|EdH|j}|r*|VqdS)zCreate text iterator. The iterator loops over the element and all subelements in document order, returning all inner text. N)r$r'strr;itertextr<)r-r$trYr"r"r#rrszElement.itertextr:NN)"rrr r!r$r+r;r<r/r2r4r9r8r@rFrJrNrOrQrUrVrLr[r]rbrerfrgrhrkrmrnrprrr"r"r"r#r}sH            rcKs&i||}|||}|||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. )r4rQ)parentr$r+r.r&r"r"r#rs  rcCstt}||_|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&r"r"r#rs rcCs&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 r;)targetr;r&r"r"r#r s  r c@sZeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)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. NcCs|rd||f}||_dS)Nz{%s}%sr;)r-Z text_or_urir$r"r"r#r/s  zQName.__init__cC|jSr:rxr1r"r"r#__str__sz QName.__str__cCsd|jj|jfS)Nz<%s %r>)r*rr;r1r"r"r#r2zQName.__repr__cCr>r:)hashr;r1r"r"r#__hash__rAzQName.__hash__cCs t|tr |j|jkS|j|kSr:r'rr;r-otherr"r"r#__le__   z QName.__le__cCs t|tr |j|jkS|j|kSr:r~rr"r"r#__lt__rz QName.__lt__cCs t|tr |j|jkS|j|kSr:r~rr"r"r#__ge__rz QName.__ge__cCs t|tr |j|jkS|j|kSr:r~rr"r"r#__gt__rz QName.__gt__cCs t|tr |j|jkS|j|kSr:r~rr"r"r#__eq__rz QName.__eq__r:) rrr r!r/rzr2r}rrrrrr"r"r"r#rs  rc@seZdZdZdddZddZddZdd d Zdd d Zdd dZ dddZ dddZ dddZ    dddddZ ddZdS)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 ||dSdSr:)_rootr )r-r&filer"r"r#r/szElementTree.__init__cCry)z!Return root element of this tree.rr1r"r"r#getrootszElementTree.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! Nr)r-r&r"r"r#_setroot"s zElementTree._setrootcCsd}t|dst|d}d}z<|dur,t}t|dr,|||_|jW|r+|SS |d}|s5n||q-||_|jW|rJ|SS|rR|ww)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)r-sourceparser close_sourcedatar"r"r#r ,s4          zElementTree.parsecCs |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). )rrpr-r$r"r"r#rpQs zElementTree.itercC:|dddkrd|}tjd|tdd|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 %rrBrC)r5r6rErr]r^r"r"r#r]] zElementTree.findcCs<|dddkrd|}tjd|tdd|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. NrrrrrBrC)r5r6rErrbrcr"r"r#rbss zElementTree.findtextcCr)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. NrrrrrBrC)r5r6rErrer^r"r"r#rerzElementTree.findallcCr)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. NrrrrrBrC)r5r6rErrfr^r"r"r#rfrzElementTree.iterfindTshort_empty_elementsc Cs|sd}n |tvrtd||s|dkrd}nd}t||Q\}}|dkr?|s8|dur?|dkr?|dvr?|d |f|d krJt||jnt|j|\} } t|} | ||j| | |d WddSWddS1sswYdS) 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$ r;r) _serialize ValueError _get_writerlower_serialize_textr _namespaces) r-file_or_filenameencodingxml_declarationdefault_namespacemethodrwriteZdeclared_encodingqnamesr`Z serializer"r"r#rs4     "zElementTree.writecCs|j|ddS)Nr)r)r)r-rr"r"r# write_c14nszElementTree.write_c14nrtr:)NNNN)rrr r!r/rrr rpr]rbrerfrrr"r"r"r#r s&  %    5rc csfz|j}Wn2ty8|dkrd}t|d|dd}|j|fVWdYdS1s0wYYdSw|dkrL|t|ddpGdfVdStY}t|tj rZ|}n2t|tj rlt |}| |j n t }dd|_||_z |j|_|j|_Wn tyYnwtj||dd d }| |j |j|fVWddS1swYdS) Nrrwxmlcharrefreplace)rerrorsrcSdSNTr"r"r"r"r#sz_get_writer.. )rrnewline)rAttributeErrorrrgetattr contextlib ExitStackr'ioBufferedIOBase RawIOBaseBufferedWritercallbackdetachwritableseekabletell TextIOWrapper)rrrrstackr"r"r#rsL   &         "rcsddiir d<fdd}|D]m}|j}t|tr-|jvr,||jnt|tr;|vr:||n|durK|turK|turKt|| D]#\}}t|tr[|j}|vrc||t|trr|jvrr||jqO|j}t|tr|jvr||jqfS)NcszU|dddkrI|dddd\}}|}|dur5t|}|dur-dt}|dkr5||<|rBd||f|<WdS||<WdSrOtd||<WdStyct|YdSw)Nr{}zns%drz%s:%sz.add_qname) rpr$r'rr;rqrr rrn)r=rrr$rirjr;r"rr#r#s<        rc Ks|j}|j}|tur|d|n|tur|d|n||}|dur;|r,|t||D] }t|||d|dq.n|d|t|} | sK|r|rlt|dddD]\} } | rad| } |d | t | fqW| D]%\} } t | t rz| j} t | t r|| j} nt | } |d || | fqn|st |s|s|d |r|t||D] }t|||d|dq|d |d n|d |j r|t|j dSdS)N r.ri: xmlns%s="%s" %s="%s">)r$r;rr _escape_cdata_serialize_xmllistrnsorted_escape_attribr'rr?r<) rr=rr`rkwargsr$r;rYrnvkr"r"r#r`s`          r) ZareabaseZbasefontbrcolframeZhrZimginputZisindexlinkmetaZparamc Ks|j}|j}|tur|dt|n|tur |dt|n||}|dur=|r0|t||D] }t|||dq2n|d|t|}|sM|r|rnt|dddD]\} } | rcd| } |d| t | fqY|D]%\} } t | t r|| j} t | t r|| j} nt | } |d || | fqp|d | } |r| d ks| d kr||n|t||D] }t|||dq| tvr|d |d |jr|t|jdSdS)NrrrcSrrr"rr"r"r#rrz!_serialize_html..rrrrrZscriptZstyler)r$r;rrr _serialize_htmlrrnrrr'r_escape_attrib_htmlr HTML_EMPTYr<) rr=rr`rr$r;rYrnrrZltagr"r"r#rs\         rcCs.|D]}||q|jr||jdSdSr:)rrr<)rr=partr"r"r#rs  r)rhtmlr;cCsLtd|r tdttD]\}}||ks||krt|=q|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)rematchrrrrn)rrrrr"r"r#rs  rrrZrdfZwsdlZxsZxsiZdc)$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/cCstd|t|jf)Nzcannot serialize %r (type %s))r)rXrrxr"r"r#rsrc Cdz!d|vr |dd}d|vr|dd}d|vr|dd}|WSttfy1t|YdSw)N&&r<r>replacer)rrrxr"r"r#rs   rc CszId|vr |dd}d|vr|dd}d|vr|dd}d|vr)|dd}d |vr3|d d }d |vr=|d d }d |vrG|d d}|WSttfyYt|YdSw)Nrrrrrr"" z rz  z rrxr"r"r#rs&       rc Cr)Nrrrrrrrrxr"r"r#r.s   rT)rrrcCs:|dkrtnt}t|j||||||d|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&rrrrrstreamr"r"r#r=s rc@s8eZdZdZddZddZddZdd Zd d Zd S) _ListDataStreamz7An auxiliary stream accumulating into a list reference.cCs ||_dSr:)lst)r-r r"r"r#r/WrAz_ListDataStream.__init__cCrrr"r1r"r"r#rZz_ListDataStream.writablecCrrr"r1r"r"r#r]r z_ListDataStream.seekablecCs|j|dSr:)r rQ)r-br"r"r#r`z_ListDataStream.writecCr>r:)r?r r1r"r"r#rcrAz_ListDataStream.tellN) rrr r!r/rrrrr"r"r"r#rUs rcCs*g}t|}t|j||||||d|S)Nr)rrr)r&rrrrrr rr"r"r#rfs rcCsPt|ts t|}|jtjdd|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'rrsysstdoutrr<)r=r<r"r"r#rss  r cs`t|tr |}|dkrtd|t|sdSd|gfdd|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 Nrcs|d}z|}Wnty|}|Ynw|jr'|js*||_|D]}t|r7|||jr?|jsB||_q,|jsO||_dSdSr) IndexErrorrQr;stripr?r<)r=levelZ child_levelZchild_indentationchild_indent_childrenZ indentationsspacer"r#rs$     z indent.._indent_children)r'rrrr?)treerrr"rr#rs rcCst}||||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 )rrrr"r"r#r s r csPt||dfddGfdddtjj}|d_~tS)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_parserc3sd}z>t|dst|d}d}dV EdH|d}|s#n|q}EdH|_W|r@|dSdS|rI|ww)NFrrTi@)r%r read_eventsrr_close_and_return_rootrootr)rrrr)it pullparserr"r#iterators,      ziterparse..iteratorcseZdZjZdS)z$iterparse..IterParseIteratorN)rrr __next__r")r rr"r#IterParseIteratorsr"N)r collectionsabcIteratorrnext)rrrr"r")rr rrr#r s r c@sDeZdZdddddZddZddZd d Zd d Zd dZdS)rN)rcCs<t|_|p ttd|_|durd}|j|j|dS)Nrw)end)r#deque _events_queuerrr _setevents)r-rrr"r"r#r/s zXMLPullParser.__init__c Cs`|jdur td|r.z |j|WdSty-}z |j|WYd}~dSd}~wwdS)Feed encoded data to parser.Nz!feed() called after end of stream)rrr SyntaxErrorr*rQ)r-rexcr"r"r#r s zXMLPullParser.feedcCs|j}d|_|Sr:)rr)r-rr"r"r#rs z$XMLPullParser._close_and_return_rootcCs |dS)zFinish feeding data to parser. Unlike XMLParser, does not return the root element. Use read_events() to consume elements from XMLPullParser. N)rr1r"r"r#rs zXMLPullParser.closeccs4|j}|r|}t|tr||V|sdSdS)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)r*popleftr' Exception)r-reventr"r"r#r"s  zXMLPullParser.read_eventscCs |jdur td|jdS)Nz"flush() called after end of stream)rrflushr1r"r"r#r20s zXMLPullParser.flushr:) rrr r/rrrrr2r"r"r"r#rs   rcCs"|sttd}|||S)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. r'rrrr)r;rr"r"r#r6s  rcCsR|sttd}|||}i}|D] }|d}|r$|||<q||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. r'r0)rrrrrprh)r;rrZidsr=r0r"r"r#rGs    rcCs,|sttd}|D]}||q |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. r'r3)Zsequencerr;r"r"r#r_s   rc@sheZdZdZddddddddZddZd d Zd d Zd dZddZ ddZ dddZ ddZ 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_piscCsdg|_g|_d|_d|_d|_|durt}||_||_|dur!t}||_ ||_ |dur-t }||_ dSr:) _data_elem_lastr_tailr_comment_factoryr6r _pi_factoryr7r_factory)r-Zelement_factoryr4r5r6r7r"r"r#r/s  zTreeBuilder.__init__cCs.t|jdks Jd|jdusJd|jS)z;Flush builder buffers and return toplevel document Element.rzmissing end tagsNzmissing toplevel element)r?r9rr1r"r"r#rszTreeBuilder.closecCsj|jr3|jdur.d|j}|jr |jjdusJd||j_n|jjdus*Jd||j_g|_dSdS)Nrzinternal error (tail)zinternal error (text))r8r:joinr;r<r;r-r;r"r"r#_flushs    zTreeBuilder._flushcCrZ)zAdd text to current element.N)r8rQr-rr"r"r#rszTreeBuilder.datacCsX|||||_}|jr|jd|n|jdur!||_|j|d|_|S)zOpen new element and return it. *tag* is the element name, *attrs* is a dict containing element attributes. r Nr)rAr>r:r9rQrr;)r-r$attrsr=r"r"r#starts  zTreeBuilder.startcCs@||j|_|jj|ksJd|jj|fd|_|jS)zOClose and return current Element. *tag* is the element name. z&end tag mismatch (expected %s, got %s)r)rAr9popr:r$r;rr"r"r#r(s zTreeBuilder.endcCs||j|j|S)z`Create a comment using the comment_factory. *text* is the text of the comment. )_handle_singler<r6r@r"r"r#comments zTreeBuilder.commentcCs||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 ''. )rFr=r7)r-rwr;r"r"r#pis zTreeBuilder.picGs:||}|r|||_|jr|jd|d|_|S)Nr r)rAr:r9rQr;)r-factoryrVargsr=r"r"r#rFszTreeBuilder._handle_singler:) rrr r!r/rrArrDr(rGrHrFr"r"r"r#rqs    rc@sxeZdZdZdddddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZdS)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)rwrc CsNzddlm}Wnty#zddl}Wn ty tdwYnw||d}|dur1t}||_|_||_|_ |j |_ i|_ |j |_t|drQ|j|_t|drZ|j|_t|drc|j|_t|drl|j|_t|d ru|j|_t|d r~|j|_t|d r|j|_d |_d |_d|_ i|_!z d |j"|_#WdSt$yYdSw)Nrexpatz7No module named expat; use SimpleXMLTreeBuilder insteadrrDr(start_nsend_nsrrGrHrzExpat %d.%d.%d)% xml.parsersrL ImportErrorZpyexpatZ ParserCreaterrrrw_targeterror_error_names_defaultZDefaultHandlerExpandr%_startStartElementHandler_endEndElementHandler _start_nsStartNamespaceDeclHandler_end_nsEndNamespaceDeclHandlerrZCharacterDataHandlerrGCommentHandlerrHProcessingInstructionHandlerZ buffer_textordered_attributes_doctypeentity version_infoversionr)r-rwrrLrr"r"r#r/sV              zXMLParser.__init__cCs*|j}|j}|D]}|dkrd|_|||jfdd}||_q|dkr/|||jfdd}||_q|dkrNt|jdrC|||j fd d}n||fd d}||_ q|d krmt|jd rb|||j fd d}n||fdd}||_ q|dkr}|||fdd}||_ q|dkr|||fdd}||_qtd|dS)NrDrcS|||||fdSr:r")r$Z attrib_inr1rQrDr"r"r#handler.z%XMLParser._setevents..handlerr(cS||||fdSr:r")r$r1rQr(r"r"r#rf3zstart-nsrMcSrer:r")rrr1rQrMr"r"r#rf:rgcSs|||pd|pdffdSNrr")rrr1rQr"r"r#rf>szend-nsrNcSrhr:r")rr1rQrNr"r"r#rfDricSs||dfdSr:r")rr1rQr"r"r#rfHr rGcSs|||j|fdSr:)rwrG)r;r1rQr-r"r"r#rfLr3rHcSs|||j||fdSr:)rwrH)Z pi_targetrr1rQr-r"r"r#rfPszunknown event %r)rrQr`rVrWrXrYr%rwrZr[r\r]r^r_r)r-Z events_queueZevents_to_reportrrQZ event_namerfr"r"r#r+"sL        zXMLParser._seteventscCs"t|}|j|_|j|jf|_|r:)r codelinenooffsetZposition)r-rjerrr"r"r# _raiseerrorWszXMLParser._raiseerrorcCsDz|j|}W|Sty!|}d|vrd|}||j|<Y|Sw)Nrr)rTKeyError)r-rinamer"r"r#_fixname]s   zXMLParser._fixnamecCs|j|pd|p dSrj)rwrMr-rrr"r"r#rZhszXMLParser._start_nscCs|j|pdSrj)rwrN)r-rr"r"r#r\kr zXMLParser._end_nscCsR|j}||}i}|r"tdt|dD]}||d||||<q|j||S)NrrBr)rrranger?rwrD)r-r$ attr_listZfixnamer+ir"r"r#rVnszXMLParser._startcCs|j||Sr:)rwr(rrrr"r"r#rXzr{zXMLParser._endc Cs|dd}|dkrTz|jj}Wn tyYdSwz||j|ddWdStySddlm}|d||jj |jj f}d|_ |jj |_ |jj |_ |w|dkre|dd d kreg|_dS|jdur|d krsd|_dS|}|s{dS|j|t|j}|d kr|jd}|d kr|dkr|j\}}} } | r| dd} n|dkr|dkr|j\}}} d} ndSt|jdr|j|| | ddn t|drtdtd|_dSdSdS)Nrrr rrKz'undefined entity %s: line %d, column %d r z s z+C14NWriterTarget._start..csi|]}||qSr"r")rr}) parse_qnamer"r#rscSs |ddS)Nrr)r)r}r"r"r#rs z)C14NWriterTarget._start..rcSs$g|]\}}|r d|nd|fqS)zxmlns:Zxmlnsr")rrrr"r"r# sz+C14NWriterTarget._start..rz+{http://www.w3.org/XML/1998/namespace}spaceZpreserver rrcSs&g|]\}}d|dt|dqS)rvz="r)_escape_attrib_c14nrr"r"r#rs&rT)rrnrrrrrrsortrQrhrrr?rrr)r-r$rCrrrZresolved_namesrZqattrsZ attr_namerjZ parsed_qnamesrurrZ attr_qnamerZspace_behaviourrr")rr-r#rVsb         zC14NWriterTarget._startcCst|jr |jd8_dS|jr||d||dd|jt|jdk|_|j |j dS)Nrrrr) rr8rArrrrEr?rrrrr"r"r#r(s  zC14NWriterTarget.endcCsh|jsdS|jr dS|jr|dn |jr|jr||dt|d|js2|ddSdS)Nrz)rrrrrr8rArr@r"r"r#rGs  zC14NWriterTarget.commentcCst|jrdS|jr|dn |jr|jr|||r'd|dt|dnd|d|js8|ddSdS)Nrz)rrrrr8rAr)r-rwrr"r"r#rHs  $zC14NWriterTarget.pir:)rrr r!r/reversedrrrrr?rArMrDrVr(rGrHr"r"r"r#rs" % % E rc Csxz+d|vr |dd}d|vr|dd}d|vr|dd}d|vr)|dd}|WSttfy;t|YdSw) Nrrrrrrr rrxr"r"r#rs    rc Csz?d|vr |dd}d|vr|dd}d|vr|dd}d|vr)|dd}d |vr3|d d }d |vr=|d d }|WSttfyOt|YdSw) Nrrrrrrrz rz rrrrxr"r"r#rs"      r)ro)_set_factoriesr:rt)rr)Ar!__all__rrrr5rr#Zcollections.abcrrrr-r rrrrr r rrcontextmanagerrrrrrk NameErrorrrrrrrrrrrrrrrrr r rrrrrrrrcompileUNICODErrrrrrWZ _elementtreerrPr"r"r"r#sH  /  0e  1=2  2      6 2 :  zn