3 ^/c@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddZ ifdd Z ifd d Z ifd d Z ddZGdddejZGddde jZdddZdS)znA library of useful helper classes to the SAX classes, for the convenience of application and driver writers. N)handler) xmlreadercCs&x |jD]\}}|j||}q W|S)z2Replace substrings of a string using a dictionary.)itemsreplace)ZsZdZkeyvaluer(/usr/lib64/python3.6/xml/sax/saxutils.py__dict_replace sr cCs6|jdd}|jdd}|jdd}|r2t||}|S)zEscape &, <, and > in a string of data. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. &&>><<rr dataZentitiesrrrescapes    rcCs2|jdd}|jdd}|r&t||}|jddS)a Unescape &, <, and > in a string of data. You can unescape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. rrr r r r rrrrrunescape"s    rcCs\|j}|jddddt||}d|krPd|krFd|jdd}qXd |}nd|}|S) aEscape and quote an attribute value. Escape &, <, and > in a string of data, then quote it for use as an attribute value. The " character will be escaped as well, if necessary. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. z z z ) z z z"z'z"%s"z"z'%s')copyZupdaterrrrrr quoteattr0s   rcsdkrddl}|jSttjr&Sttjtjfr<SttjrlGfddd}|}dd|_ nDtj }dd|_ j |_ yj |_ j|_Wntk rYnXtj||ddd d S) NrcseZdZjZfddZdS)z _gettextwriter.._wrappercs t|SN)Zgetattrselfnameoutrr __getattr__[z,_gettextwriter.._wrapper.__getattr__N)__name__ __module__ __qualname__Z __class__rrrrr_wrapperYsr#cSsdSrrrrrr^z _gettextwriter..cSsdS)NTrrrrrr$cr%ZxmlcharrefreplacerT)encodingZerrorsZnewlineZ write_through)sysZstdout isinstanceioZ TextIOBasecodecsZ StreamWriterZStreamReaderWriterZ RawIOBaseZcloseZBufferedIOBaseZwritablewriteZseekableZtellZAttributeErrorZ TextIOWrapper)rr&r'r#Zbufferrrr_gettextwriterHs.     r,c@seZdZd ddZddZd!dd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZdS)" XMLGeneratorN iso-8859-1FcCsVtjj|t||}|j|_|j|_ig|_|jd|_ g|_ ||_ ||_ d|_ dS)NrF)rContentHandler__init__r,r+_writeZflush_flush _ns_contexts_current_context_undeclared_ns_maps _encoding_short_empty_elements_pending_start_element)rrr&Zshort_empty_elementsrrrr1ss   zXMLGenerator.__init__cCsJ|drBd|dkr d|dS|j|d}|rB|d|dS|dS)z7Builds a qualified name from a (ns_url, localname) pairrz$http://www.w3.org/XML/1998/namespacezxml:rz:)r5)rrprefixrrr_qnames  zXMLGenerator._qnamecCs|jr|jdd|_dS)Nr Fr9r2)r endElementrrr_finish_pending_start_elements z*XMLGenerator._finish_pending_start_elementcCs|jd|jdS)Nz$ )r2r7rrrr startDocumentszXMLGenerator.startDocumentcCs |jdSr)r3r?rrr endDocumentrzXMLGenerator.endDocumentcCs0|jj|jj||j|<|jj||fdSr)r4Zappendr5rr6rr:urirrrstartPrefixMappings zXMLGenerator.startPrefixMappingcCs|jd|_|jd=dS)Nrr/r/)r4r5rr:rrrendPrefixMapping zXMLGenerator.endPrefixMappingcCs^|j|jd|x*|jD]\}}|jd|t|fq W|jrPd|_n |jddS)Nr %s=%sTr )r>r2rrr8r9)rrattrsrrrr startElementszXMLGenerator.startElementcCs*|jr|jdd|_n|jd|dSNz/>Fzr<rrrrr= zXMLGenerator.endElementcCs|j|jd|j|x8|jD].\}}|rD|jd||fq$|jd|q$Wg|_x0|jD]$\}}|jd|j|t|fqfW|jrd|_n |jddS)Nrz xmlns:%s="%s"z xmlns="%s"rHTr )r>r2r;r6rrr8r9)rrqnamerIr:rCrrrrstartElementNSs zXMLGenerator.startElementNScCs0|jr|jdd|_n|jd|j|dSrK)r9r2r;rrrMrrr endElementNSrLzXMLGenerator.endElementNScCs4|r0|jt|ts"t||j}|jt|dSr)r>r(strr7r2rrZcontentrrr characters   zXMLGenerator.characterscCs0|r,|jt|ts"t||j}|j|dSr)r>r(rQr7r2rRrrrignorableWhitespacerTz XMLGenerator.ignorableWhitespacecCs|j|jd||fdS)Nz )r>r2rZtargetrrrrprocessingInstructionsz"XMLGenerator.processingInstruction)Nr.F)F)r r!r"r1r;r>r@rArDrFrJr=rNrPrSrUrWrrrrr-qs   r-c@seZdZdZd;ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:ZdS)< XMLFilterBaseaYThis class is designed to sit between an XMLReader and the client application's event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.NcCstjj|||_dSr)r XMLReaderr1_parentrZparentrrrr1rGzXMLFilterBase.__init__cC|jj|dSr) _err_handlererrorrZ exceptionrrrr^rzXMLFilterBase.errorcCr\r)r] fatalErrorr_rrrr`rzXMLFilterBase.fatalErrorcCr\r)r]warningr_rrrrarzXMLFilterBase.warningcCr\r) _cont_handlersetDocumentLocator)rZlocatorrrrrcrz XMLFilterBase.setDocumentLocatorcC|jjdSr)rbr@r?rrrr@rzXMLFilterBase.startDocumentcCrdr)rbrAr?rrrrArzXMLFilterBase.endDocumentcC|jj||dSr)rbrDrBrrrrDrz XMLFilterBase.startPrefixMappingcCr\r)rbrFrErrrrF rzXMLFilterBase.endPrefixMappingcCrer)rbrJ)rrrIrrrrJ rzXMLFilterBase.startElementcCr\r)rbr=rrrrr=rzXMLFilterBase.endElementcC|jj|||dSr)rbrN)rrrMrIrrrrNrzXMLFilterBase.startElementNScCrer)rbrPrOrrrrPrzXMLFilterBase.endElementNScCr\r)rbrSrRrrrrSrzXMLFilterBase.characterscCr\r)rbrU)rZcharsrrrrUrz!XMLFilterBase.ignorableWhitespacecCrer)rbrWrVrrrrWrz#XMLFilterBase.processingInstructioncCr\r)rb skippedEntityrrrrrg!rzXMLFilterBase.skippedEntitycCrfr) _dtd_handler notationDecl)rrpublicIdsystemIdrrrri&rzXMLFilterBase.notationDeclcCs|jj||||dSr)rhunparsedEntityDecl)rrrjrkZndatarrrrl)rz XMLFilterBase.unparsedEntityDeclcCs|jj||Sr)Z _ent_handler resolveEntity)rrjrkrrrrm.rzXMLFilterBase.resolveEntitycCs@|jj||jj||jj||jj||jj|dSr)rZZsetContentHandlerZsetErrorHandlerZsetEntityResolverZ setDTDHandlerparse)rsourcerrrrn3s     zXMLFilterBase.parsecCr\r)rZ setLocale)rZlocalerrrrp:rzXMLFilterBase.setLocalecC |jj|Sr)rZ getFeaturerrrrrr=rzXMLFilterBase.getFeaturecCrer)rZ setFeature)rrZstaterrrrs@rzXMLFilterBase.setFeaturecCrqr)rZ getPropertyrrrrrtCrzXMLFilterBase.getPropertycCrer)rZ setProperty)rrrrrrruFrzXMLFilterBase.setPropertycCs|jSrrZr?rrr getParentKrzXMLFilterBase.getParentcCs ||_dSrrvr[rrr setParentNrzXMLFilterBase.setParentr) r r!r"__doc__r1r^r`rarcr@rArDrFrJr=rNrPrSrUrWrgrirlrmrnrprrrsrtrurwrxrrrrrXs: rXcCs t|trtj|}n^t|drt|}tj}t|jdtrH|j|n |j|t|drtt|jtrt|j |j|j dko|j dkr|j }t jjt jj|}t jj||}t jj|r|j |t|d}n$|j tjj||tjj|j }|j||S)zThis function takes an InputSource and an optional base URL and returns a fully resolved InputSource object ready for reading.readrrNZrb)r(rQrZ InputSourceZhasattrr{ZsetCharacterStreamZ setByteStreamrZ setSystemIdZgetCharacterStreamZ getByteStreamZ getSystemIdosZpathZdirnameZnormpathZjoinZisfileZopenurllibrnZurljoinZrequestZurlopen)roZbaseZfZsysidZbaseheadZ sysidfilenamerrrprepare_input_sourceSs*          r~)rz)ryr|Z urllib.parser}Zurllib.requestr)r*rzrrr rrrr,r0r-rYrXr~rrrrZs     )so