# Anyway the text based approach is about 60 times faster than using the full dom implementation.
use_dom_implementation=0
-
-import exceptions
-if use_dom_implementation<>0:
+if use_dom_implementation!=0:
try:
from xml.dom import implementation
from xml.dom.ext import PrettyPrint
except:
- raise exceptions.ImportError, "PyXML is required for using the dom implementation"
+ raise ImportError("PyXML is required for using the dom implementation")
#The implementation is used for the creating the XML document.
#The prettyprint module is used for converting the xml document object to a xml file
import sys
-assert sys.version_info[0]>=2
-if sys.version_info[1]<2:
- True=1
- False=0
- file=open
+# assert sys.version_info[0]>=2
+# if sys.version_info[1]<2:
+# True=1
+# False=0
+# file=open
sys.setrecursionlimit=50
#The recursion limit is set conservative so mistakes like s=svg() s.addElement(s)
data = data.replace("&", "&")
data = data.replace("<", "<")
data = data.replace(">", ">")
- for chars, entity in entities.items():
+ for chars, entity in list(entities.items()):
data = data.replace(chars, entity)
return data
self.text=text
self.namespace=namespace
self.cdata=cdata
- for arg in args.keys():
+ for arg in list(args.keys()):
self.attributes[arg]=args[arg]
def addElement(self,SVGelement):
"""adds an element to a SVGelement
def toXml(self,level,f):
f.write('\t'*level)
f.write('<'+self.type)
- for attkey in self.attributes.keys():
+ for attkey in list(self.attributes.keys()):
f.write(' '+_escape(str(attkey))+'='+_quoteattr(str(self.attributes[attkey])))
if self.namespace:
f.write(' xmlns="'+ _escape(str(self.namespace))+'" ')
"""
def __init__(self,text=None,**args):
SVGelement.__init__(self,'tspan',**args)
- if self.text<>None:
+ if self.text!=None:
self.text=text
def __repr__(self):
s="<tspan"
- for key,value in self.attributes.items():
+ for key,value in list(self.attributes.items()):
s+= ' %s="%s"' % (key,value)
s+='>'
s+=self.text
def __repr__(self):
s="<tref"
- for key,value in self.attributes.items():
+ for key,value in list(self.attributes.items()):
s+= ' %s="%s"' % (key,value)
s+='/>'
return s
"""
def __init__(self,x=None,y=None,width=None,height=None,fill=None,stroke=None,stroke_width=None,**args):
if width==None or height==None:
- if width<>None:
- raise ValueError, 'height is required'
- if height<>None:
- raise ValueError, 'width is required'
+ if width!=None:
+ raise ValueError('height is required')
+ if height!=None:
+ raise ValueError('width is required')
else:
- raise ValueError, 'both height and width are required'
+ raise ValueError('both height and width are required')
SVGelement.__init__(self,'rect',{'width':width,'height':height},**args)
- if x<>None:
+ if x!=None:
self.attributes['x']=x
- if y<>None:
+ if y!=None:
self.attributes['y']=y
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
class ellipse(SVGelement):
"""
def __init__(self,cx=None,cy=None,rx=None,ry=None,fill=None,stroke=None,stroke_width=None,**args):
if rx==None or ry== None:
- if rx<>None:
- raise ValueError, 'rx is required'
- if ry<>None:
- raise ValueError, 'ry is required'
+ if rx!=None:
+ raise ValueError('rx is required')
+ if ry!=None:
+ raise ValueError('ry is required')
else:
- raise ValueError, 'both rx and ry are required'
+ raise ValueError('both rx and ry are required')
SVGelement.__init__(self,'ellipse',{'rx':rx,'ry':ry},**args)
- if cx<>None:
+ if cx!=None:
self.attributes['cx']=cx
- if cy<>None:
+ if cy!=None:
self.attributes['cy']=cy
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
"""
def __init__(self,cx=None,cy=None,r=None,fill=None,stroke=None,stroke_width=None,**args):
if r==None:
- raise ValueError, 'r is required'
+ raise ValueError('r is required')
SVGelement.__init__(self,'circle',{'r':r},**args)
- if cx<>None:
+ if cx!=None:
self.attributes['cx']=cx
- if cy<>None:
+ if cy!=None:
self.attributes['cy']=cy
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
class point(circle):
"""
def __init__(self,x1=None,y1=None,x2=None,y2=None,stroke=None,stroke_width=None,**args):
SVGelement.__init__(self,'line',**args)
- if x1<>None:
+ if x1!=None:
self.attributes['x1']=x1
- if y1<>None:
+ if y1!=None:
self.attributes['y1']=y1
- if x2<>None:
+ if x2!=None:
self.attributes['x2']=x2
- if y2<>None:
+ if y2!=None:
self.attributes['y2']=y2
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
class polyline(SVGelement):
"""
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
SVGelement.__init__(self,'polyline',{'points':_xypointlist(points)},**args)
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
class polygon(SVGelement):
"""
def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
SVGelement.__init__(self,'polygon',{'points':_xypointlist(points)},**args)
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
class path(SVGelement):
"""
def __init__(self,pathdata,fill=None,stroke=None,stroke_width=None,id=None,**args):
SVGelement.__init__(self,'path',{'d':str(pathdata)},**args)
- if stroke<>None:
+ if stroke!=None:
self.attributes['stroke']=stroke
- if fill<>None:
+ if fill!=None:
self.attributes['fill']=fill
- if stroke_width<>None:
+ if stroke_width!=None:
self.attributes['stroke-width']=stroke_width
- if id<>None:
+ if id!=None:
self.attributes['id']=id
"""
def __init__(self,x=None,y=None,text=None,font_size=None,font_family=None,text_anchor=None,**args):
SVGelement.__init__(self,'text',**args)
- if x<>None:
+ if x!=None:
self.attributes['x']=x
- if y<>None:
+ if y!=None:
self.attributes['y']=y
- if font_size<>None:
+ if font_size!=None:
self.attributes['font-size']=font_size
- if font_family<>None:
+ if font_family!=None:
self.attributes['font-family']=font_family
- if text<>None:
+ if text!=None:
self.text=text
- if text_anchor<>None:
+ if text_anchor!=None:
self.attributes['text-anchor']=text_anchor
"""
def __init__(self,link,text=None,**args):
SVGelement.__init__(self,'textPath',{'xlink:href':link},**args)
- if text<>None:
+ if text!=None:
self.text=text
class pattern(SVGelement):
"""
def __init__(self,x=None,y=None,width=None,height=None,patternUnits=None,**args):
SVGelement.__init__(self,'pattern',**args)
- if x<>None:
+ if x!=None:
self.attributes['x']=x
- if y<>None:
+ if y!=None:
self.attributes['y']=y
- if width<>None:
+ if width!=None:
self.attributes['width']=width
- if height<>None:
+ if height!=None:
self.attributes['height']=height
- if patternUnits<>None:
+ if patternUnits!=None:
self.attributes['patternUnits']=patternUnits
class title(SVGelement):
"""
def __init__(self,text=None,**args):
SVGelement.__init__(self,'title',**args)
- if text<>None:
+ if text!=None:
self.text=text
class description(SVGelement):
"""
def __init__(self,text=None,**args):
SVGelement.__init__(self,'desc',**args)
- if text<>None:
+ if text!=None:
self.text=text
class lineargradient(SVGelement):
"""
def __init__(self,x1=None,y1=None,x2=None,y2=None,id=None,**args):
SVGelement.__init__(self,'linearGradient',**args)
- if x1<>None:
+ if x1!=None:
self.attributes['x1']=x1
- if y1<>None:
+ if y1!=None:
self.attributes['y1']=y1
- if x2<>None:
+ if x2!=None:
self.attributes['x2']=x2
- if y2<>None:
+ if y2!=None:
self.attributes['y2']=y2
- if id<>None:
+ if id!=None:
self.attributes['id']=id
class radialgradient(SVGelement):
"""
def __init__(self,cx=None,cy=None,r=None,fx=None,fy=None,id=None,**args):
SVGelement.__init__(self,'radialGradient',**args)
- if cx<>None:
+ if cx!=None:
self.attributes['cx']=cx
- if cy<>None:
+ if cy!=None:
self.attributes['cy']=cy
- if r<>None:
+ if r!=None:
self.attributes['r']=r
- if fx<>None:
+ if fx!=None:
self.attributes['fx']=fx
- if fy<>None:
+ if fy!=None:
self.attributes['fy']=fy
- if id<>None:
+ if id!=None:
self.attributes['id']=id
class stop(SVGelement):
"""
def __init__(self,offset,stop_color=None,**args):
SVGelement.__init__(self,'stop',{'offset':offset},**args)
- if stop_color<>None:
+ if stop_color!=None:
self.attributes['stop-color']=stop_color
class style(SVGelement):
"""
def __init__(self,url,x=None,y=None,width=None,height=None,**args):
if width==None or height==None:
- if width<>None:
- raise ValueError, 'height is required'
- if height<>None:
- raise ValueError, 'width is required'
+ if width!=None:
+ raise ValueError('height is required')
+ if height!=None:
+ raise ValueError('width is required')
else:
- raise ValueError, 'both height and width are required'
+ raise ValueError('both height and width are required')
SVGelement.__init__(self,'image',{'xlink:href':url,'width':width,'height':height},**args)
- if x<>None:
+ if x!=None:
self.attributes['x']=x
- if y<>None:
+ if y!=None:
self.attributes['y']=y
class cursor(SVGelement):
"""
def __init__(self,id=None,viewBox=None,refx=None,refy=None,markerWidth=None,markerHeight=None,**args):
SVGelement.__init__(self,'marker',**args)
- if id<>None:
+ if id!=None:
self.attributes['id']=id
- if viewBox<>None:
+ if viewBox!=None:
self.attributes['viewBox']=_viewboxlist(viewBox)
- if refx<>None:
+ if refx!=None:
self.attributes['refX']=refx
- if refy<>None:
+ if refy!=None:
self.attributes['refY']=refy
- if markerWidth<>None:
+ if markerWidth!=None:
self.attributes['markerWidth']=markerWidth
- if markerHeight<>None:
+ if markerHeight!=None:
self.attributes['markerHeight']=markerHeight
class group(SVGelement):
"""
def __init__(self,id=None,**args):
SVGelement.__init__(self,'g',**args)
- if id<>None:
+ if id!=None:
self.attributes['id']=id
class symbol(SVGelement):
def __init__(self,id=None,viewBox=None,**args):
SVGelement.__init__(self,'symbol',**args)
- if id<>None:
+ if id!=None:
self.attributes['id']=id
- if viewBox<>None:
+ if viewBox!=None:
self.attributes['viewBox']=_viewboxlist(viewBox)
class defs(SVGelement):
"""
def __init__(self,link,x=None,y=None,width=None,height=None,**args):
SVGelement.__init__(self,'use',{'xlink:href':link},**args)
- if x<>None:
+ if x!=None:
self.attributes['x']=x
- if y<>None:
+ if y!=None:
self.attributes['y']=y
- if width<>None:
+ if width!=None:
self.attributes['width']=width
- if height<>None:
+ if height!=None:
self.attributes['height']=height
a view can be used to create a view with different attributes"""
def __init__(self,id=None,**args):
SVGelement.__init__(self,'view',**args)
- if id<>None:
+ if id!=None:
self.attributes['id']=id
class script(SVGelement):
"""
def __init__(self,attribute,fr=None,to=None,dur=None,**args):
SVGelement.__init__(self,'animate',{'attributeName':attribute},**args)
- if fr<>None:
+ if fr!=None:
self.attributes['from']=fr
- if to<>None:
+ if to!=None:
self.attributes['to']=to
- if dur<>None:
+ if dur!=None:
self.attributes['dur']=dur
class animateMotion(SVGelement):
"""
def __init__(self,pathdata,dur,**args):
SVGelement.__init__(self,'animateMotion',**args)
- if pathdata<>None:
+ if pathdata!=None:
self.attributes['path']=str(pathdata)
- if dur<>None:
+ if dur!=None:
self.attributes['dur']=dur
class animateTransform(SVGelement):
def __init__(self,type=None,fr=None,to=None,dur=None,**args):
SVGelement.__init__(self,'animateTransform',{'attributeName':'transform'},**args)
#As far as I know the attributeName is always transform
- if type<>None:
+ if type!=None:
self.attributes['type']=type
- if fr<>None:
+ if fr!=None:
self.attributes['from']=fr
- if to<>None:
+ if to!=None:
self.attributes['to']=to
- if dur<>None:
+ if dur!=None:
self.attributes['dur']=dur
class animateColor(SVGelement):
"""ac=animateColor(attribute,type,from,to,dur,**args)
"""
def __init__(self,attribute,type=None,fr=None,to=None,dur=None,**args):
SVGelement.__init__(self,'animateColor',{'attributeName':attribute},**args)
- if type<>None:
+ if type!=None:
self.attributes['type']=type
- if fr<>None:
+ if fr!=None:
self.attributes['from']=fr
- if to<>None:
+ if to!=None:
self.attributes['to']=to
- if dur<>None:
+ if dur!=None:
self.attributes['dur']=dur
class set(SVGelement):
"""st=set(attribute,to,during,**args)
"""
def __init__(self,attribute,to=None,dur=None,**args):
SVGelement.__init__(self,'set',{'attributeName':attribute},**args)
- if to<>None:
+ if to!=None:
self.attributes['to']=to
- if dur<>None:
+ if dur!=None:
self.attributes['dur']=dur
"""
def __init__(self,viewBox=None, width=None, height=None,**args):
SVGelement.__init__(self,'svg',**args)
- if viewBox<>None:
+ if viewBox!=None:
self.attributes['viewBox']=_viewboxlist(viewBox)
- if width<>None:
+ if width!=None:
self.attributes['width']=width
- if height<>None:
+ if height!=None:
self.attributes['height']=height
self.namespace="http://www.w3.org/2000/svg"
#Voeg een element toe aan de grafiek toe.
if use_dom_implementation==0:
def toXml(self, filename='',compress=False):
- import cStringIO
- xml=cStringIO.StringIO()
+ import io
+ xml=io.StringIO()
xml.write("<?xml version='1.0' encoding='UTF-8'?>\n")
xml.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n")
self.svg.toXml(0,xml)
if not filename:
if compress:
import gzip
- f=cStringIO.StringIO()
+ f=io.StringIO()
zf=gzip.GzipFile(fileobj=f,mode='wb')
zf.write(xml.getvalue())
zf.close()
if element.text:
textnode=root.createTextNode(element.text)
e.appendChild(textnode)
- for attribute in element.attributes.keys(): #in element.attributes is supported from python 2.2
+ for attribute in list(element.attributes.keys()): #in element.attributes is supported from python 2.2
e.setAttribute(attribute,str(element.attributes[attribute]))
if element.elements:
for el in element.elements:
return elementroot
root=appender(self.svg,root)
if not filename:
- import cStringIO
- xml=cStringIO.StringIO()
+ import io
+ xml=io.StringIO()
PrettyPrint(root,xml)
if compress:
import gzip
- f=cStringIO.StringIO()
+ f=io.StringIO()
zf=gzip.GzipFile(fileobj=f,mode='wb')
zf.write(xml.getvalue())
zf.close()
try:
if filename[-4:]=='svgz':
import gzip
- import cStringIO
- xml=cStringIO.StringIO()
+ import io
+ xml=io.StringIO()
PrettyPrint(root,xml)
f=gzip.GzipFile(filename=filename,mode='wb',compresslevel=9)
f.write(xml.getvalue())
PrettyPrint(root,f)
f.close()
except:
- print "Cannot write SVG file: " + filename
+ print("Cannot write SVG file: " + filename)
def validate(self):
try:
import xml.parsers.xmlproc.xmlval
except:
- raise exceptions.ImportError,'PyXml is required for validating SVG'
+ raise ImportError('PyXml is required for validating SVG')
svg=self.toXml()
xv=xml.parsers.xmlproc.xmlval.XMLValidator()
try:
xv.feed(svg)
except:
- raise "SVG is not well formed, see messages above"
+ raise # "SVG is not well formed, see messages above"
else:
- print "SVG well formed"
+ print("SVG well formed")
if __name__=='__main__':
s.addElement(c)
d.setSVG(s)
- print d.toXml()
+ print(d.toXml())