1 #!/usr/bin/env python\r
2 \r
3 """\r
4 uniconv-ext.py\r
5 Python script for running UniConvertor in Inkscape extensions\r
6 \r
7 Copyright (C) 2008 Stephen Silver\r
8 \r
9 This program is free software; you can redistribute it and/or modify\r
10 it under the terms of the GNU General Public License as published by\r
11 the Free Software Foundation; either version 2 of the License, or\r
12 (at your option) any later version.\r
13 \r
14 This program is distributed in the hope that it will be useful,\r
15 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
17 GNU General Public License for more details.\r
18 \r
19 You should have received a copy of the GNU General Public License\r
20 along with this program; if not, write to the Free Software\r
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
22 \r
23 """\r
24 \r
25 import os\r
26 import random\r
27 import subprocess\r
28 import sys\r
29 import tempfile\r
30 \r
31 # We need a filename ending in ".svg" for UniConvertor.\r
32 # This is a hack.\r
33 chars = list("0123456789abcdefghijklmnopqrstuvwxyz")\r
34 while True:\r
35 random.shuffle(chars)\r
36 svgfile = os.path.join(tempfile.gettempdir(), ''.join(chars) + '.svg')\r
37 if not os.path.exists(svgfile):\r
38 break\r
39 \r
40 # Run UniConvertor, and determine our return code.\r
41 try:\r
42 p = subprocess.Popen('uniconv "%s" "%s"' % (sys.argv[1], svgfile),\r
43 shell=True, stderr=subprocess.PIPE)\r
44 err = p.stderr.read()\r
45 rc = p.wait()\r
46 if rc:\r
47 sys.stderr.write("UniConvertor failed: %s\n" % err)\r
48 rc = 1\r
49 except Exception, inst:\r
50 sys.stderr.write("Spawn error: %s\n" % str(inst))\r
51 rc = 1\r
52 \r
53 # If successful, copy the SVG file to stdout.\r
54 if rc == 0:\r
55 try:\r
56 f = open(svgfile, "rU")\r
57 for line in f:\r
58 sys.stdout.write(line)\r
59 f.close()\r
60 except IOError, inst:\r
61 sys.stderr.write("Error reading temporary SVG file: %s\n" % str(inst))\r
62 rc = 1\r
63 \r
64 # Clean up and return.\r
65 try:\r
66 os.remove(svgfile)\r
67 except Exception:\r
68 pass\r
69 sys.exit(rc)\r