Code

cc8f7db0e1f8f14f72554a4586310474de2e1942
[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 Copyright (C) 2008 Aaron Spike, aaron@ekips.org
9 this is  the first Python script  ever created
10 its based on embedimage.py
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 Version 0.3
28 TODO
29 - fix bug: not saving existing .zip after a Collect for Output is run
30      this bug occurs because after running an effect extention the inkscape:output_extension is reset to svg.inkscape
31      the file name is still xxx.zip. after saving again the file xxx.zip is written with a plain .svg which
32      looks like a corrupt zip
33 - maybe add better extention
34 """
36 import inkex, os.path
37 import os
38 import string
39 import zipfile
40 import shutil
41 import sys
42 import tempfile
43 import gettext
44 _ = gettext.gettext
46 class MyEffect(inkex.Effect):
47     def __init__(self):
48         inkex.Effect.__init__(self)
50     def output(self):
51         pass
53     def effect(self):
54         ttmp_orig = self.document.getroot()
56         docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
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 + os.path.sep + 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('//svg:image', namespaces=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 + os.path.sep + 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[:4]!='data'):
93             absref=node.get(inkex.addNS('absref',u'sodipodi'))
94             if (os.path.isfile(absref)):
95                 shutil.copy(absref,tmp_dir)
96                 z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
97             elif (os.path.isfile(tmp_dir + os.path.sep + absref)):
98                 #TODO: please explain why this clause is necessary
99                 shutil.copy(tmp_dir + os.path.sep + absref,tmp_dir)
100                 z.write(tmp_dir + os.path.sep + absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
101             else:
102                 inkex.errormsg(_('Could not locate file: %s') % absref)
104             node.set(inkex.addNS('href',u'xlink'),os.path.basename(absref))
105             node.set(inkex.addNS('absref',u'sodipodi'),os.path.basename(absref))
107             
108 if __name__ == '__main__':
109     e = MyEffect()
110     e.affect()
113 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99