Abuse "".join(list) some more, to replace string concatenation. On Python 2.6.5, 80% of the time spent serialising the output SVG file is saved with this change.

This commit is contained in:
Louis Simard 2010-08-11 23:05:00 -04:00
parent f00b1a24f8
commit beccc7c577

View file

@ -428,9 +428,7 @@ def findReferencedElements(node, ids=None):
# one stretch of text, please! (we could use node.normalize(), but # one stretch of text, please! (we could use node.normalize(), but
# this actually modifies the node, and we don't want to keep # this actually modifies the node, and we don't want to keep
# whitespace around if there's any) # whitespace around if there's any)
stylesheet = '' stylesheet = "".join([child.nodeValue for child in node.childNodes])
for child in node.childNodes:
stylesheet += child.nodeValue
if stylesheet != '': if stylesheet != '':
cssRules = parseCssString(stylesheet) cssRules = parseCssString(stylesheet)
for rule in cssRules: for rule in cssRules:
@ -648,9 +646,7 @@ def renameID(doc, idFrom, idTo, identifiedElements, referencedIDs):
# there's a CDATASection node surrounded by whitespace # there's a CDATASection node surrounded by whitespace
# nodes # nodes
# (node.normalize() will NOT work here, it only acts on Text nodes) # (node.normalize() will NOT work here, it only acts on Text nodes)
oldValue = '' oldValue = "".join([child.nodeValue for child in node.childNodes])
for child in node.childNodes:
oldValue += child.nodeValue
# not going to reparse the whole thing # not going to reparse the whole thing
newValue = oldValue.replace('url(#' + idFrom + ')', 'url(#' + idTo + ')') newValue = oldValue.replace('url(#' + idFrom + ')', 'url(#' + idTo + ')')
newValue = newValue.replace("url(#'" + idFrom + "')", 'url(#' + idTo + ')') newValue = newValue.replace("url(#'" + idFrom + "')", 'url(#' + idTo + ')')
@ -1375,12 +1371,10 @@ def repairStyle(node, options):
del styleMap[propName] del styleMap[propName]
# sew our remaining style properties back together into a style attribute # sew our remaining style properties back together into a style attribute
fixedStyle = '' fixedStyle = [prop + ':' + styleMap[prop] + ';' for prop in styleMap.keys()]
for prop in styleMap.keys() :
fixedStyle += prop + ':' + styleMap[prop] + ';'
if fixedStyle != '' : if len(fixedStyle) > 0:
node.setAttribute('style', fixedStyle) node.setAttribute('style', "".join(fixedStyle))
else: else:
node.removeAttribute('style') node.removeAttribute('style')
@ -2480,12 +2474,14 @@ def makeWellFormed(str):
# - somewhat judicious use of whitespace # - somewhat judicious use of whitespace
# - ensure id attributes are first # - ensure id attributes are first
def serializeXML(element, options, ind = 0, preserveWhitespace = False): def serializeXML(element, options, ind = 0, preserveWhitespace = False):
outParts = []
indent = ind indent = ind
I='' I=''
if options.indent_type == 'tab': I='\t' if options.indent_type == 'tab': I='\t'
elif options.indent_type == 'space': I=' ' elif options.indent_type == 'space': I=' '
outString = (I * ind) + '<' + element.nodeName outParts.extend([(I * ind), '<', element.nodeName])
# always serialize the id or xml:id attributes first # always serialize the id or xml:id attributes first
if element.getAttribute('id') != '': if element.getAttribute('id') != '':
@ -2493,13 +2489,13 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
quot = '"' quot = '"'
if id.find('"') != -1: if id.find('"') != -1:
quot = "'" quot = "'"
outString += ' ' + 'id=' + quot + id + quot outParts.extend([' id=', quot, id, quot])
if element.getAttribute('xml:id') != '': if element.getAttribute('xml:id') != '':
id = element.getAttribute('xml:id') id = element.getAttribute('xml:id')
quot = '"' quot = '"'
if id.find('"') != -1: if id.find('"') != -1:
quot = "'" quot = "'"
outString += ' ' + 'xml:id=' + quot + id + quot outParts.extend([' xml:id=', quot, id, quot])
# now serialize the other attributes # now serialize the other attributes
attrList = element.attributes attrList = element.attributes
@ -2513,16 +2509,16 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
attrValue = makeWellFormed( attr.nodeValue ) attrValue = makeWellFormed( attr.nodeValue )
outString += ' ' outParts.append(' ')
# preserve xmlns: if it is a namespace prefix declaration # preserve xmlns: if it is a namespace prefix declaration
if attr.prefix != None: if attr.prefix != None:
outString += attr.prefix + ':' outParts.extend([attr.prefix, ':'])
elif attr.namespaceURI != None: elif attr.namespaceURI != None:
if attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' and attr.nodeName.find('xmlns') == -1: if attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' and attr.nodeName.find('xmlns') == -1:
outString += 'xmlns:' outParts.append('xmlns:')
elif attr.namespaceURI == 'http://www.w3.org/1999/xlink': elif attr.namespaceURI == 'http://www.w3.org/1999/xlink':
outString += 'xlink:' outParts.append('xlink:')
outString += attr.localName + '=' + quot + attrValue + quot outParts.extend([attr.localName, '=', quot, attrValue, quot])
if attr.nodeName == 'xml:space': if attr.nodeName == 'xml:space':
if attrValue == 'preserve': if attrValue == 'preserve':
@ -2533,43 +2529,43 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
# if no children, self-close # if no children, self-close
children = element.childNodes children = element.childNodes
if children.length > 0: if children.length > 0:
outString += '>' outParts.append('>')
onNewLine = False onNewLine = False
for child in element.childNodes: for child in element.childNodes:
# element node # element node
if child.nodeType == 1: if child.nodeType == 1:
if preserveWhitespace: if preserveWhitespace:
outString += serializeXML(child, options, 0, preserveWhitespace) outParts.append(serializeXML(child, options, 0, preserveWhitespace))
else: else:
outString += os.linesep + serializeXML(child, options, indent + 1, preserveWhitespace) outParts.extend([os.linesep, serializeXML(child, options, indent + 1, preserveWhitespace)])
onNewLine = True onNewLine = True
# text node # text node
elif child.nodeType == 3: elif child.nodeType == 3:
# trim it only in the case of not being a child of an element # trim it only in the case of not being a child of an element
# where whitespace might be important # where whitespace might be important
if preserveWhitespace: if preserveWhitespace:
outString += makeWellFormed(child.nodeValue) outParts.append(makeWellFormed(child.nodeValue))
else: else:
outString += makeWellFormed(child.nodeValue.strip()) outParts.append(makeWellFormed(child.nodeValue.strip()))
# CDATA node # CDATA node
elif child.nodeType == 4: elif child.nodeType == 4:
outString += '<![CDATA[' + child.nodeValue + ']]>' outParts.extend(['<![CDATA[', child.nodeValue, ']]>'])
# Comment node # Comment node
elif child.nodeType == 8: elif child.nodeType == 8:
outString += '<!--' + child.nodeValue + '-->' outParts.extend(['<!--', child.nodeValue, '-->'])
# TODO: entities, processing instructions, what else? # TODO: entities, processing instructions, what else?
else: # ignore the rest else: # ignore the rest
pass pass
if onNewLine: outString += (I * ind) if onNewLine: outParts.append(I * ind)
outString += '</' + element.nodeName + '>' outParts.extend(['</', element.nodeName, '>'])
if indent > 0: outString += os.linesep if indent > 0: outParts.append(os.linesep)
else: else:
outString += '/>' outParts.append('/>')
if indent > 0: outString += os.linesep if indent > 0: outParts.append(os.linesep)
return outString return "".join(outParts)
# this is the main method # this is the main method
# input is a string representation of the input XML # input is a string representation of the input XML