Code

Partial fix
[inkscape.git] / share / extensions / svg_and_media_zip_output.py
1 #!/usr/bin/env python 
2 """
3 svg_and_media_zip_output.py
4 An extention which collects all images to the documents directory and 
5 creates a zip archive containing all images and the document
7 Copyright (C) 2005 Pim Snel, pim@lingewoud.com
8 this is  the first Python script  ever created
9 its based on embedimage.py
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 Version 0.3
27 TODO
28 - fix bug: not saving existing .zip after a Collect for Output is run
29      this bug occurs because after running an effect extention the inkscape:output_extension is reset to svg.inkscape
30      the file name is still xxx.zip. after saving again the file xxx.zip is written with a plain .svg which 
31      looks like a corrupt zip
32 - maybe add better extention
33 """
35 import inkex, os.path
36 import os
37 import string
38 import zipfile
39 import shutil
40 import sys
41 import tempfile
43 class MyEffect(inkex.Effect):
44     def __init__(self):
45         inkex.Effect.__init__(self)
47     def output(self):
48         pass
50     def effect(self):
51         ttmp_orig = self.document.getroot()
52         
53         docbase = ttmp_orig.get(inkex.addNS('docbase',u'sodipodi'),'')
54         docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
56         inkex.debug((docbase,docname))
58         orig_tmpfile = sys.argv[1]
60         #create os temp dir
61         tmp_dir = tempfile.mkdtemp()
62         
63         # create destination zip in same directory as the document
64         z = zipfile.ZipFile(tmp_dir + '/'+ docname + '.zip', 'w')    
66         #fixme replace whatever extention
67         docstripped = docname.replace('.zip', '')
68     
69         #read tmpdoc and copy all images to temp dir
70         for node in self.document.xpath('//image',inkex.NSS):
71             self.collectAndZipImages(node, tmp_dir, docname, z)
73         ##copy tmpdoc to tempdir
74         dst_file = os.path.join(tmp_dir, docstripped)
75         stream = open(dst_file,'w')
76     
77         self.document.write(stream)
78         
79         stream.close()
80         
81         z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg') 
82         z.close()
84         out = open(tmp_dir + '/'+ docname + '.zip','r')
85         sys.stdout.write(out.read())
86         out.close() 
87         
88         shutil.rmtree(tmp_dir)
90     def collectAndZipImages(self, node, tmp_dir, docname, z):
91         xlink = node.get(inkex.addNS('href',u'xlink'))
92         if (xlink.value[:4]!='data'):
93             absref=node.get(inkex.addNS('absref',u'sodipodi'))
95             if (os.path.isfile(absref)):
96                 shutil.copy(absref,tmp_dir)
97                 z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
99             elif (os.path.isfile(tmp_dir + '/' + absref)):
100                 shutil.copy(tmp_dir + '/' + absref,tmp_dir)
101                 z.write(tmp_dir + '/' + absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
103             node.set(inkex.addNS('href',u'xlink'),os.path.basename(absref))
104             node.set(inkex.addNS('absref',u'sodipodi'),os.path.basename(absref))
106             
107 e = MyEffect()
108 e.affect()