summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f3486ce)
raw | patch | inline | side by side (parent: f3486ce)
author | sasilver <sasilver@users.sourceforge.net> | |
Sat, 14 Jun 2008 15:08:05 +0000 (15:08 +0000) | ||
committer | sasilver <sasilver@users.sourceforge.net> | |
Sat, 14 Jun 2008 15:08:05 +0000 (15:08 +0000) |
share/extensions/run_command.py | patch | blob | history |
index 0919d767d7de4fb06271758c469ebbd724ca7a66..e1b51b3b020d3a1241123e6ca2a91ab97853e646 100644 (file)
-import os\r
-import sys\r
-import tempfile\r
-\r
-"""\r
-run_command.py\r
-Module for running SVG-generating commands in Inkscape extensions\r
-\r
-Copyright (C) 2008 Stephen Silver\r
-\r
-This program is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-This program is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with this program; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
-"""\r
-\r
-# Run a command that generates an SVG file from an input file.\r
-# On success, outputs the contents of the SVG file to stdout, and exits\r
-# with a return code of 0.\r
-# On failure, outputs an error message to stderr, and exits with a return\r
-# code of 1.\r
-def run(command_format, prog_name):\r
- svgfile = tempfile.mktemp(".svg")\r
- command = command_format % svgfile\r
- msg = None\r
- # In order to get a return code from the process, we use subprocess.Popen\r
- # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3\r
- # (Unix only). As the Inkscape package for Windows includes Python 2.5,\r
- # this should cover all supported platforms.\r
- try:\r
- try:\r
- from subprocess import Popen, PIPE\r
- p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\r
- rc = p.wait()\r
- out = p.stdout.read()\r
- err = p.stderr.read()\r
- except ImportError:\r
- try:\r
- from popen2 import Popen3\r
- p = Popen3(command, True)\r
- p.wait()\r
- rc = p.poll()\r
- out = p.fromchild.read()\r
- err = p.childerr.read()\r
- except ImportError:\r
- # shouldn't happen...\r
- msg = "Neither subprocess.Popen nor popen2.Popen3 is available"\r
- if rc and msg is None:\r
- msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)\r
- except Exception, inst:\r
- msg = "Error attempting to run %s: %s" % (prog_name, str(inst))\r
-\r
- # If successful, copy the SVG file to stdout.\r
- if msg is None:\r
- try:\r
- f = open(svgfile, "rb")\r
+import os
+import sys
+import tempfile
+
+"""
+run_command.py
+Module for running SVG-generating commands in Inkscape extensions
+
+Copyright (C) 2008 Stephen Silver
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+"""
+
+# Run a command that generates an SVG file from an input file.
+# On success, outputs the contents of the SVG file to stdout, and exits
+# with a return code of 0.
+# On failure, outputs an error message to stderr, and exits with a return
+# code of 1.
+def run(command_format, prog_name):
+ svgfile = tempfile.mktemp(".svg")
+ command = command_format % svgfile
+ msg = None
+ # In order to get a return code from the process, we use subprocess.Popen
+ # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
+ # (Unix only). As the Inkscape package for Windows includes Python 2.5,
+ # this should cover all supported platforms.
+ try:
+ try:
+ from subprocess import Popen, PIPE
+ p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
+ rc = p.wait()
+ out = p.stdout.read()
+ err = p.stderr.read()
+ except ImportError:
+ try:
+ from popen2 import Popen3
+ p = Popen3(command, True)
+ p.wait()
+ rc = p.poll()
+ out = p.fromchild.read()
+ err = p.childerr.read()
+ except ImportError:
+ # shouldn't happen...
+ msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
+ if rc and msg is None:
+ msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
+ except Exception, inst:
+ msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
+
+ # If successful, copy the SVG file to stdout.
+ if msg is None:
+ if os.name == 'nt': # make stdout work in binary on Windows
+ import msvcrt
+ msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+ try:
+ f = open(svgfile, "rb")
data = f.read()
- sys.stdout.write(data)\r
- f.close()\r
- except IOError, inst:\r
- msg = "Error reading temporary SVG file: %s" % str(inst)\r
-\r
- # Clean up.\r
+ sys.stdout.write(data)
+ f.close()
+ except IOError, inst:
+ msg = "Error reading temporary SVG file: %s" % str(inst)
+
+ # Clean up.
try:
- os.remove(svgfile)\r
- except Exception:\r
- pass\r
-\r
- # Ouput error message (if any) and exit.\r
- if msg is not None:\r
- sys.stderr.write(msg + "\n")\r
- sys.exit(1)\r
- else:\r
- sys.exit(0)\r
-\r
-\r
-# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99\r
+ os.remove(svgfile)
+ except Exception:
+ pass
+
+ # Ouput error message (if any) and exit.
+ if msg is not None:
+ sys.stderr.write(msg + "\n")
+ sys.exit(1)
+ else:
+ sys.exit(0)
+
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99