Merge branch 'master' of /tmp/yourule-esperanza/yourule into master
[yourule] / SVGdraw.py
index 6ee35492bdbbe2b0977d8a03477de0a053692381..5d4a19400a6bb30f4e9b7ed4fcd16f56905ef487 100644 (file)
@@ -69,23 +69,21 @@ __version__="1.0"
 # 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)
@@ -104,7 +102,7 @@ def _escape(data, entities={}):
     data = data.replace("&", "&amp;")
     data = data.replace("<", "&lt;")
     data = data.replace(">", "&gt;")
-    for chars, entity in entities.items():
+    for chars, entity in list(entities.items()):
         data = data.replace(chars, entity)
     return data
 
@@ -243,7 +241,7 @@ class SVGelement:
         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
@@ -255,7 +253,7 @@ class 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))+'" ')
@@ -297,11 +295,11 @@ class tspan(SVGelement):
     """
     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
@@ -323,7 +321,7 @@ class tref(SVGelement):
     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
@@ -370,22 +368,22 @@ class rect(SVGelement):
     """
     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):
@@ -395,22 +393,22 @@ 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
         
    
@@ -421,17 +419,17 @@ class circle(SVGelement):
     """
     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):
@@ -450,17 +448,17 @@ class line(SVGelement):
     """
     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):
@@ -470,11 +468,11 @@ 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):
@@ -484,11 +482,11 @@ 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):
@@ -498,13 +496,13 @@ 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
         
         
@@ -515,17 +513,17 @@ class text(SVGelement):
     """
     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
 
 
@@ -536,7 +534,7 @@ class textpath(SVGelement):
     """
     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):
@@ -548,15 +546,15 @@ 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):
@@ -567,7 +565,7 @@ 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):
@@ -578,7 +576,7 @@ 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):
@@ -589,15 +587,15 @@ 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):
@@ -608,17 +606,17 @@ 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):
@@ -628,7 +626,7 @@ 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):
@@ -647,16 +645,16 @@ class image(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):
@@ -676,17 +674,17 @@ class marker(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):
@@ -697,7 +695,7 @@ 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):
@@ -711,9 +709,9 @@ 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):
@@ -742,14 +740,14 @@ class use(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
             
             
@@ -768,7 +766,7 @@ class view(SVGelement):
     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):
@@ -787,11 +785,11 @@ class animate(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):
@@ -801,9 +799,9 @@ 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):
@@ -814,13 +812,13 @@ 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)
@@ -829,13 +827,13 @@ class animateColor(SVGelement):
     """
     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)
@@ -844,9 +842,9 @@ class set(SVGelement):
     """
     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
 
 
@@ -868,11 +866,11 @@ class svg(SVGelement):
     """
     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"
         
@@ -894,15 +892,15 @@ class drawing:
         #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()
@@ -946,7 +944,7 @@ class drawing:
                 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:
@@ -955,12 +953,12 @@ class drawing:
                 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()
@@ -972,8 +970,8 @@ class drawing:
                 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())
@@ -983,20 +981,20 @@ class drawing:
                         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__':
 
     
@@ -1030,5 +1028,5 @@ if __name__=='__main__':
             s.addElement(c)
     d.setSVG(s)
      
-    print d.toXml()
+    print(d.toXml())
 

Benjamin Mako Hill || Want to submit a patch?