Code

Win32 fix: set stdout to binary mode before writing a file to it
[inkscape.git] / share / extensions / inkex.py
index d5c1ce2136fd630ca417fd43f8e457122d95c2f2..c190fdde031918d62c33f6e55efcc6f792aed1da 100755 (executable)
@@ -20,6 +20,8 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 """
 import sys, copy, optparse, random, re
+import gettext
+_ = gettext.gettext
 
 #a dictionary of all of the xmlns prefixes in a standard inkscape doc
 NSS = {
@@ -56,12 +58,29 @@ def unittouu(string):
 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')
+    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'))
 
 def debug(what):
     sys.stderr.write(str(what) + "\n")
     return what
 
+def errormsg(msg):
+    """Intended for end-user-visible error messages.
+    
+       (Currently just writes to stderr with an appended newline, but could do
+       something better in future: e.g. could add markup to distinguish error
+       messages from status messages or debugging output.)
+      
+       Note that this should always be combined with translation:
+
+         import gettext
+         _ = gettext.gettext
+         ...
+         inkex.errormsg(_("This extension requires two selected paths."))
+    """
+    sys.stderr.write(str(msg) + "\n")
+    return what
+
 def check_inkbool(option, opt, value):
     if str(value).capitalize() == 'True':
         return True
@@ -116,15 +135,15 @@ class Effect:
         self.current_layer = self.document.getroot()
         self.view_center = (0.0,0.0)
 
-        layerattr = self.document.xpath('//sodipodi:namedview/@inkscape:current-layer', NSS)
+        layerattr = self.document.xpath('//sodipodi:namedview/@inkscape:current-layer', namespaces=NSS)
         if layerattr:
             layername = layerattr[0]
-            layer = self.document.xpath('//g[@id="%s"]' % layername, NSS)
+            layer = self.document.xpath('//svg:g[@id="%s"]' % layername, namespaces=NSS)
             if layer:
                 self.current_layer = layer[0]
 
-        xattr = self.document.xpath('//sodipodi:namedview/@inkscape:cx', NSS)
-        yattr = self.document.xpath('//sodipodi:namedview/@inkscape:cy', NSS)
+        xattr = self.document.xpath('//sodipodi:namedview/@inkscape:cx', namespaces=NSS)
+        yattr = self.document.xpath('//sodipodi:namedview/@inkscape:cy', namespaces=NSS)
         doc_height = unittouu(self.document.getroot().get('height'))
         if xattr and yattr:
             x = xattr[0]
@@ -135,10 +154,10 @@ class Effect:
         """Collect selected nodes"""
         for id in self.options.ids:
             path = '//*[@id="%s"]' % id
-            for node in self.document.xpath(path, NSS):
+            for node in self.document.xpath(path, namespaces=NSS):
                 self.selected[id] = node
     def getdocids(self):
-        docIdNodes = self.document.xpath('//@id', NSS)
+        docIdNodes = self.document.xpath('//@id', namespaces=NSS)
         for m in docIdNodes:
             self.doc_ids[m] = 1
     def output(self):
@@ -163,9 +182,11 @@ class Effect:
         return new_id
     def xpathSingle(self, path):
         try:
-            retval = self.document.xpath(path, NSS)[0]
+            retval = self.document.xpath(path, namespaces=NSS)[0]
         except:
-            debug("No matching node for expression: %s" % path)
+            errormsg(_("No matching node for expression: %s") % path)
             retval = None
         return retval
             
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99