#!/usr/bin/env python """ Copyright (C) 2006 Wade Mealing This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ import getopt import sys , optparse from xml.dom import minidom import re from inkex import InkOption __author__ = "Wade Mealing " __version__ = "$Revision: 0.02 $" __date__ = "$Date: Wed Jul 26 11:01:20 2006 $" __copyright__ = "Copyright (c) 2006 Wade Mealing" __license__ = "Python" class InkscapeDoc: sourcefile = "" precision = 2 my_regex = r'[0-9]+\.[0-9]+' p = re.compile( my_regex ) elements_to_round = [ 'x', 'y', 'x1' , 'y1', 'x2', 'y2', 'width', 'height' , 'd' , 'sodipodi:ry', 'sodipodi:rx', 'sodipodi:cy', 'sodipody:cx' , 'cx', 'cy', 'fx', 'fy', 'r' ] def __init__(self, sourcefile, precision): self.sourcefile = sourcefile self.precision = precision self.xmldoc = minidom.parse( self.sourcefile ) stream = open( sourcefile) stream.close() self.parse( self.xmldoc) def getxml(self): return self.xmldoc.toxml() def roundnum(self, match ): return str ( round( float (match.group()), self.precision) ) def parse_Document(self, node): for c in node.childNodes: self.parse( c ) def parse_Element(self, node): # get a list of the attributes for this element keys = node.attributes.keys() for at in keys: if unicode(at) in self.elements_to_round: x = self.p.sub(self.roundnum, node.attributes[at].value) node.attributes[at].value = x def parse_Text(self, node): pass def parse_Comment(self,node): pass def parse( self, node): parseMethod = getattr(self , "parse_%s" % node.__class__.__name__) parseMethod(node) for c in node.childNodes: self.parse( c ) class SetPrecision: def __init__(self): self.document=None self.selected={} self.options=None self.args=None self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption) self.OptionParser.add_option("--id", action="append", type="string", dest="ids", default=[], help="id attribute of object to manipulate") self.OptionParser.add_option("-p", "--precision", action="store", type="int", dest="precision", default=2, help="The number of decimal places you wish to have numbers rounded to") def getoptions(self,args=sys.argv[1:]): """Collect command line arguments""" self.options, self.args = self.OptionParser.parse_args(args) def execute(self): # I essentially just skipped all the inkex magic, I know. # pdb.set_trace() self.getoptions() k = InkscapeDoc( self.args[0], self.options.precision ) print k.getxml() e = SetPrecision() e.execute()