uniconv_output.py revision 5f6d480d9cd8f7f495a567cc4212592bfecdfcfc
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraModule for running UniConverter exporting commands in Inkscape extensions
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraCopyright (C) 2009 Nicolas Dufour (jazzynico)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraBased on unicon-ext.py and run_command.py by Stephen Silver
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraThis program is free software; you can redistribute it and/or modify
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveirait under the terms of the GNU General Public License as published by
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveirathe Free Software Foundation; either version 2 of the License, or
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira(at your option) any later version.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraThis program is distributed in the hope that it will be useful,
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveirabut WITHOUT ANY WARRANTY; without even the implied warranty of
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraGNU General Public License for more details.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraYou should have received a copy of the GNU General Public License
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiraalong with this program; if not, write to the Free Software
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# Run a command that generates an UniConvertor export file from a SVG file.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# On success, outputs the contents of the UniConverter convertion to stdout,
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# and exits with a return code of 0.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# On failure, outputs an error message to stderr, and exits with a return
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiradef run(command_format, prog_name, uniconv_format):
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira outfile = tempfile.mktemp(uniconv_format)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira command = command_format + outfile
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # In order to get a return code from the process, we use subprocess.Popen
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # (Unix only). As the Inkscape package for Windows includes Python 2.5,
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # this should cover all supported platforms.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira from subprocess import Popen, PIPE
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # shouldn't happen...
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # If successful, copy the output file to stdout.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if os.name == 'nt': # make stdout work in binary on Windows
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "Error reading temporary file: %s" % str(inst)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # Ouput error message (if any) and exit.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira from subprocess import Popen, PIPE
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen('uniconvertor', shell=True, stdout=PIPE, stderr=PIPE).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen('uniconv', shell=True, stdout=PIPE, stderr=PIPE).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen3('uniconv', True).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen3('uniconvertor', True).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if p!=32512 : cmd = 'uniconvertor'
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # there's no succeffully-returning uniconv command; try to get the module directly (on Windows)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # cannot simply import uniconvertor, it aborts if you import it without parameters
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira imp.find_module("uniconvertor")
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira sys.stderr.write(_('You need to install the UniConvertor software.\n'+\
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira 'For GNU/Linux: install the package python-uniconvertor.\n'+\
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira 'For Windows: download it from\n'+\
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira 'http://sk1project.org/modules.php?name=Products&product=uniconvertor\n'+\
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira 'and install into your Inkscape\'s Python location\n'))
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira cmd = 'python -c "from uniconvertor import uniconv; uniconv();"'
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99