Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3..../xml/dom
File: pulldom.py
import xml.sax
[0] Fix | Delete
import xml.sax.handler
[1] Fix | Delete
[2] Fix | Delete
START_ELEMENT = "START_ELEMENT"
[3] Fix | Delete
END_ELEMENT = "END_ELEMENT"
[4] Fix | Delete
COMMENT = "COMMENT"
[5] Fix | Delete
START_DOCUMENT = "START_DOCUMENT"
[6] Fix | Delete
END_DOCUMENT = "END_DOCUMENT"
[7] Fix | Delete
PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION"
[8] Fix | Delete
IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE"
[9] Fix | Delete
CHARACTERS = "CHARACTERS"
[10] Fix | Delete
[11] Fix | Delete
class PullDOM(xml.sax.ContentHandler):
[12] Fix | Delete
_locator = None
[13] Fix | Delete
document = None
[14] Fix | Delete
[15] Fix | Delete
def __init__(self, documentFactory=None):
[16] Fix | Delete
from xml.dom import XML_NAMESPACE
[17] Fix | Delete
self.documentFactory = documentFactory
[18] Fix | Delete
self.firstEvent = [None, None]
[19] Fix | Delete
self.lastEvent = self.firstEvent
[20] Fix | Delete
self.elementStack = []
[21] Fix | Delete
self.push = self.elementStack.append
[22] Fix | Delete
try:
[23] Fix | Delete
self.pop = self.elementStack.pop
[24] Fix | Delete
except AttributeError:
[25] Fix | Delete
# use class' pop instead
[26] Fix | Delete
pass
[27] Fix | Delete
self._ns_contexts = [{XML_NAMESPACE:'xml'}] # contains uri -> prefix dicts
[28] Fix | Delete
self._current_context = self._ns_contexts[-1]
[29] Fix | Delete
self.pending_events = []
[30] Fix | Delete
[31] Fix | Delete
def pop(self):
[32] Fix | Delete
result = self.elementStack[-1]
[33] Fix | Delete
del self.elementStack[-1]
[34] Fix | Delete
return result
[35] Fix | Delete
[36] Fix | Delete
def setDocumentLocator(self, locator):
[37] Fix | Delete
self._locator = locator
[38] Fix | Delete
[39] Fix | Delete
def startPrefixMapping(self, prefix, uri):
[40] Fix | Delete
if not hasattr(self, '_xmlns_attrs'):
[41] Fix | Delete
self._xmlns_attrs = []
[42] Fix | Delete
self._xmlns_attrs.append((prefix or 'xmlns', uri))
[43] Fix | Delete
self._ns_contexts.append(self._current_context.copy())
[44] Fix | Delete
self._current_context[uri] = prefix or None
[45] Fix | Delete
[46] Fix | Delete
def endPrefixMapping(self, prefix):
[47] Fix | Delete
self._current_context = self._ns_contexts.pop()
[48] Fix | Delete
[49] Fix | Delete
def startElementNS(self, name, tagName , attrs):
[50] Fix | Delete
# Retrieve xml namespace declaration attributes.
[51] Fix | Delete
xmlns_uri = 'http://www.w3.org/2000/xmlns/'
[52] Fix | Delete
xmlns_attrs = getattr(self, '_xmlns_attrs', None)
[53] Fix | Delete
if xmlns_attrs is not None:
[54] Fix | Delete
for aname, value in xmlns_attrs:
[55] Fix | Delete
attrs._attrs[(xmlns_uri, aname)] = value
[56] Fix | Delete
self._xmlns_attrs = []
[57] Fix | Delete
uri, localname = name
[58] Fix | Delete
if uri:
[59] Fix | Delete
# When using namespaces, the reader may or may not
[60] Fix | Delete
# provide us with the original name. If not, create
[61] Fix | Delete
# *a* valid tagName from the current context.
[62] Fix | Delete
if tagName is None:
[63] Fix | Delete
prefix = self._current_context[uri]
[64] Fix | Delete
if prefix:
[65] Fix | Delete
tagName = prefix + ":" + localname
[66] Fix | Delete
else:
[67] Fix | Delete
tagName = localname
[68] Fix | Delete
if self.document:
[69] Fix | Delete
node = self.document.createElementNS(uri, tagName)
[70] Fix | Delete
else:
[71] Fix | Delete
node = self.buildDocument(uri, tagName)
[72] Fix | Delete
else:
[73] Fix | Delete
# When the tagname is not prefixed, it just appears as
[74] Fix | Delete
# localname
[75] Fix | Delete
if self.document:
[76] Fix | Delete
node = self.document.createElement(localname)
[77] Fix | Delete
else:
[78] Fix | Delete
node = self.buildDocument(None, localname)
[79] Fix | Delete
[80] Fix | Delete
for aname,value in attrs.items():
[81] Fix | Delete
a_uri, a_localname = aname
[82] Fix | Delete
if a_uri == xmlns_uri:
[83] Fix | Delete
if a_localname == 'xmlns':
[84] Fix | Delete
qname = a_localname
[85] Fix | Delete
else:
[86] Fix | Delete
qname = 'xmlns:' + a_localname
[87] Fix | Delete
attr = self.document.createAttributeNS(a_uri, qname)
[88] Fix | Delete
node.setAttributeNodeNS(attr)
[89] Fix | Delete
elif a_uri:
[90] Fix | Delete
prefix = self._current_context[a_uri]
[91] Fix | Delete
if prefix:
[92] Fix | Delete
qname = prefix + ":" + a_localname
[93] Fix | Delete
else:
[94] Fix | Delete
qname = a_localname
[95] Fix | Delete
attr = self.document.createAttributeNS(a_uri, qname)
[96] Fix | Delete
node.setAttributeNodeNS(attr)
[97] Fix | Delete
else:
[98] Fix | Delete
attr = self.document.createAttribute(a_localname)
[99] Fix | Delete
node.setAttributeNode(attr)
[100] Fix | Delete
attr.value = value
[101] Fix | Delete
[102] Fix | Delete
self.lastEvent[1] = [(START_ELEMENT, node), None]
[103] Fix | Delete
self.lastEvent = self.lastEvent[1]
[104] Fix | Delete
self.push(node)
[105] Fix | Delete
[106] Fix | Delete
def endElementNS(self, name, tagName):
[107] Fix | Delete
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
[108] Fix | Delete
self.lastEvent = self.lastEvent[1]
[109] Fix | Delete
[110] Fix | Delete
def startElement(self, name, attrs):
[111] Fix | Delete
if self.document:
[112] Fix | Delete
node = self.document.createElement(name)
[113] Fix | Delete
else:
[114] Fix | Delete
node = self.buildDocument(None, name)
[115] Fix | Delete
[116] Fix | Delete
for aname,value in attrs.items():
[117] Fix | Delete
attr = self.document.createAttribute(aname)
[118] Fix | Delete
attr.value = value
[119] Fix | Delete
node.setAttributeNode(attr)
[120] Fix | Delete
[121] Fix | Delete
self.lastEvent[1] = [(START_ELEMENT, node), None]
[122] Fix | Delete
self.lastEvent = self.lastEvent[1]
[123] Fix | Delete
self.push(node)
[124] Fix | Delete
[125] Fix | Delete
def endElement(self, name):
[126] Fix | Delete
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
[127] Fix | Delete
self.lastEvent = self.lastEvent[1]
[128] Fix | Delete
[129] Fix | Delete
def comment(self, s):
[130] Fix | Delete
if self.document:
[131] Fix | Delete
node = self.document.createComment(s)
[132] Fix | Delete
self.lastEvent[1] = [(COMMENT, node), None]
[133] Fix | Delete
self.lastEvent = self.lastEvent[1]
[134] Fix | Delete
else:
[135] Fix | Delete
event = [(COMMENT, s), None]
[136] Fix | Delete
self.pending_events.append(event)
[137] Fix | Delete
[138] Fix | Delete
def processingInstruction(self, target, data):
[139] Fix | Delete
if self.document:
[140] Fix | Delete
node = self.document.createProcessingInstruction(target, data)
[141] Fix | Delete
self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
[142] Fix | Delete
self.lastEvent = self.lastEvent[1]
[143] Fix | Delete
else:
[144] Fix | Delete
event = [(PROCESSING_INSTRUCTION, target, data), None]
[145] Fix | Delete
self.pending_events.append(event)
[146] Fix | Delete
[147] Fix | Delete
def ignorableWhitespace(self, chars):
[148] Fix | Delete
node = self.document.createTextNode(chars)
[149] Fix | Delete
self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
[150] Fix | Delete
self.lastEvent = self.lastEvent[1]
[151] Fix | Delete
[152] Fix | Delete
def characters(self, chars):
[153] Fix | Delete
node = self.document.createTextNode(chars)
[154] Fix | Delete
self.lastEvent[1] = [(CHARACTERS, node), None]
[155] Fix | Delete
self.lastEvent = self.lastEvent[1]
[156] Fix | Delete
[157] Fix | Delete
def startDocument(self):
[158] Fix | Delete
if self.documentFactory is None:
[159] Fix | Delete
import xml.dom.minidom
[160] Fix | Delete
self.documentFactory = xml.dom.minidom.Document.implementation
[161] Fix | Delete
[162] Fix | Delete
def buildDocument(self, uri, tagname):
[163] Fix | Delete
# Can't do that in startDocument, since we need the tagname
[164] Fix | Delete
# XXX: obtain DocumentType
[165] Fix | Delete
node = self.documentFactory.createDocument(uri, tagname, None)
[166] Fix | Delete
self.document = node
[167] Fix | Delete
self.lastEvent[1] = [(START_DOCUMENT, node), None]
[168] Fix | Delete
self.lastEvent = self.lastEvent[1]
[169] Fix | Delete
self.push(node)
[170] Fix | Delete
# Put everything we have seen so far into the document
[171] Fix | Delete
for e in self.pending_events:
[172] Fix | Delete
if e[0][0] == PROCESSING_INSTRUCTION:
[173] Fix | Delete
_,target,data = e[0]
[174] Fix | Delete
n = self.document.createProcessingInstruction(target, data)
[175] Fix | Delete
e[0] = (PROCESSING_INSTRUCTION, n)
[176] Fix | Delete
elif e[0][0] == COMMENT:
[177] Fix | Delete
n = self.document.createComment(e[0][1])
[178] Fix | Delete
e[0] = (COMMENT, n)
[179] Fix | Delete
else:
[180] Fix | Delete
raise AssertionError("Unknown pending event ",e[0][0])
[181] Fix | Delete
self.lastEvent[1] = e
[182] Fix | Delete
self.lastEvent = e
[183] Fix | Delete
self.pending_events = None
[184] Fix | Delete
return node.firstChild
[185] Fix | Delete
[186] Fix | Delete
def endDocument(self):
[187] Fix | Delete
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
[188] Fix | Delete
self.pop()
[189] Fix | Delete
[190] Fix | Delete
def clear(self):
[191] Fix | Delete
"clear(): Explicitly release parsing structures"
[192] Fix | Delete
self.document = None
[193] Fix | Delete
[194] Fix | Delete
class ErrorHandler:
[195] Fix | Delete
def warning(self, exception):
[196] Fix | Delete
print(exception)
[197] Fix | Delete
def error(self, exception):
[198] Fix | Delete
raise exception
[199] Fix | Delete
def fatalError(self, exception):
[200] Fix | Delete
raise exception
[201] Fix | Delete
[202] Fix | Delete
class DOMEventStream:
[203] Fix | Delete
def __init__(self, stream, parser, bufsize):
[204] Fix | Delete
self.stream = stream
[205] Fix | Delete
self.parser = parser
[206] Fix | Delete
self.bufsize = bufsize
[207] Fix | Delete
if not hasattr(self.parser, 'feed'):
[208] Fix | Delete
self.getEvent = self._slurp
[209] Fix | Delete
self.reset()
[210] Fix | Delete
[211] Fix | Delete
def reset(self):
[212] Fix | Delete
self.pulldom = PullDOM()
[213] Fix | Delete
# This content handler relies on namespace support
[214] Fix | Delete
self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
[215] Fix | Delete
self.parser.setContentHandler(self.pulldom)
[216] Fix | Delete
[217] Fix | Delete
def __getitem__(self, pos):
[218] Fix | Delete
rc = self.getEvent()
[219] Fix | Delete
if rc:
[220] Fix | Delete
return rc
[221] Fix | Delete
raise IndexError
[222] Fix | Delete
[223] Fix | Delete
def __next__(self):
[224] Fix | Delete
rc = self.getEvent()
[225] Fix | Delete
if rc:
[226] Fix | Delete
return rc
[227] Fix | Delete
raise StopIteration
[228] Fix | Delete
[229] Fix | Delete
def __iter__(self):
[230] Fix | Delete
return self
[231] Fix | Delete
[232] Fix | Delete
def expandNode(self, node):
[233] Fix | Delete
event = self.getEvent()
[234] Fix | Delete
parents = [node]
[235] Fix | Delete
while event:
[236] Fix | Delete
token, cur_node = event
[237] Fix | Delete
if cur_node is node:
[238] Fix | Delete
return
[239] Fix | Delete
if token != END_ELEMENT:
[240] Fix | Delete
parents[-1].appendChild(cur_node)
[241] Fix | Delete
if token == START_ELEMENT:
[242] Fix | Delete
parents.append(cur_node)
[243] Fix | Delete
elif token == END_ELEMENT:
[244] Fix | Delete
del parents[-1]
[245] Fix | Delete
event = self.getEvent()
[246] Fix | Delete
[247] Fix | Delete
def getEvent(self):
[248] Fix | Delete
# use IncrementalParser interface, so we get the desired
[249] Fix | Delete
# pull effect
[250] Fix | Delete
if not self.pulldom.firstEvent[1]:
[251] Fix | Delete
self.pulldom.lastEvent = self.pulldom.firstEvent
[252] Fix | Delete
while not self.pulldom.firstEvent[1]:
[253] Fix | Delete
buf = self.stream.read(self.bufsize)
[254] Fix | Delete
if not buf:
[255] Fix | Delete
self.parser.close()
[256] Fix | Delete
return None
[257] Fix | Delete
self.parser.feed(buf)
[258] Fix | Delete
rc = self.pulldom.firstEvent[1][0]
[259] Fix | Delete
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
[260] Fix | Delete
return rc
[261] Fix | Delete
[262] Fix | Delete
def _slurp(self):
[263] Fix | Delete
""" Fallback replacement for getEvent() using the
[264] Fix | Delete
standard SAX2 interface, which means we slurp the
[265] Fix | Delete
SAX events into memory (no performance gain, but
[266] Fix | Delete
we are compatible to all SAX parsers).
[267] Fix | Delete
"""
[268] Fix | Delete
self.parser.parse(self.stream)
[269] Fix | Delete
self.getEvent = self._emit
[270] Fix | Delete
return self._emit()
[271] Fix | Delete
[272] Fix | Delete
def _emit(self):
[273] Fix | Delete
""" Fallback replacement for getEvent() that emits
[274] Fix | Delete
the events that _slurp() read previously.
[275] Fix | Delete
"""
[276] Fix | Delete
rc = self.pulldom.firstEvent[1][0]
[277] Fix | Delete
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
[278] Fix | Delete
return rc
[279] Fix | Delete
[280] Fix | Delete
def clear(self):
[281] Fix | Delete
"""clear(): Explicitly release parsing objects"""
[282] Fix | Delete
self.pulldom.clear()
[283] Fix | Delete
del self.pulldom
[284] Fix | Delete
self.parser = None
[285] Fix | Delete
self.stream = None
[286] Fix | Delete
[287] Fix | Delete
class SAX2DOM(PullDOM):
[288] Fix | Delete
[289] Fix | Delete
def startElementNS(self, name, tagName , attrs):
[290] Fix | Delete
PullDOM.startElementNS(self, name, tagName, attrs)
[291] Fix | Delete
curNode = self.elementStack[-1]
[292] Fix | Delete
parentNode = self.elementStack[-2]
[293] Fix | Delete
parentNode.appendChild(curNode)
[294] Fix | Delete
[295] Fix | Delete
def startElement(self, name, attrs):
[296] Fix | Delete
PullDOM.startElement(self, name, attrs)
[297] Fix | Delete
curNode = self.elementStack[-1]
[298] Fix | Delete
parentNode = self.elementStack[-2]
[299] Fix | Delete
parentNode.appendChild(curNode)
[300] Fix | Delete
[301] Fix | Delete
def processingInstruction(self, target, data):
[302] Fix | Delete
PullDOM.processingInstruction(self, target, data)
[303] Fix | Delete
node = self.lastEvent[0][1]
[304] Fix | Delete
parentNode = self.elementStack[-1]
[305] Fix | Delete
parentNode.appendChild(node)
[306] Fix | Delete
[307] Fix | Delete
def ignorableWhitespace(self, chars):
[308] Fix | Delete
PullDOM.ignorableWhitespace(self, chars)
[309] Fix | Delete
node = self.lastEvent[0][1]
[310] Fix | Delete
parentNode = self.elementStack[-1]
[311] Fix | Delete
parentNode.appendChild(node)
[312] Fix | Delete
[313] Fix | Delete
def characters(self, chars):
[314] Fix | Delete
PullDOM.characters(self, chars)
[315] Fix | Delete
node = self.lastEvent[0][1]
[316] Fix | Delete
parentNode = self.elementStack[-1]
[317] Fix | Delete
parentNode.appendChild(node)
[318] Fix | Delete
[319] Fix | Delete
[320] Fix | Delete
default_bufsize = (2 ** 14) - 20
[321] Fix | Delete
[322] Fix | Delete
def parse(stream_or_string, parser=None, bufsize=None):
[323] Fix | Delete
if bufsize is None:
[324] Fix | Delete
bufsize = default_bufsize
[325] Fix | Delete
if isinstance(stream_or_string, str):
[326] Fix | Delete
stream = open(stream_or_string, 'rb')
[327] Fix | Delete
else:
[328] Fix | Delete
stream = stream_or_string
[329] Fix | Delete
if not parser:
[330] Fix | Delete
parser = xml.sax.make_parser()
[331] Fix | Delete
return DOMEventStream(stream, parser, bufsize)
[332] Fix | Delete
[333] Fix | Delete
def parseString(string, parser=None):
[334] Fix | Delete
from io import StringIO
[335] Fix | Delete
[336] Fix | Delete
bufsize = len(string)
[337] Fix | Delete
buf = StringIO(string)
[338] Fix | Delete
if not parser:
[339] Fix | Delete
parser = xml.sax.make_parser()
[340] Fix | Delete
return DOMEventStream(buf, parser, bufsize)
[341] Fix | Delete
[342] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function