a dh(!@sdZgdZdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z Gddde Z d d ZGd d d Zifd dZd^ddZd_ddZeZGdddZGdddZe jddZd`ddZddZdZz eeZWneyYn0ddZd d!Zeeed"Zd#d$Z d%d&d'd(d)d*d+d,Z!e!e _!d-d.Z"d/d0Z#d1d2Z$d3d4Z%daddd5d6d7d8Z&Gd9d:d:ej'Z(dbddd5d6d;d<Z)d=d>Z*dcd@dAZ+dddBdCZ,dedDdEZ-GdFdGdGZ.dfdHdIZ/dgdJdKZ0e/Z1dhdLdMZ2GdNdOdOZ3GdPdQdQZ4didddRdSdTZ5e6dUej7j8Z9GdVdWdWZ:dXdYZ;dZd[ZTdd]l>m?Z?Wne@yzYn 0e?eedS)jaLightweight 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"6/opt/imh-python/lib/python3.9/xml/etree/ElementTree.pyr jsr cCs t|dS)z2Return True if *element* appears to be an Element.tag)hasattr)elementr"r"r#rxsrc@seZdZdZdZdZdZdZifddZddZ ddZ d d Z d d Z d dZ ddZddZddZddZddZddZddZddZdd Zd7d!d"Zd8d#d$Zd9d%d&Zd:d'd(Zd)d*Zd;d+d,Zd-d.Zd/d0Zd1d2Zdtext...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__szElement.__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)r3r$r+texttail)r-elemr"r"r#r7s  zElement.__copy__cCs t|jSr9)lenr,r1r"r"r#__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)r4r5 FutureWarningr=r,r1r"r"r#__bool__s zElement.__bool__cCs |j|Sr9r,r-indexr"r"r# __getitem__szElement.__getitem__cCs8t|tr |D]}||qn ||||j|<dSr9)r'slice_assert_is_elementr,)r-rFr&eltr"r"r# __setitem__s   zElement.__setitem__cCs |j|=dSr9rDrEr"r"r# __delitem__szElement.__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. NrIr,appendr- subelementr"r"r#rNs zElement.appendcCs$|D]}|||j|qdS)zkAppend subelements from a sequence. *elements* is a sequence with zero or more elements. NrM)r-elementsr&r"r"r#extends zElement.extendcCs|||j||dS)z(Insert *subelement* at position *index*.N)rIr,insert)r-rFrPr"r"r#rSs zElement.insertcCs t|tstdt|jdS)Nzexpected an Element, not %s)r' _Element_Pyr)typer)r-er"r"r#rIs zElement._assert_is_elementcCs|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,removerOr"r"r#rW s zElement.removecCst|||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#rXs 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-rZdefaultr[r"r"r#r\&s zElement.findtextcCst|||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. )rfindallrYr"r"r#r_4s zElement.findallcCst|||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. )riterfindrYr"r"r#r`?s zElement.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#raJs 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-keyr^r"r"r#rbUs 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-rcvaluer"r"r#setbsz Element.setcCs |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#rflsz Element.keyscCs |jS)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#rgus z Element.itemsccsD|dkr d}|dus|j|kr$|V|jD]}||EdHq*dS)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$rVr"r"r#ris  z Element.iterccsX|j}t|ts|durdS|j}|r,|V|D]"}|EdH|j}|r0|Vq0dS)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$trVr"r"r#rkszElement.itertext)N)NN)N)N)N)N)"rrr r!r$r+r:r;r/r2r3r8r7r>rCrGrKrLrNrRrSrIrWrXr\r_r`rarbrerfrgrirkr"r"r"r#r}s>           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. )r3rN)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__cCs|jSr9rpr1r"r"r#__str__sz QName.__str__cCsd|jj|jfS)Nz<%s %r>)r*rr:r1r"r"r#r2szQName.__repr__cCs t|jSr9)hashr:r1r"r"r#__hash__szQName.__hash__cCs t|tr|j|jkS|j|kSr9r'rr:r-otherr"r"r#__le__s  z QName.__le__cCs t|tr|j|jkS|j|kSr9rtrur"r"r#__lt__s  z QName.__lt__cCs t|tr|j|jkS|j|kSr9rtrur"r"r#__ge__s  z QName.__ge__cCs t|tr|j|jkS|j|kSr9rtrur"r"r#__gt__s  z QName.__gt__cCs t|tr|j|jkS|j|kSr9rtrur"r"r#__eq__s  z QName.__eq__)N) rrr r!r/rqr2rsrwrxryrzr{r"r"r"r#rs rc@s~eZdZdZdddZddZddZdd d Zdd d Zdd dZ dddZ d ddZ d!ddZ d"ddddZ 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||dSr9)_rootr )r-r&filer"r"r#r/szElementTree.__init__cCs|jS)z!Return root element of this tree.r|r1r"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"szElementTree._setrootcCsd}t|dst|d}d}zt|durVt}t|drV|||_|jW|rT|S|d}|sfqr||qV||_|jW|r|Sn|r|0dS)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%openrrr|closerfeed)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). )r|rir-r$r"r"r#riQs zElementTree.itercCs:|dddkr,d|}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 %rr?r@)r4r5rBr|rXrYr"r"r#rX]s zElementTree.findcCs<|dddkr,d|}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. Nrrrrr?r@)r4r5rBr|r\r]r"r"r#r\ss zElementTree.findtextcCs:|dddkr,d|}tjd|tdd|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. Nrrrrr?r@)r4r5rBr|r_rYr"r"r#r_s zElementTree.findallcCs:|dddkr,d|}tjd|tdd|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. Nrrrrr?r@)r4r5rBr|r`rYr"r"r#r`s zElementTree.iterfindTshort_empty_elementscCs|s d}n|tvrtd||s4|dkr0d}nd}|}t||}|dkr|sd|dur|dvr|} |dkrd dl} | } |d | f|d krt||jn,t|j|\} } t|} | ||j| | |d Wdn1s0YdS) 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-asciiN)rrunicoderrz$ r:r) _serialize ValueErrorlower _get_writerlocalegetpreferredencoding_serialize_textr| _namespaces)r-file_or_filenameencodingxml_declarationdefault_namespacemethodrZ enc_lowerwriteZdeclared_encodingrqnamesr[Z serializer"r"r#rs:    zElementTree.writecCs|j|ddS)Nr)r)r)r-r}r"r"r# write_c14nszElementTree.write_c14n)NN)N)N)N)NN)N)N)NNNN)rrr r!r/rrr rirXr\r_r`rrr"r"r"r#r s$  %     :rc csHz |j}Wnbtyl|dkr,t|d}nt|d|dd}||jVWdn1s^0YYn0|dkr~|Vnt}t|tjr|}ndt|tjrt |}| |j n@t}dd|_ ||_z|j |_ |j|_WntyYn0tj||ddd}| |j |jVWdn1s:0YdS) Nrwxmlcharrefreplace)rerrorscSsdSNTr"r"r"r"r#z_get_writer.. )rrnewline)rAttributeErrorr contextlib ExitStackr'ioBufferedIOBase RawIOBaseBufferedWritercallbackdetachwritableseekabletell TextIOWrapper)rrrr}stackr"r"r#rsB   ,        rcsddiird<fdd}|D]}|j}t|trZ|jvr||jn.add_qname) rir$r'rr:rjrr rrg)r<rrr$rcrdr:r"rr#r*s4         rc Ks|j}|j}|tur$|d|nv|tur<|d|n^||}|dur||r\|t||D]}t|||d|dq`n|d|t|} | s|r2|rt|dddD](\} } | rd| } |d | t | fq| D]L\} } t | t r| j} t | t r|| j} nt | } |d || | fq|sHt |sH|s|d |rb|t||D]}t|||d|dqf|d |d n|d |j r|t|j dS)N r.rc: xmlns%s="%s" %s="%s">)r$r:rr _escape_cdata_serialize_xmllistrgsorted_escape_attribr'rr=r;) rr<rr[rkwargsr$r:rVrgvkr"r"r#rgs\              r) ZareabaseZbasefontbrcolframehrZimginputZisindexlinkmetaZparamc Ks|j}|j}|tur(|dt|n|turD|dt|nh||}|dur|rd|t||D]}t|||dqhn,|d|t|}|s|r8|rt|dddD](\} } | rd| } |d| t | fq|D]N\} } t | t r| j} t | t r|| j} nt | } |d || | fq|d | } |rx| d ksb| d krl||n |t||D]}t|||dq|| tvr|d |d |jr|t|jdS)NrrrcSs|dSrr"rr"r"r#rrz!_serialize_html..rrrrrZscriptstyler)r$r:rrr _serialize_htmlrrgrrr'r_escape_attrib_htmlr HTML_EMPTYr;) rr<rr[rr$r:rVrgrrZltagr"r"r#rsX            rcCs*|D] }||q|jr&||jdSr9)rkr;)rr<partr"r"r#rs  r)rhtmlr:cCsLtd|rtdttD]\}}||ks8||kr t|=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)rematchrrrrg)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|jfdS)Nzcannot serialize %r (type %s))r)rUrrpr"r"r#rsrc CsfzBd|vr|dd}d|vr*|dd}d|vr>|dd}|WSttfy`t|Yn0dS)N&&r<r>replacer)rrrpr"r"r#r s   rc Cszd|vr|dd}d|vr*|dd}d|vr>|dd}d|vrR|dd}d |vrf|d d }d |vrz|d d }d |vr|d d}|WSttfyt|Yn0dS)Nrrrrrr"" z rz  z rrpr"r"r#rs$       rc CsfzBd|vr|dd}d|vr*|dd}d|vr>|dd}|WSttfy`t|Yn0dS)Nrrrrrrrrpr"r"r#r5s   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#rDs rc@s8eZdZdZddZddZddZdd Zd d Zd S) _ListDataStreamz7An auxiliary stream accumulating into a list reference.cCs ||_dSr9)lst)r-rr"r"r#r/^sz_ListDataStream.__init__cCsdSrr"r1r"r"r#rasz_ListDataStream.writablecCsdSrr"r1r"r"r#rdsz_ListDataStream.seekablecCs|j|dSr9)rrN)r-br"r"r#rgsz_ListDataStream.writecCs t|jSr9)r=rr1r"r"r#rjsz_ListDataStream.tellN) rrr r!r/rrrrr"r"r"r#r\s rcCs*g}t|}t|j||||||d|S)Nr)rrr)r&rrrrrrrr"r"r#rms rcCsLt|tst|}|jtjdd|j}|r<|ddkrHtjddS)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#rzs  r cs`t|tr|}|dkr(td|t|s4dSd|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 |}Wn(ty<|}|Yn0|jrN|jsT||_|D],}t|rn|||jr~|jsX||_qX|js||_dSr) IndexErrorrNr: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 )rrr r"r"r#r s  r csft||dfddGfdddtjj}|d_~dtdsbtd 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_parserc3sjzVEdHd}|s q,|q}EdH|_Wrfnrd0dS)Ni@) read_eventsrr_close_and_return_rootrootr)rr)rit pullparserrr"r#iterators   ziterparse..iteratorcseZdZjZdS)z$iterparse..IterParseIteratorN)rrr __next__r")rr"r#IterParseIteratorsrNFrrT)r collectionsabcIteratorrr%r)rr rrr")rrrrrr#r s   r c@s<eZdZd ddddZddZddZd d Zd d ZdS)rN)r cCs<t|_|pttd|_|dur(d}|j|j|dS)Nro)end)rdeque _events_queuerrr  _setevents)r-r r r"r"r#r/s  zXMLPullParser.__init__c Cs\|jdurtd|rXz|j|Wn0tyV}z|j|WYd}~n d}~00dS)Feed encoded data to parser.Nz!feed() called after end of stream)r rr SyntaxErrorrrN)r-rexcr"r"r#rs zXMLPullParser.feedcCs|j}d|_|Sr9)r r)r-rr"r"r#r s 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)r r1r"r"r#rszXMLPullParser.closeccs.|j}|r*|}t|tr"|q|VqdS)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-r eventr"r"r#r 's  zXMLPullParser.read_events)N)rrr r/rr rr r"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. rrrrr)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. rr0)rrrrrirb)r:rr Zidsr<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. rr")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|_|dur*t}||_||_|durBt}||_ ||_ |durZt }||_ dSr9) _data_elem_lastr|_tailr_comment_factoryr%r _pi_factoryr&r_factory)r-Zelement_factoryr#r$r%r&r"r"r#r/s zTreeBuilder.__init__cCs.t|jdksJd|jdus(Jd|jS)z;Flush builder buffers and return toplevel document Element.rzmissing end tagsNzmissing toplevel element)r=r(r|r1r"r"r#rszTreeBuilder.closecCsf|jrb|jdur\d|j}|jr@|jjdus6Jd||j_n|jjdusTJd||j_g|_dS)Nrzinternal error (tail)zinternal error (text))r'r)joinr*r;r:r-r:r"r"r#_flushs   zTreeBuilder._flushcCs|j|dS)zAdd text to current element.N)r'rNr-rr"r"r#rszTreeBuilder.datacCsX|||||_}|jr2|jd|n|jdurB||_|j|d|_|S)zOpen new element and return it. *tag* is the element name, *attrs* is a dict containing element attributes. rNr)r0r-r)r(rNr|r*)r-r$attrsr<r"r"r#starts  zTreeBuilder.startcCs@||j|_|jj|ks4Jd|jj|fd|_|jS)zOClose and return current Element. *tag* is the element name. z&end tag mismatch (expected %s, got %s)r)r0r(popr)r$r*rr"r"r#rs zTreeBuilder.endcCs||j|j|S)z`Create a comment using the comment_factory. *text* is the text of the comment. )_handle_singler+r%r/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 ''. )r5r,r&)r-ror:r"r"r#pis zTreeBuilder.picGs:||}|r6|||_|jr0|jd|d|_|S)Nrr)r0r)r(rNr*)r-factoryrSargsr<r"r"r#r5szTreeBuilder._handle_single)N)N) rrr r!r/rr0rr3rr6r7r5r"r"r"r#rqs   rc@speZdZdZdddddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZdS)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)rorc Cs^zddlm}Wn:tyJz ddl}WntyDtdYn0Yn0||d}|durft}||_|_||_|_ |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 |_ d|_!i|_"zd |j#|_$Wnt%yXYn0dS)Nrexpatz7No module named expat; use SimpleXMLTreeBuilder insteadrr3rstart_nsend_nsrr6r7rzExpat %d.%d.%d)& xml.parsersr; ImportErrorZpyexpatZ ParserCreaterrr ro_targeterror_error_names_defaultZDefaultHandlerExpandr%_startStartElementHandler_endEndElementHandler _start_nsStartNamespaceDeclHandler_end_nsEndNamespaceDeclHandlerrZCharacterDataHandlerr6CommentHandlerr7ProcessingInstructionHandlerZ buffer_textordered_attributesspecified_attributes_doctypeentity version_infoversionr)r-rorr;rr"r"r#r/sP             zXMLParser.__init__cCs8|j}|j}|D] }|dkrDd|_d|_|||jfdd}||_q|dkrf|||jfdd}||_q|dkrt|j dr|||j fd d}n||fd d}||_ q|d krt|j d r|||j fd d}n||fdd}||_ q|dkr|||fdd}||_q|dkr&|||fdd}||_qtd|qdS)Nr3rcSs|||||fdSr9r")r$Z attrib_inr!rNr3r"r"r#handler0sz%XMLParser._setevents..handlerrcSs||||fdSr9r")r$r!rNrr"r"r#rU5szstart-nsr<cSs|||||fdSr9r")rrr!rNr<r"r"r#rU<scSs|||p d|pdffdSNrr")rrr!rNr"r"r#rU@szend-nsr=cSs||||fdSr9r")rr!rNr=r"r"r#rUFscSs||dfdSr9r")rr!rNr"r"r#rUJsr6cSs|||j|fdSr9)ror6)r:r!rNr-r"r"r#rUNsr7cSs|||j||fdSr9)ror7)Z pi_targetrr!rNr-r"r"r#rURszunknown event %r)r rNrOrPrErFrGrHr%rorIrJrKrLrMrNr)r-Z events_queueZevents_to_reportrrNZ event_namerUr"r"r#r#sL          zXMLParser._seteventscCs&t|}|j|_|j|jf|_|dSr9)r codelinenooffsetZposition)r-rderrr"r"r# _raiseerrorYszXMLParser._raiseerrorcCsDz|j|}Wn0ty>|}d|vr0d|}||j|<Yn0|S)Nrr)rCKeyError)r-rcnamer"r"r#_fixname_s zXMLParser._fixnamecCs|j|p d|pdSrV)ror<r-rrr"r"r#rIjszXMLParser._start_nscCs|j|p dSrV)ror=)r-rr"r"r#rKmszXMLParser._end_nscCsR|j}||}i}|rDtdt|dD]}||d||||<q&|j||S)Nrr?r)r^ranger=ror3)r-r$ attr_listZfixnamer+ir"r"r#rEpszXMLParser._startcCs|j||Sr9)rorr^rr"r"r#rG|szXMLParser._endc Cs|dd}|dkrz |jj}Wnty4YdS0z||j|ddWnXtyddlm}|d||jj |jj f}d|_ |jj |_ |jj |_ |Yn0n"|dkr|dd d krg|_n|jdur|d krd|_dS|}|sdS|j|t|j}|d kr|jd}|d kr`|dkr`|j\}}} } | r| dd} n*|dkr|dkr|j\}}} d} ndSt|jdr|j|| | ddnt|drtdtd|_dS)Nrrrrr:z'undefined entity %s: line %d, column %d r z r;rArZErrorLineNumberZErrorColumnNumberrWrXrYrQrrNr=r%rgr4r5RuntimeWarning) r-r:rZ data_handlerr;rZnrUr]Zpubidsystemr"r"r#rDsd               zXMLParser._defaultc CsHz|j|dWn0|jyB}z||WYd}~n d}~00dS)rFN)rParserBr[)r-rrr"r"r#rszXMLParser.feedc Csz|jddWn0|jyB}z||WYd}~n d}~00zLz |jj}WntydYn0|W|`|`|`|`SW|`|`|`|`n|`|`|`|`0dS)z;Finish feeding data to parser and return element structure.rTN) rrkrBr[rorrr r@)r-rZ close_handlerr"r"r#rs     zXMLParser.close)rrr r!r/rr[r^rIrKrErGrDrrr"r"r"r#rs .6  6r)out from_filecKs|dur|durtdd}|dur0t}}tt|jfi|d}|durd|||n|durxt||d|dur| SdS)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) rrrrrrrrr r)Zxml_datarlrmoptionssiorr"r"r#rs     rz ^\w+:\w+$c@seZdZdZddddddddddZefddZd d Zdd d Zd dZ dj fddZ ddZ ddZ dddZddZddZddZdS) 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_tagsc Cs||_g|_||_||_|r$t|nd|_|r6t|nd|_||_|rRt||_nd|_|rjt|j |_ nd|_ dgg|_ g|_ |s|j tt|j gi|_dg|_d|_d|_d|_d|_dS)N)rrFr)_writer'_with_comments _strip_textre_exclude_attrs _exclude_tags_rewrite_prefixes_qname_aware_tags intersection_find_qname_aware_attrs_declared_ns_stack _ns_stackrNrrrg _prefix_map_preserve_space_pending_start _root_seen _root_done_ignored_depth) r-rrprqrrrsrtrurvr"r"r#r/s2  zC14NWriterTarget.__init__ccs ||D]}|r|EdHqdSr9r")r-Zns_stackZ _reversedr[r"r"r#_iter_namespaces-s z!C14NWriterTarget._iter_namespacescCs\|dd\}}||jD]$\}}||krd|d|Sqtd|d|ddS)NrrrrzPrefix z of QName "" is not declared in scope)splitrrr)r-Z prefixed_namerr]rpr"r"r#_resolve_prefix_name2s z%C14NWriterTarget._resolve_prefix_namecCs|dur:|dddkr,|ddddnd|f\}}n|}t}||jD]B\}}||kr||vr|rz|d|n|||fS||qP|jr||jvr|j|}ndt|j}|j|<|jd||f|d|||fS|s d|vr |||fS||j D]J\}}||kr|jd||f|rR|d|n|||fSq|st|||fSt d|d dS) Nrrrrrrirz Namespace "r) rrerraddr|rr=rNrr)r-rrr$Z prefixes_seenurr"r"r#_qname9s.2      & zC14NWriterTarget._qnamecCs|js|j|dSr9)rr'rNr1r"r"r#r^szC14NWriterTarget.datarcCs||j}|jdd=|jr.|jds.|}|jdurz|jd}|_|rVt|rV|nd}|jg||R|durzdS|r|jr|t |dSNr) r'ryrrr_looks_like_prefix_namerErrw_escape_cdata_c14n)r-Z _join_textrr9 qname_textr"r"r#r0bs    zC14NWriterTarget._flushcCs0|jr dS|jr||jd||fdSr)rr'r0rrNr_r"r"r#r<ps zC14NWriterTarget.start_nscCs|jdur,|js||jvr,|jd7_dS|jr:|g}|j||jdurn||jvrn|||f|_dS||||dSr) r{rr'r0rrNr}rrE)r-r$r2new_namespacesr"r"r#r3xs   zC14NWriterTarget.startcs jdur$|r$fdd|D}|h|}i}|durV|}||<||jdur|r|}|r|D]0} || } t| rv| }|| <||qvqd}nd}jfddt|dddD} |rdd|D} | ng} |rjt|D]^\} }|dur@| |vr@||vr@| ||d }| | \}} }| |r\|n| |fq | d }j |r|d knj d j }|d | |d | r|d dd| D|d|dur|t| ||d d_j gdS)Ncs i|]\}}|jvr||qSr")rz.0rrr1r"r# rz+C14NWriterTarget._start..csi|]}||qSr"r")rri) parse_qnamer"r#rrcSs |ddS)Nrr)r)rir"r"r#rrz)C14NWriterTarget._start..rcSs$g|]\}}|rd|nd|fqS)zxmlns:Zxmlnsr")rrrr"r"r# sz+C14NWriterTarget._start..rz+{http://www.w3.org/XML/1998/namespace}spaceZpreserverrrcSs&g|]\}}d|dt|dqS)rnz="r)_escape_attrib_c14nrr"r"r#rrrT)rzrgrrrrrrsortrNrbrrwr.rrr)r-r$r2rrrZresolved_namesrZqattrsZ attr_namerdZ parsed_qnamesrarrZ attr_qnamerZspace_behaviourrr")rr-r#rEs^        zC14NWriterTarget._startcCst|jr|jd8_dS|jr&||d||dd|jt|jdk|_|j |j dS)Nrrrr) rr'r0rwrrr4r=rrrrr"r"r#rs  zC14NWriterTarget.endcCsd|js dS|jrdS|jr&|dn|jr:|jr:||dt|d|js`|ddS)Nrz)rxrrrwrr'r0rr/r"r"r#r6s  zC14NWriterTarget.commentcCsp|jr dS|jr|dn|jr0|jr0|||rNd|dt|dn d|d|jsl|ddS)Nrz)rrrwrr'r0r)r-rorr"r"r#r7s  $zC14NWriterTarget.pi)N)N)rrr r!r/reversedrrrrr.r0r<r3rErr6r7r"r"r"r#rs  %  % E  rc CszzVd|vr|dd}d|vr*|dd}d|vr>|dd}d|vrR|dd}|WSttfytt|Yn0dS) Nrrrrrrr rrpr"r"r#rs    rc Csz~d|vr|dd}d|vr*|dd}d|vr>|dd}d|vrR|dd}d |vrf|d d }d |vrz|d d }|WSttfyt|Yn0dS) Nrrrrrrrz rz rrrrpr"r"r#rs       r)rh)_set_factories)N)N)N)NN)NN)rr)N)NN)N)N)N)N)Ar!__all__rrrr4rrcollections.abcrrrrr rrrrr r rrcontextmanagerrrrrre NameErrorrrrrrrrrrrrrrrrr r rrrrrrrrcompileUNICODErrrrrrT _elementtreerr?r"r"r"r#sH /   0j 3 =2 2     6  05   zg