···
import xml.sax.saxutils as xml
···
def checkChildrenAllowed(self):
'{} does not support children'.format(type(self)))
return self.args.get('id', None)
···
def writeSvgElement(self, idGen, isDuplicate, outputFile, dryRun,
if isDuplicate(self) and self.id is None:
···
self.writeContent(idGen, isDuplicate, outputFile, dryRun)
self.writeChildrenContent(idGen, isDuplicate, outputFile,
···
outputFile.write(self.TAG_NAME)
writeXmlNodeArgs(self.args, outputFile)
-
if not self.hasContent and not self.children:
self.writeContent(idGen, isDuplicate, outputFile, dryRun)
self.writeChildrenContent(idGen, isDuplicate, outputFile,
···
def writeChildrenContent(self, idGen, isDuplicate, outputFile, dryRun):
''' Override in a subclass to add data between the start and end
tags. This will not be called if hasContent is False. '''
-
for child in self.children:
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
-
for child in self.children:
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
···
if isinstance(v, DrawingElement)]
def writeSvgDefs(self, idGen, isDuplicate, outputFile, dryRun):
super().writeSvgDefs(idGen, isDuplicate, outputFile, dryRun)
-
for child in self.children:
child.writeSvgDefs(idGen, isDuplicate, outputFile, dryRun)
if isinstance(other, type(self)):
return (self.TAG_NAME == other.TAG_NAME and
self.args == other.args and
-
self.children == other.children)
def appendAnim(self, animateElement):
self.children.append(animateElement)
···
class DrawingParentElement(DrawingBasicElement):
''' Base class for SVG elements that can have child nodes '''
-
def __init__(self, children=(), **args):
self.children = list(children)
-
if len(self.children) > 0:
self.checkChildrenAllowed()
-
def draw(self, obj, **kwargs):
if not hasattr(obj, 'writeSvgElement'):
elements = obj.toDrawables(elements=elementsModule, **kwargs)
-
def append(self, element):
self.checkChildrenAllowed()
-
self.children.append(element)
-
def extend(self, iterable):
self.checkChildrenAllowed()
-
self.children.extend(iterable)
def writeContent(self, idGen, isDuplicate, outputFile, dryRun):
···
def writeChildrenContent(self, idGen, isDuplicate, outputFile, dryRun):
''' Override in a subclass to add data between the start and end
tags. This will not be called if hasContent is False. '''
-
for child in self.children:
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
-
for child in self.children:
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
def appendLine(self, line, **kwargs):
self.append(TSpan(line, **kwargs))
···
import xml.sax.saxutils as xml
+
from collections import defaultdict
···
+
self.orderedChildren = defaultdict(list)
def checkChildrenAllowed(self):
'{} does not support children'.format(type(self)))
+
''' Returns self.children and self.orderedChildren as a single list. '''
+
output = list(self.children)
+
for z in sorted(self.orderedChildren):
+
output.extend(self.orderedChildren[z])
return self.args.get('id', None)
···
def writeSvgElement(self, idGen, isDuplicate, outputFile, dryRun,
+
children = self.allChildren()
if isDuplicate(self) and self.id is None:
···
self.writeContent(idGen, isDuplicate, outputFile, dryRun)
self.writeChildrenContent(idGen, isDuplicate, outputFile,
···
outputFile.write(self.TAG_NAME)
writeXmlNodeArgs(self.args, outputFile)
+
if not self.hasContent and not children:
self.writeContent(idGen, isDuplicate, outputFile, dryRun)
self.writeChildrenContent(idGen, isDuplicate, outputFile,
···
def writeChildrenContent(self, idGen, isDuplicate, outputFile, dryRun):
''' Override in a subclass to add data between the start and end
tags. This will not be called if hasContent is False. '''
+
children = self.allChildren()
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
···
if isinstance(v, DrawingElement)]
def writeSvgDefs(self, idGen, isDuplicate, outputFile, dryRun):
super().writeSvgDefs(idGen, isDuplicate, outputFile, dryRun)
+
for child in self.allChildren():
child.writeSvgDefs(idGen, isDuplicate, outputFile, dryRun)
if isinstance(other, type(self)):
return (self.TAG_NAME == other.TAG_NAME and
self.args == other.args and
+
self.children == other.children and
+
self.orderedChildren == other.orderedChildren)
def appendAnim(self, animateElement):
self.children.append(animateElement)
···
class DrawingParentElement(DrawingBasicElement):
''' Base class for SVG elements that can have child nodes '''
+
def __init__(self, children=(), orderedChildren=None, **args):
self.children = list(children)
+
self.orderedChildren.update(
+
(z, list(v)) for z, v in orderedChildren.items())
+
if self.children or self.orderedChildren:
self.checkChildrenAllowed()
+
def draw(self, obj, *, z=None, **kwargs):
if not hasattr(obj, 'writeSvgElement'):
elements = obj.toDrawables(elements=elementsModule, **kwargs)
+
self.extend(elements, z=z)
+
def append(self, element, *, z=None):
self.checkChildrenAllowed()
+
self.orderedChildren[z].append(element)
+
self.children.append(element)
+
def extend(self, iterable, *, z=None):
self.checkChildrenAllowed()
+
self.orderedChildren[z].extend(iterable)
+
self.children.extend(iterable)
def writeContent(self, idGen, isDuplicate, outputFile, dryRun):
···
def writeChildrenContent(self, idGen, isDuplicate, outputFile, dryRun):
''' Override in a subclass to add data between the start and end
tags. This will not be called if hasContent is False. '''
+
children = self.allChildren()
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
def appendLine(self, line, **kwargs):
self.append(TSpan(line, **kwargs))