From 221983b0b68acaa97f8b2c2ba74b7e7fc0e3a84f Mon Sep 17 00:00:00 2001 From: Benjamin Mako Hill Date: Sun, 27 Dec 2020 07:20:49 +0100 Subject: [PATCH] updates for python3 --- SVGdraw.py | 270 ++++++++++++++++++++++++++-------------------------- svgruler.py | 6 +- yourule.py | 16 ++-- 3 files changed, 145 insertions(+), 147 deletions(-) diff --git a/SVGdraw.py b/SVGdraw.py index 6ee3549..5d4a194 100644 --- a/SVGdraw.py +++ b/SVGdraw.py @@ -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("&", "&") 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 @@ -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="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("\n") xml.write("\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()) diff --git a/svgruler.py b/svgruler.py index ccba888..21b7a28 100644 --- a/svgruler.py +++ b/svgruler.py @@ -19,7 +19,7 @@ # . # define the length of things -from __future__ import division + import SVGdraw class SVGRuler: @@ -77,8 +77,8 @@ class SVGRuler: self.drawing.setSVG(self.svg) def getxml(self): - import cStringIO - xml = cStringIO.StringIO() + import io + xml = io.StringIO() xml.write("\n") xml.write("\n") self.svg.toXml(0,xml) diff --git a/yourule.py b/yourule.py index 0d8adbc..ebd85cb 100755 --- a/yourule.py +++ b/yourule.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # YouRule: Onscreen Ruler Generator # @@ -19,7 +19,7 @@ # . -from __future__ import division + import web import sys, os, re from storm.locals import * @@ -28,7 +28,7 @@ from storm.locals import * sys.path.append(os.path.dirname(__file__)) from svgruler import SVGRuler -from jinja import Environment, FileSystemLoader +from jinja2 import Environment, FileSystemLoader jinja_env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>', loader=FileSystemLoader(os.path.dirname(__file__) + "/templates/")) @@ -69,11 +69,11 @@ class Ruler(object): def __init__(self, **kw): self.pixel_width = float(kw['pixel_width']) self.unit_width = float(kw['unit_width']) - self.units = unicode(kw['units']) - if kw.has_key('model'): - self.model = unicode(kw['model']) + self.units = str(kw['units']) + if 'model' in kw: + self.model = str(kw['model']) else: - self.model = u'' + self.model = '' def cm_width(self): if self.units == 'centimeters': @@ -115,7 +115,7 @@ class index: class show_ruler: def GET(self, ruler_url, ext): - if web.input().has_key('fromgallery'): + if 'fromgallery' in web.input(): fromgallery = True else: fromgallery = False -- 2.30.2