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