X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=share%2Fextensions%2Fsvg_and_media_zip_output.py;h=341de728b94b4738d67aa6c0282ab8045abe59c0;hb=b17a9f02e386b2f328d61afc3c48d952b28dafe6;hp=74311b4315c4a97e5abd0e49ee7e74ad296c979f;hpb=77d65c763568495a6ffc7c15a81964448139f47a;p=inkscape.git diff --git a/share/extensions/svg_and_media_zip_output.py b/share/extensions/svg_and_media_zip_output.py index 74311b431..341de728b 100644 --- a/share/extensions/svg_and_media_zip_output.py +++ b/share/extensions/svg_and_media_zip_output.py @@ -23,7 +23,11 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -Version 0.3 +Version 0.4 (Nicolas Dufour, nicoduf@yahoo.fr) + fix a coding bug: now use UTF-8 to save filenames in the archive. + fix xlink href parsing (now a real URL in 0.47). + fix Win32 stdout \r\r\n bug (stdout now set to bin mode). + fix a double .svg extension bug (added svg and svgz in the docstripped extensions). TODO - fix bug: not saving existing .zip after a Collect for Output is run @@ -33,79 +37,100 @@ TODO - maybe add better extention """ -import inkex, os.path -import os +import inkex +import urlparse +import urllib +import os, os.path import string import zipfile import shutil import sys import tempfile +import gettext +_ = gettext.gettext -class MyEffect(inkex.Effect): +class SVG_and_Media_ZIP_Output(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) def output(self): - pass + out = open(self.zip_file,'rb') + if os.name == 'nt': + try: + import msvcrt + msvcrt.setmode(1, os.O_BINARY) + except: + pass + sys.stdout.write(out.read()) + out.close() + self.clear_tmp() + + def clear_tmp(self): + shutil.rmtree(self.tmp_dir) def effect(self): ttmp_orig = self.document.getroot() - docbase = ttmp_orig.get(inkex.addNS('docbase',u'sodipodi'),'') docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi')) - - orig_tmpfile = sys.argv[1] + if docname is None: docname = self.args[-1] #create os temp dir - tmp_dir = tempfile.mkdtemp() - - # create destination zip in same directory as the document - z = zipfile.ZipFile(tmp_dir + os.path.sep + docname + '.zip', 'w') + self.tmp_dir = tempfile.mkdtemp() #fixme replace whatever extention docstripped = docname.replace('.zip', '') - + docstripped = docstripped.replace('.svg', '') + docstripped = docstripped.replace('.svgz', '') + + # create destination zip in same directory as the document + self.zip_file = os.path.join(self.tmp_dir, docstripped) + '.zip' + z = zipfile.ZipFile(self.zip_file, 'w') + #read tmpdoc and copy all images to temp dir for node in self.document.xpath('//svg:image', namespaces=inkex.NSS): - self.collectAndZipImages(node, tmp_dir, docname, z) + self.collectAndZipImages(node, docname, z) ##copy tmpdoc to tempdir - dst_file = os.path.join(tmp_dir, docstripped) + dst_file = os.path.join(self.tmp_dir, docstripped) stream = open(dst_file,'w') - + self.document.write(stream) - + stream.close() - - z.write(dst_file.encode("latin-1"),docstripped.encode("latin-1")+'.svg') - z.close() - out = open(tmp_dir + os.path.sep + docname + '.zip','r') - sys.stdout.write(out.read()) - out.close() - - shutil.rmtree(tmp_dir) + z.write(dst_file,docstripped.encode("utf_8")+'.svg') - def collectAndZipImages(self, node, tmp_dir, docname, z): + z.close() + + def collectAndZipImages(self, node, docname, z): xlink = node.get(inkex.addNS('href',u'xlink')) if (xlink[:4]!='data'): absref=node.get(inkex.addNS('absref',u'sodipodi')) + url=urlparse.urlparse(xlink) + href=urllib.unquote(url.path) + if os.name == 'nt' and href[0] == '/': + href = href[1:] + if (href != None): + absref=os.path.realpath(href) + if (os.path.isfile(absref)): - shutil.copy(absref,tmp_dir) - z.write(absref.encode("latin-1"),os.path.basename(absref).encode("latin-1")) - elif (os.path.isfile(tmp_dir + os.path.sep + absref)): + shutil.copy(absref, self.tmp_dir) + z.write(absref, os.path.basename(absref).encode("utf_8")) + elif (os.path.isfile(os.path.join(self.tmp_dir, absref))): #TODO: please explain why this clause is necessary - shutil.copy(tmp_dir + os.path.sep + absref,tmp_dir) - z.write(tmp_dir + os.path.sep + absref.encode("latin-1"),os.path.basename(absref).encode("latin-1")) + shutil.copy(os.path.join(self.tmp_dir, absref), self.tmp_dir) + z.write(os.path.join(self.tmp_dir, absref), + os.path.basename(absref).encode("utf_8")) else: - inkex.debug('Could not locate file: %s' % absref) + inkex.errormsg(_('Could not locate file: %s') % absref) node.set(inkex.addNS('href',u'xlink'),os.path.basename(absref)) node.set(inkex.addNS('absref',u'sodipodi'),os.path.basename(absref)) - -e = MyEffect() -e.affect() + +if __name__ == '__main__': #pragma: no cover + e = SVG_and_Media_ZIP_Output() + e.affect() # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99