Code

Mnemonics in "Fill and stroke", "Align and distribute", and "Transform" dialogs ...
[inkscape.git] / share / extensions / svg_and_media_zip_output.py
index 67cabdf22f00b3c93932334381040ae69efd71b0..640c9ede4c1979dbc3818bd595b3243cefb31ed4 100644 (file)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#!/usr/bin/env python
 """
 svg_and_media_zip_output.py
 An extention which collects all images to the documents directory and
@@ -23,7 +23,8 @@ 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
 
-Version 0.3
+Version 0.5 (Nicolas Dufour, nicoduf@yahoo.fr)
+    Fix a bug related to special caracters in the path (LP #456248).
 
 TODO
 - fix bug: not saving existing .zip after a Collect for Output is run
@@ -31,10 +32,13 @@ TODO
      the file name is still xxx.zip. after saving again the file xxx.zip is written with a plain .svg which
      looks like a corrupt zip
 - maybe add better extention
+- consider switching to lzma in order to allow cross platform compression with no encoding problem...
 """
 
-import inkex, os.path
-import os
+import inkex
+import urlparse
+import urllib
+import os, os.path
 import string
 import zipfile
 import shutil
@@ -46,9 +50,19 @@ _ = gettext.gettext
 class SVG_and_Media_ZIP_Output(inkex.Effect):
     def __init__(self):
         inkex.Effect.__init__(self)
+        if os.name == 'nt':
+            self.encoding = "cp437"
+        else:
+            self.encoding = "latin-1"     
 
     def output(self):
-        out = open(self.zip_file,'r')
+        out = open(self.zip_file,'rb')
+        if os.name == 'nt':
+            try:
+                import msvcrt
+                msvcrt.setmode(1, os.O_BINARY)
+            except:
+                pass
         sys.stdout.write(out.read())
         out.close()
         self.clear_tmp()
@@ -56,24 +70,23 @@ class SVG_and_Media_ZIP_Output(inkex.Effect):
     def clear_tmp(self):
         shutil.rmtree(self.tmp_dir)
 
-
     def effect(self):
         ttmp_orig = self.document.getroot()
 
         docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
         if docname is None: docname = self.args[-1]
 
-        #orig_tmpfile = sys.argv[1]
-
         #create os temp dir
         self.tmp_dir = tempfile.mkdtemp()
 
-        # create destination zip in same directory as the document
-        self.zip_file = self.tmp_dir + os.path.sep + docname + '.zip'
-        z = zipfile.ZipFile(self.zip_file, 'w')
-
         #fixme replace whatever extention
         docstripped = docname.replace('.zip', '')
+        docstripped = docstripped.replace('.svg', '')
+        docstripped = docstripped.replace('.svgz', '')
+        
+        # create destination zip in same directory as the document
+        self.zip_file = os.path.join(self.tmp_dir, docstripped) + '.zip'
+        z = zipfile.ZipFile(self.zip_file, 'w')
 
         #read tmpdoc and copy all images to temp dir
         for node in self.document.xpath('//svg:image', namespaces=inkex.NSS):
@@ -87,24 +100,30 @@ class SVG_and_Media_ZIP_Output(inkex.Effect):
 
         stream.close()
 
-        z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg') 
-        z.close()
+        z.write(dst_file,docstripped.encode(self.encoding)+'.svg')
 
+        z.close()
 
     def collectAndZipImages(self, node, docname, z):
         xlink = node.get(inkex.addNS('href',u'xlink'))
         if (xlink[:4]!='data'):
-            absref = node.get(inkex.addNS('absref',u'sodipodi'))
-            if absref is None:
-              absref = node.get(inkex.addNS('href',u'xlink'))
+            absref=node.get(inkex.addNS('absref',u'sodipodi'))
+            url=urlparse.urlparse(xlink)
+            href=urllib.url2pathname(url.path)
+            
+            if (href != None):
+                absref=os.path.realpath(href)
+
+            absref=unicode(absref, "utf-8")
+
             if (os.path.isfile(absref)):
                 shutil.copy(absref, self.tmp_dir)
-                z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
-            elif (os.path.isfile(self.tmp_dir + os.path.sep + absref)):
+                z.write(absref, os.path.basename(absref).encode(self.encoding))
+            elif (os.path.isfile(os.path.join(self.tmp_dir, absref))):
                 #TODO: please explain why this clause is necessary
-                shutil.copy(self.tmp_dir + os.path.sep + absref, self.tmp_dir)
-                z.write(self.tmp_dir + os.path.sep + absref.encode("latin-1"),
-                        os.path.basename(absref).encode("latin-1"))
+                shutil.copy(os.path.join(self.tmp_dir, absref), self.tmp_dir)
+                z.write(os.path.join(self.tmp_dir, absref),
+                        os.path.basename(absref).encode(self.encoding))
             else:
                 inkex.errormsg(_('Could not locate file: %s') % absref)
 
@@ -117,4 +136,4 @@ if __name__ == '__main__':   #pragma: no cover
     e.affect()
 
 
-# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99