Code

Extensions. Fix misleading message in inkex.py (Bug #602041)
[inkscape.git] / share / extensions / inkex.py
index adc0d0fed59e6f246ecb855314ec9a78b2a1d02e..7d40bc70ca75f306ebdd4e28714844b86807396d 100755 (executable)
@@ -27,7 +27,8 @@ _ = gettext.gettext
 #a dictionary of all of the xmlns prefixes in a standard inkscape doc
 NSS = {
 u'sodipodi' :u'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
-u'cc'       :u'http://web.resource.org/cc/',
+u'cc'       :u'http://creativecommons.org/ns#',
+u'ccOLD'    :u'http://web.resource.org/cc/',
 u'svg'      :u'http://www.w3.org/2000/svg',
 u'dc'       :u'http://purl.org/dc/elements/1.1/',
 u'rdf'      :u'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
@@ -37,7 +38,8 @@ u'xml'      :u'http://www.w3.org/XML/1998/namespace'
 }
 
 #a dictionary of unit to user unit conversion factors
-uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}
+uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'm':3543.3070866,
+          'km':3543307.0866, 'pc':15.0, 'yd':3240 , 'ft':1080}
 def unittouu(string):
     '''Returns userunits given a string representation of units in another system'''
     unit = re.compile('(%s)$' % '|'.join(uuconv.keys()))
@@ -61,8 +63,9 @@ def uutounit(val, unit):
 
 try:
     from lxml import etree
-except:
-    sys.exit(_('The fantastic lxml wrapper for libxml2 is required by inkex.py and therefore this extension. Please download and install the latest version from http://cheeseshop.python.org/pypi/lxml/, or install it through your package manager by a command like: sudo apt-get install python-lxml'))
+except Exception, e:
+    sys.exit(_("The fantastic lxml wrapper for libxml2 is required by inkex.py and therefore this extension. Please download and install the latest version from http://cheeseshop.python.org/pypi/lxml/, or install it through your package manager by a command like: sudo apt-get install python-lxml\n\nTechnical details:\n%s" % (e,)))
 
 def debug(what):
     sys.stderr.write(str(what) + "\n")
@@ -82,7 +85,7 @@ def errormsg(msg):
          ...
          inkex.errormsg(_("This extension requires two selected paths."))
     """
-    sys.stderr.write((str(msg) + "\n").encode("UTF-8"))
+    sys.stderr.write((unicode(msg) + "\n").encode("UTF-8"))
 
 def check_inkbool(option, opt, value):
     if str(value).capitalize() == 'True':
@@ -107,7 +110,6 @@ class Effect:
     """A class for creating Inkscape SVG Effects"""
 
     def __init__(self, *args, **kwargs):
-        self.id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
         self.document=None
         self.ctx=None
         self.selected={}
@@ -132,7 +134,7 @@ class Effect:
             try:
                 stream = open(file,'r')
             except:
-                stream = open(self.args[-1],'r')
+                stream = open(self.svg_file,'r')
         except:
             stream = sys.stdin
         self.document = etree.parse(stream)
@@ -166,6 +168,21 @@ class Effect:
             for node in self.document.xpath(path, namespaces=NSS):
                 self.selected[i] = node
 
+    def getElementById(self, id):
+        path = '//*[@id="%s"]' % id
+        el_list = self.document.xpath(path, namespaces=NSS)
+        if el_list:
+          return el_list[0]
+        else:
+          return None
+
+    def getParentNode(self, node):
+        for parent in self.document.getiterator():
+            if node in parent.getchildren():
+                return parent
+                break
+
+
     def getdocids(self):
         docIdNodes = self.document.xpath('//@id', namespaces=NSS)
         for m in docIdNodes:
@@ -190,6 +207,7 @@ class Effect:
 
     def affect(self, args=sys.argv[1:], output=True):
         """Affect an SVG document with a callback effect"""
+        self.svg_file = args[-1]
         self.getoptions(args)
         self.parse()
         self.getposinlayer()
@@ -197,12 +215,12 @@ class Effect:
         self.getdocids()
         self.effect()
         if output: self.output()
-        
+
     def uniqueId(self, old_id, make_new_id = True):
         new_id = old_id
         if make_new_id:
             while new_id in self.doc_ids:
-                new_id = "%s%s" % (new_id,random.choice(self.id_characters))
+                new_id += random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
             self.doc_ids[new_id] = 1
         return new_id