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 self.documentDst=None
49 def parseTmp(self,file=None):
50 """Parse document in specified file or on stdin"""
51 reader = inkex.xml.dom.ext.reader.Sax2.Reader()
52 try:
53 try:
54 stream = open(file,'r')
55 except:
56 stream = open(self.args[-1],'r')
57 except:
58 stream = sys.stdin
59 self.documentDst = reader.fromStream(stream)
60 stream.close()
62 def output(self):
63 pass
65 def effect(self):
67 #get needed info from orig document
68 ctx_orig = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
70 ttmp_orig = inkex.xml.xpath.Evaluate('/svg',self.document, context=ctx_orig)
72 docbase = ttmp_orig[0].attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docbase')
73 docname = ttmp_orig[0].attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname')
75 orig_tmpfile = sys.argv[1]
77 # create destination zip in same directory as the document
78 z = zipfile.ZipFile(docbase.value + '/'+ docname.value + '.zip', 'w')
80 #create os temp dir
81 tmp_dir = tempfile.mkdtemp()
83 #fixme replace whatever extention
84 docstripped = docname.value.replace('.zip', '')
86 #read tmpdoc and copy all images to temp dir
87 for node in inkex.xml.xpath.Evaluate('//image',self.document, context=ctx_orig):
88 self.collectAndZipImages(node, tmp_dir, docname, z)
90 ##copy tmpdoc to tempdir
91 dst_file = os.path.join(tmp_dir, docstripped)
92 stream = open(dst_file,'w')
94 inkex.xml.dom.ext.Print(self.document,stream)
96 stream.close()
98 z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg')
99 z.close()
101 shutil.move(docbase.value + '/'+ docname.value + '.zip',docbase.value + '/'+ docname.value)
103 shutil.rmtree(tmp_dir)
105 def collectAndZipImages(self, node, tmp_dir, docname, z):
106 xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
107 if (xlink.value[:4]!='data'):
108 absref=node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'absref')
110 if (os.path.isfile(absref.value)):
111 shutil.copy(absref.value,tmp_dir)
112 z.write(absref.value.encode("latin-1"),os.path.basename(absref.value).encode("latin-1"))
114 elif (os.path.isfile(tmp_dir + '/' + absref.value)):
115 shutil.copy(tmp_dir + '/' + absref.value,tmp_dir)
116 z.write(tmp_dir + '/' + absref.value.encode("latin-1"),os.path.basename(absref.value).encode("latin-1"))
118 xlink.value = os.path.basename(absref.value)
119 absref.value = os.path.basename(absref.value)
122 e = MyEffect()
123 e.affect()