home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June (Extra) / CHIP 2006-06.3.iso / program / opensource / Inkscape-0.43-2.win32.exe / share / extensions / inkex.py < prev    next >
Encoding:
Python Source  |  2005-06-16  |  2.8 KB  |  92 lines

  1. #!/usr/bin/env python
  2. """
  3. inkex.py
  4. A helper module for creating Inkscape extensions
  5.  
  6. Copyright (C) 2005 Aaron Spike, aaron@ekips.org
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. """
  22. import sys, copy, optparse
  23. try:
  24.     import xml.dom.ext
  25.     import xml.dom.ext.reader.Sax2
  26.     import xml.xpath
  27. except:
  28.     sys.exit('The inkex.py module requires PyXML. Please download the latest version from <http://pyxml.sourceforge.net/>.')
  29.  
  30. def debug(what):
  31.     sys.stderr.write(str(what) + "\n")
  32.     return what
  33.  
  34. def check_inkbool(option, opt, value):
  35.     if str(value).capitalize() == 'True':
  36.         return True
  37.     elif str(value).capitalize() == 'False':
  38.         return False
  39.     else:
  40.         raise OptionValueError("option %s: invalid inkbool value: %s" % (opt, value))
  41.  
  42. class InkOption(optparse.Option):
  43.     TYPES = optparse.Option.TYPES + ("inkbool",)
  44.     TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER)
  45.     TYPE_CHECKER["inkbool"] = check_inkbool
  46.  
  47.  
  48. class Effect:
  49.     """A class for creating Inkscape SVG Effects"""
  50.     def __init__(self):
  51.         self.document=None
  52.         self.selected={}
  53.         self.options=None
  54.         self.args=None
  55.         self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption)
  56.         self.OptionParser.add_option("--id",
  57.                         action="append", type="string", dest="ids", default=[], 
  58.                         help="id attribute of object to manipulate")
  59.     def effect(self):
  60.         pass
  61.     def getoptions(self,args=sys.argv[1:]):
  62.         """Collect command line arguments"""
  63.         self.options, self.args = self.OptionParser.parse_args(args)
  64.     def parse(self,file=None):
  65.         """Parse document in specified file or on stdin"""
  66.         reader = xml.dom.ext.reader.Sax2.Reader()
  67.         try:
  68.             try:
  69.                 stream = open(file,'r')
  70.             except:
  71.                 stream = open(self.args[-1],'r')
  72.         except:
  73.             stream = sys.stdin
  74.         self.document = reader.fromStream(stream)
  75.         stream.close()
  76.     def getselected(self):
  77.         """Collect selected nodes"""
  78.         for id in self.options.ids:
  79.             path = '//*[@id="%s"]' % id
  80.             for node in xml.xpath.Evaluate(path,self.document):
  81.                 self.selected[id] = node
  82.     def output(self):
  83.         """Serialize document into XML on stdout"""
  84.         xml.dom.ext.Print(self.document)
  85.     def affect(self):
  86.         """Affect an SVG document with a callback effect"""
  87.         self.getoptions()
  88.         self.parse()
  89.         self.getselected()
  90.         self.effect()
  91.         self.output()
  92.