uniconv_output.py revision 5f6d480d9cd8f7f495a567cc4212592bfecdfcfc
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira#!/usr/bin/env python
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira"""
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveirauniconv_output.py
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos OliveiraModule for running UniConverter exporting commands in Inkscape extensions
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
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 Oliveira
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 Oliveira
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 Oliveira
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"""
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
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 Oliveira# code of 1.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiraimport os
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiraimport sys
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiraimport tempfile
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiraimport gettext
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira_ = gettext.gettext
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
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 msg = None
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 try:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira try:
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 rc = p.wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira out = p.stdout.read()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira err = p.stderr.read()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except ImportError:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira try:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira from popen2 import Popen3
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen3(command, True)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p.wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira rc = p.poll()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira out = p.fromchild.read()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira err = p.childerr.read()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except ImportError:
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 if rc and msg is None:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except Exception, inst:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # If successful, copy the output file to stdout.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if msg is None:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if os.name == 'nt': # make stdout work in binary on Windows
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira import msvcrt
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira try:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira f = open(outfile, "rb")
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira data = f.read()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira sys.stdout.write(data)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira f.close()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except IOError, inst:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira msg = "Error reading temporary file: %s" % str(inst)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # Clean up.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira try:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira os.remove(outfile)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except Exception:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira pass
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # Ouput error message (if any) and exit.
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if msg is not None:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira sys.stderr.write(msg + "\n")
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira sys.exit(1)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira else:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira sys.exit(0)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveiradef get_command():
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira cmd = None
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira try:
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 if p==0 :
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira cmd = 'uniconvertor'
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira else:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen('uniconv', shell=True, stdout=PIPE, stderr=PIPE).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if p==0 :
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira cmd = 'uniconv'
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except ImportError:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira from popen2 import Popen3
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira p = Popen3('uniconv', True).wait()
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if p!=32512 : cmd = 'uniconv'
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
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira if cmd == None:
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 try:
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira # cannot simply import uniconvertor, it aborts if you import it without parameters
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira import imp
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira imp.find_module("uniconvertor")
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira except ImportError:
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 sys.exit(1)
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira cmd = 'python -c "from uniconvertor import uniconv; uniconv();"'
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira return cmd
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
89403dd20985abf3b4689cea57344290de56849cVinícius dos Santos Oliveira