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
35 def run(command_format, prog_name, uniconv_format):
36 outfile = tempfile.mktemp(uniconv_format)
37 command = command_format + outfile
38 msg = None
39 # In order to get a return code from the process, we use subprocess.Popen
40 # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
41 # (Unix only). As the Inkscape package for Windows includes Python 2.5,
42 # this should cover all supported platforms.
43 try:
44 try:
45 from subprocess import Popen, PIPE
46 p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
47 rc = p.wait()
48 out = p.stdout.read()
49 err = p.stderr.read()
50 except ImportError:
51 try:
52 from popen2 import Popen3
53 p = Popen3(command, True)
54 p.wait()
55 rc = p.poll()
56 out = p.fromchild.read()
57 err = p.childerr.read()
58 except ImportError:
59 # shouldn't happen...
60 msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
61 if rc and msg is None:
62 msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
63 except Exception, inst:
64 msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
66 # If successful, copy the output file to stdout.
67 if msg is None:
68 if os.name == 'nt': # make stdout work in binary on Windows
69 import msvcrt
70 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
71 try:
72 f = open(outfile, "rb")
73 data = f.read()
74 sys.stdout.write(data)
75 f.close()
76 except IOError, inst:
77 msg = "Error reading temporary file: %s" % str(inst)
79 # Clean up.
80 try:
81 os.remove(outfile)
82 except Exception:
83 pass
85 # Ouput error message (if any) and exit.
86 if msg is not None:
87 sys.stderr.write(msg + "\n")
88 sys.exit(1)
89 else:
90 sys.exit(0)
92 def get_command():
93 cmd = None
95 try:
96 from subprocess import Popen, PIPE
97 p = Popen('uniconv', shell=True, stdout=PIPE, stderr=PIPE).wait()
98 if p==0 :
99 cmd = 'uniconv'
100 else:
101 p = Popen('uniconvertor', shell=True, stdout=PIPE, stderr=PIPE).wait()
102 if p==0 :
103 cmd = 'uniconvertor'
104 except ImportError:
105 from popen2 import Popen3
106 p = Popen3('uniconv', True).wait()
107 if p!=32512 : cmd = 'uniconv'
108 p = Popen3('uniconvertor', True).wait()
109 if p!=32512 : cmd = 'uniconvertor'
111 if cmd == None:
112 # there's no succeffully-returning uniconv command; try to get the module directly (on Windows)
113 try:
114 # cannot simply import uniconvertor, it aborts if you import it without parameters
115 import imp
116 imp.find_module("uniconvertor")
117 except ImportError:
118 sys.stderr.write(_('You need to install the UniConvertor software.\n'+\
119 'For GNU/Linux: install the package python-uniconvertor.\n'+\
120 'For Windows: download it from\n'+\
121 'http://sk1project.org/modules.php?name=Products&product=uniconvertor\n'+\
122 'and install into your Inkscape\'s Python location\n'))
123 sys.exit(1)
124 cmd = 'python -c "from uniconvertor import uniconv; uniconv();"'
125 return cmd
127 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99