applied swfcombine finding patch from Ricardo Pedroso
[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             try:
61                 system_drive = os.environ['SYSTEMDRIVE']
62             except KeyError:
63                 system_drive = 'c:'
64             try:
65                 program_files = os.environ['PROGRAMFILES']
66             except KeyError:
67                 program_files = ''
68             locations.extend([
69                               opj("c:", os.sep, "swftools", prog),
70                               opj("c:", os.sep, "Program Files", "SWFTools", prog),
71                               opj(system_drive, os.sep, program_files, "SWFTools", prog),
72                               opj(basedir, prog),
73                              ])
74         else:
75             locations.extend([
76                               opj("/usr", "local", "bin", prog),
77                               opj("/usr", "bin", prog),
78                               opj(basedir, '..', 'src', prog),
79                              ])
80         #print locations
81
82         exe = prog
83         for e in locations:
84             #print e
85             if os.path.isfile(e):
86                 exe = e
87                 found = True
88                 break
89         #print exe, found
90         return exe, found
91
92     def swfcombine(self, *args):
93         try:
94             self.__swfcombine(*args)
95         except Exception, e:
96             wx.CallAfter(Publisher.sendMessage,
97                          "SWF_COMBINE_ERROR", unicode(e))
98             time.sleep(0.05)
99
100     def __swfcombine(self, *args):
101         exe, found = self.__find_swfcombine()
102         # uncoment to test a failed swfcombine find
103         #found = False
104
105         if not found:
106             raise Exception(u"Could not execute %s: %s not found" % (exe, exe))
107
108         # Create a command line
109         cmd = [exe,]
110         cmd.extend(args)
111
112         if "wxMSW" in wx.PlatformInfo:
113             try:
114                 import win32process
115                 # To avoid an ugly "DOS Window" to show up
116                 flags = win32process.CREATE_NO_WINDOW
117             except ImportError:
118                 flags = 0
119         else:
120             flags = 0
121         output = Popen(cmd, stdin=PIPE, stdout=PIPE,
122                        stderr=PIPE, creationflags=flags).communicate()[0]
123
124         # Check the process output
125         if output:
126             raise Exception(u"Error executing %s:%s%s" %
127                              (u" ".join(cmd), os.linesep, output))
128