merged in Ricardo Pedroso's new pdf2swf gui
[swftools.git] / wx / gui / plugin.py
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # gpdf2swf.py
5 # graphical user interface for pdf2swf
6 #
7 # Part of the swftools package.
8
9 # Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org> 
10 #
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.
15 #
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.
20 #
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 */
24
25 import os
26 import time
27 import wx
28 from wx.lib.pubsub import Publisher
29 from subprocess import Popen, PIPE
30
31 class Plugin:
32     def before_render(self):
33         pass
34
35     def before_save(self, page):
36         pass
37
38     def after_save(self, page):
39         pass
40
41     def _swap_extension(self, filename, newext, flashversion=""):
42         name, ext = os.path.splitext(filename)
43         return u"%s%s.%s" % (name, flashversion, newext)
44
45     def __find_swfcombine(self):
46         found = False
47         prog = "swfcombine.exe" if "wxMSW" in wx.PlatformInfo else "swfcombine"
48         basedir = os.path.dirname(__file__)
49
50         opj = os.path.join
51         locations = [opj(basedir, prog)]
52         if "wxMSW" in wx.PlatformInfo:
53             locations.extend([
54                               opj("c:", "swftools", prog),
55                               opj("c:", "Program Files", "SWFTools", prog)
56                              ])
57         else:
58             locations.extend([
59                               opj(os.sep, "usr", "local", "bin", prog),
60                               opj(os.sep, "usr", "bin", prog),
61                              ])
62
63         exe = prog
64         for e in locations:
65             if os.path.isfile(e):
66                 exe = e
67                 found = True
68                 break
69         return exe, found
70
71     def swfcombine(self, *args):
72         try:
73             self.__swfcombine(*args)
74         except Exception, e:
75             wx.CallAfter(Publisher.sendMessage,
76                          "SWF_COMBINE_ERROR", unicode(e))
77             time.sleep(0.05)
78
79     def __swfcombine(self, *args):
80         exe, found = self.__find_swfcombine()
81         # uncoment to test a failed swfcombine find
82         #found = False
83
84         if not found:
85             raise Exception(u"Could not execute %s: %s not found" % (exe, exe))
86
87         # Create a command line
88         cmd = [exe,]
89         cmd.extend(args)
90
91         output = Popen(cmd, stdout=PIPE).communicate()[0]
92
93         # Check the process output
94         if output:
95             raise Exception(u"Error executing %s:%s%s" %
96                              (u" ".join(cmd), os.linesep, output))
97