#!/usr/bin/env python """ inkex.py A helper module for creating Inkscape extensions """ import sys, getopt import xml.dom.ext import xml.dom.ext.reader.Sax2 import xml.xpath def parseStyle(s): """Create a dictionary from the value of an inline style attribute""" return dict([i.split(":") for i in s.split(";")]) def formatStyle(a): """Format an inline style attribute from a dictionary""" return ";".join([":".join(i) for i in a.iteritems()]) def affect(effect_callback): """Affect an SVG document with a callback effect""" #collect ids from the arguments #TODO: get other arguments? opts = getopt.getopt(sys.argv[1:],'',['id=']) ids = [opt[1] for opt in opts[0] if opt[0]=='--id'] #create xml parser reader = xml.dom.ext.reader.Sax2.Reader() #open SVG tempfile #TODO: optionailly read from STDIN stream = open(sys.argv[-1:][0],'r') #create DOM object doc = reader.fromStream(stream) stream.close() #collect nodes nodes = {} for id in ids: path = '//*[@id="%s"]' % id for node in xml.xpath.Evaluate(path,doc): nodes[id] = node #affect the document params = { 'doc': doc, 'selected': nodes } effect_callback(params) #serialize DOM object into XML on stdout xml.dom.ext.Print(doc)