Code

share/extension/*.py: noop: minor whitespace regularizations ahead of adding vim...
[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
44 class MyEffect(inkex.Effect):
45     def __init__(self):
46         inkex.Effect.__init__(self)
48     def output(self):
49         pass
51     def effect(self):
52         ttmp_orig = self.document.getroot()
54         docbase = ttmp_orig.get(inkex.addNS('docbase',u'sodipodi'),'')
55         docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
57         orig_tmpfile = sys.argv[1]
59         #create os temp dir
60         tmp_dir = tempfile.mkdtemp()
61         
62         # create destination zip in same directory as the document
63         z = zipfile.ZipFile(tmp_dir + os.path.sep + docname + '.zip', 'w')    
65         #fixme replace whatever extention
66         docstripped = docname.replace('.zip', '')
67     
68         #read tmpdoc and copy all images to temp dir
69         for node in self.document.xpath('//svg:image', namespaces=inkex.NSS):
70             self.collectAndZipImages(node, tmp_dir, docname, z)
72         ##copy tmpdoc to tempdir
73         dst_file = os.path.join(tmp_dir, docstripped)
74         stream = open(dst_file,'w')
75     
76         self.document.write(stream)
77         
78         stream.close()
79         
80         z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg') 
81         z.close()
83         out = open(tmp_dir + os.path.sep + docname + '.zip','r')
84         sys.stdout.write(out.read())
85         out.close() 
86         
87         shutil.rmtree(tmp_dir)
89     def collectAndZipImages(self, node, tmp_dir, docname, z):
90         xlink = node.get(inkex.addNS('href',u'xlink'))
91         if (xlink[:4]!='data'):
92             absref=node.get(inkex.addNS('absref',u'sodipodi'))
93             if (os.path.isfile(absref)):
94                 shutil.copy(absref,tmp_dir)
95                 z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
96             elif (os.path.isfile(tmp_dir + os.path.sep + absref)):
97                 #TODO: please explain why this clause is necessary
98                 shutil.copy(tmp_dir + os.path.sep + absref,tmp_dir)
99                 z.write(tmp_dir + os.path.sep + absref.encode("latin-1"),os.path.basename(absref).encode("latin-1"))
100             else:
101                 inkex.debug('Could not locate file: %s' % absref)
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()