Code

88c294245283617390c7bf12dd0b64114f8407b1
[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, urlparse, urllib
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 SVG_and_Media_ZIP_Output(inkex.Effect):
47     def __init__(self):
48         inkex.Effect.__init__(self)
50     def output(self):
51         out = open(self.zip_file,'r')
52         sys.stdout.write(out.read())
53         out.close()
54         self.clear_tmp()
56     def clear_tmp(self):
57         shutil.rmtree(self.tmp_dir)
60     def effect(self):
61         ttmp_orig = self.document.getroot()
63         docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
64         if docname is None: docname = self.args[-1]
66         #orig_tmpfile = sys.argv[1]
68         #create os temp dir
69         self.tmp_dir = tempfile.mkdtemp()
71         # create destination zip in same directory as the document
72         self.zip_file = self.tmp_dir + os.path.sep + docname + '.zip'
73         z = zipfile.ZipFile(self.zip_file, 'w')
75         #fixme replace whatever extention
76         docstripped = docname.replace('.zip', '')
78         #read tmpdoc and copy all images to temp dir
79         for node in self.document.xpath('//svg:image', namespaces=inkex.NSS):
80             self.collectAndZipImages(node, docname, z)
82         ##copy tmpdoc to tempdir
83         dst_file = os.path.join(self.tmp_dir, docstripped)
84         stream = open(dst_file,'w')
86         self.document.write(stream)
88         stream.close()
90         z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg') 
91         z.close()
94     def collectAndZipImages(self, node, docname, z):
95         xlink = node.get(inkex.addNS('href',u'xlink'))
96         if (xlink[:4]!='data'):
97             absref=node.get(inkex.addNS('absref','sodipodi'))
98             url=urlparse.urlparse(xlink)
99             href=urllib.unquote(url.path)
100             if os.name == 'nt' and href[0] == '/':
101                 href = href[1:]
102             if (href != None):
103                 absref=os.path.realpath(href)
105             if (os.path.isfile(absref)):
106                 shutil.copy(absref, self.tmp_dir)
107                 z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
108             elif (os.path.isfile(self.tmp_dir + os.path.sep + absref)):
109                 #TODO: please explain why this clause is necessary
110                 shutil.copy(self.tmp_dir + os.path.sep + absref, self.tmp_dir)
111                 z.write(self.tmp_dir + os.path.sep + absref.encode("latin-1"),
112                         os.path.basename(absref).encode("latin-1"))
113             else:
114                 inkex.errormsg(_('Could not locate file: %s') % absref)
116             node.set(inkex.addNS('href',u'xlink'),os.path.basename(absref))
117             node.set(inkex.addNS('absref',u'sodipodi'),os.path.basename(absref))
120 if __name__ == '__main__':   #pragma: no cover
121     e = SVG_and_Media_ZIP_Output()
122     e.affect()
125 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99