From: aurium Date: Tue, 14 Apr 2009 21:52:36 +0000 (+0000) Subject: more modularized and testable svg_and_media_zip_output extension X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=79403357976b86c4213768a20ffc6228404e23eb;p=inkscape.git more modularized and testable svg_and_media_zip_output extension --- diff --git a/share/extensions/svg_and_media_zip_output.py b/share/extensions/svg_and_media_zip_output.py index 65c230672..67cabdf22 100644 --- a/share/extensions/svg_and_media_zip_output.py +++ b/share/extensions/svg_and_media_zip_output.py @@ -43,12 +43,19 @@ 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,'r') + 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() @@ -59,20 +66,21 @@ class MyEffect(inkex.Effect): #orig_tmpfile = sys.argv[1] #create os temp dir - tmp_dir = tempfile.mkdtemp() + self.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.zip_file = self.tmp_dir + os.path.sep + docname + '.zip' + z = zipfile.ZipFile(self.zip_file, 'w') #fixme replace whatever extention docstripped = docname.replace('.zip', '') #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) @@ -82,25 +90,21 @@ class MyEffect(inkex.Effect): 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) - def collectAndZipImages(self, node, tmp_dir, docname, z): + 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')) if absref is None: absref = node.get(inkex.addNS('href',u'xlink')) if (os.path.isfile(absref)): - shutil.copy(absref,tmp_dir) + shutil.copy(absref, self.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)): + elif (os.path.isfile(self.tmp_dir + os.path.sep + 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(self.tmp_dir + os.path.sep + absref, self.tmp_dir) + z.write(self.tmp_dir + os.path.sep + absref.encode("latin-1"), + os.path.basename(absref).encode("latin-1")) else: inkex.errormsg(_('Could not locate file: %s') % absref) @@ -109,7 +113,7 @@ class MyEffect(inkex.Effect): if __name__ == '__main__': #pragma: no cover - e = MyEffect() + e = SVG_and_Media_ZIP_Output() e.affect() diff --git a/share/extensions/test/svg_and_media_zip_output.test.py b/share/extensions/test/svg_and_media_zip_output.test.py index 4e077edd3..42610f0af 100755 --- a/share/extensions/test/svg_and_media_zip_output.test.py +++ b/share/extensions/test/svg_and_media_zip_output.test.py @@ -12,15 +12,16 @@ sys.path.append('..') # this line allows to import the extension code import unittest from svg_and_media_zip_output import * -class MyEffectBasicTest(unittest.TestCase): +class SVG_and_Media_ZIP_Output_BasicTest(unittest.TestCase): #def setUp(self): def test_run_without_parameters(self): args = [ 'minimal-blank.svg' ] - e = MyEffect() + e = SVG_and_Media_ZIP_Output() e.affect( args, False ) #self.assertEqual( e.something, 'some value', 'A commentary about that.' ) + e.clear_tmp() if __name__ == '__main__': unittest.main()