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'))
57 if docname is None: docname = self.args[-1]
59 #orig_tmpfile = sys.argv[1]
61 #create os temp dir
62 tmp_dir = tempfile.mkdtemp()
64 # create destination zip in same directory as the document
65 z = zipfile.ZipFile(tmp_dir + os.path.sep + docname + '.zip', 'w')
67 #fixme replace whatever extention
68 docstripped = docname.replace('.zip', '')
70 #read tmpdoc and copy all images to temp dir
71 for node in self.document.xpath('//svg:image', namespaces=inkex.NSS):
72 self.collectAndZipImages(node, tmp_dir, docname, z)
74 ##copy tmpdoc to tempdir
75 dst_file = os.path.join(tmp_dir, docstripped)
76 stream = open(dst_file,'w')
78 self.document.write(stream)
80 stream.close()
82 z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg')
83 z.close()
85 out = open(tmp_dir + os.path.sep + docname + '.zip','r')
86 sys.stdout.write(out.read())
87 out.close()
89 shutil.rmtree(tmp_dir)
91 def collectAndZipImages(self, node, tmp_dir, docname, z):
92 xlink = node.get(inkex.addNS('href',u'xlink'))
93 if (xlink[:4]!='data'):
94 absref = node.get(inkex.addNS('absref',u'sodipodi'))
95 if absref is None:
96 absref = node.get(inkex.addNS('href',u'xlink'))
97 if (os.path.isfile(absref)):
98 shutil.copy(absref,tmp_dir)
99 z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
100 elif (os.path.isfile(tmp_dir + os.path.sep + absref)):
101 #TODO: please explain why this clause is necessary
102 shutil.copy(tmp_dir + os.path.sep + absref,tmp_dir)
103 z.write(tmp_dir + os.path.sep + absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
104 else:
105 inkex.errormsg(_('Could not locate file: %s') % absref)
107 node.set(inkex.addNS('href',u'xlink'),os.path.basename(absref))
108 node.set(inkex.addNS('absref',u'sodipodi'),os.path.basename(absref))
111 if __name__ == '__main__': #pragma: no cover
112 e = MyEffect()
113 e.affect()
116 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99