f58cc50fc577c8e17a1380b942ce3ea57c7b61cd
[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     one_page_per_file = False
33
34     def before_render(self):
35         pass
36
37     def before_save(self, page):
38         pass
39
40     def after_save(self, page):
41         pass
42
43     def _swap_extension(self, filename, newext, flashversion=""):
44         name, ext = os.path.splitext(filename)
45         return u"%s%s.%s" % (name, flashversion, newext)
46
47     def __find_swfcombine(self):
48         found = False
49         if "wxMSW" in wx.PlatformInfo:
50             prog = "swfcombine.exe"
51         else:
52             prog = "swfcombine"
53         #basedir = os.path.dirname(__file__)
54         basedir = GPDF2SWF_BASEDIR
55         #print 'basedir', basedir
56
57         opj = os.path.join
58         locations = [os.path.normpath(opj(basedir, '..', prog))]
59         if "wxMSW" in wx.PlatformInfo:
60             locations.extend([
61                               opj("c:", "swftools", prog),
62                               opj("c:", "Program Files", "SWFTools", prog)
63                              ])
64         else:
65             locations.extend([
66                               opj("/usr", "local", "bin", prog),
67                               opj("/usr", "bin", prog),
68                               opj(basedir, '..', 'src', prog),
69                              ])
70         #print locations
71
72         exe = prog
73         for e in locations:
74             #print e
75             if os.path.isfile(e):
76                 exe = e
77                 found = True
78                 break
79         #print exe, found
80         return exe, found
81
82     def swfcombine(self, *args):
83         try:
84             self.__swfcombine(*args)
85         except Exception, e:
86             wx.CallAfter(Publisher.sendMessage,
87                          "SWF_COMBINE_ERROR", unicode(e))
88             time.sleep(0.05)
89
90     def __swfcombine(self, *args):
91         exe, found = self.__find_swfcombine()
92         # uncoment to test a failed swfcombine find
93         #found = False
94
95         if not found:
96             raise Exception(u"Could not execute %s: %s not found" % (exe, exe))
97
98         # Create a command line
99         cmd = [exe,]
100         cmd.extend(args)
101
102         if "wxMSW" in wx.PlatformInfo:
103             try:
104                 import win32process
105                 # To avoid an ugly "DOS Window" to show up
106                 flags = win32process.CREATE_NO_WINDOW
107             except ImportError:
108                 flags = 0
109         else:
110             flags = 0
111         output = Popen(cmd, stdin=PIPE, stdout=PIPE,
112                        stderr=PIPE, creationflags=flags).communicate()[0]
113
114         # Check the process output
115         if output:
116             raise Exception(u"Error executing %s:%s%s" %
117                              (u" ".join(cmd), os.linesep, output))
118