Code

Updated the text of the JessyInk effect extension to make translation easier (bug...
[inkscape.git] / share / extensions / uniconv_output.py
1 #!/usr/bin/env python
3 """
4 uniconv_output.py
5 Module for running UniConverter exporting commands in Inkscape extensions
7 Copyright (C) 2009 Nicolas Dufour (jazzynico)
8 Based on unicon-ext.py and run_command.py by Stephen Silver
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23 """
25 # Run a command that generates an UniConvertor export file from a SVG file.
26 # On success, outputs the contents of the UniConverter convertion to stdout, 
27 # and exits with a return code of 0.
28 # On failure, outputs an error message to stderr, and exits with a return
29 # code of 1.
31 import os
32 import sys
33 import tempfile
34 import gettext
35 _ = gettext.gettext
37 def run(command_format, prog_name, uniconv_format):
38     outfile = tempfile.mktemp(uniconv_format)
39     command = command_format + outfile
40     msg = None
41     # In order to get a return code from the process, we use subprocess.Popen
42     # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
43     # (Unix only).  As the Inkscape package for Windows includes Python 2.5,
44     # this should cover all supported platforms.
45     try:
46         try:
47             from subprocess import Popen, PIPE
48             p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
49             rc = p.wait()
50             out = p.stdout.read()
51             err = p.stderr.read()
52         except ImportError:
53             try:
54                 from popen2 import Popen3
55                 p = Popen3(command, True)
56                 p.wait()
57                 rc = p.poll()
58                 out = p.fromchild.read()
59                 err = p.childerr.read()
60             except ImportError:
61                 # shouldn't happen...
62                 msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
63         if rc and msg is None:
64             msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
65     except Exception, inst:
66         msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
68     # If successful, copy the output file to stdout.
69     if msg is None:
70         if os.name == 'nt':  # make stdout work in binary on Windows
71             import msvcrt
72             msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
73         try:
74             f = open(outfile, "rb")
75             data = f.read()
76             sys.stdout.write(data)
77             f.close()
78         except IOError, inst:
79             msg = "Error reading temporary file: %s" % str(inst)
81     # Clean up.
82     try:
83         os.remove(outfile)
84     except Exception:
85         pass
87     # Ouput error message (if any) and exit.
88     if msg is not None:
89         sys.stderr.write(msg + "\n")
90         sys.exit(1)
91     else:
92         sys.exit(0)
94 def get_command():
95     cmd = None
97     try:
98         from subprocess import Popen, PIPE
99         p = Popen('uniconvertor', shell=True, stdout=PIPE, stderr=PIPE).wait()
100         if p==0 :
101             cmd = 'uniconvertor'
102         else:
103             p = Popen('uniconv', shell=True, stdout=PIPE, stderr=PIPE).wait()
104             if p==0 :
105                 cmd = 'uniconv'
106     except ImportError:
107         from popen2 import Popen3
108         p = Popen3('uniconv', True).wait()
109         if p!=32512 : cmd = 'uniconv'
110         p = Popen3('uniconvertor', True).wait()
111         if p!=32512 : cmd = 'uniconvertor'
113     if cmd == None:
114         # there's no succeffully-returning uniconv command; try to get the module directly (on Windows)
115         try:
116             # cannot simply import uniconvertor, it aborts if you import it without parameters
117             import imp
118             imp.find_module("uniconvertor")
119         except ImportError:
120             sys.stderr.write(_('You need to install the UniConvertor software.\n'+\
121                          'For GNU/Linux: install the package python-uniconvertor.\n'+\
122                          'For Windows: download it from\n'+\
123                          'http://sk1project.org/modules.php?name=Products&product=uniconvertor\n'+\
124                          'and install into your Inkscape\'s Python location\n'))
125             sys.exit(1)
126         cmd = 'python -c "import uniconvertor; uniconvertor.uniconv_run();"'
128     return cmd
130 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99