##// END OF EJS Templates
Mac OSX support for svg converter
Jonathan Frederic -
Show More
@@ -1,72 +1,78
1 1 """Module containing a transformer that converts outputs in the notebook from
2 2 one format to another.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 import os
17 17 from IPython.utils.tempdir import TemporaryDirectory
18 18
19 19 from .convertfigures import ConvertFiguresTransformer
20 20
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Constants
24 24 #-----------------------------------------------------------------------------
25 25
26 INKSCAPE_COMMAND = "inkscape --without-gui --export-pdf=\"{to_filename}\" \"{from_filename}\""
26 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
27 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
27 28
28 29
29 30 #-----------------------------------------------------------------------------
30 31 # Classes
31 32 #-----------------------------------------------------------------------------
32 33
33 34 class ConvertSvgTransformer(ConvertFiguresTransformer):
34 35 """
35 36 Converts all of the outputs in a notebook from one format to another.
36 37 """
37 38
38 39
39 40 def __init__(self, **kw):
40 41 """
41 42 Constructor
42 43 """
43 44 super(ConvertSvgTransformer, self).__init__(['svg'], 'pdf', **kw)
44 45
45 46
46 47 def convert_figure(self, data_format, data):
47 48 """
48 49 Convert a single Svg figure. Returns converted data.
49 50 """
50 51
51 52 #Work in a temporary directory
52 53 with TemporaryDirectory() as tmpdir:
53 54
54 55 #Write fig to temp file
55 56 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
56 57 with open(input_filename, 'w') as f:
57 58 f.write(data)
58 59
60 #Determine command (different on Mac OSX)
61 command = INKSCAPE_COMMAND
62 if sys.platform == 'darwin':
63 command = INKSCAPE_OSX_COMMAND
64
59 65 #Call conversion application
60 66 output_filename = os.path.join(tmpdir, 'figure.pdf')
61 shell = INKSCAPE_COMMAND.format(from_filename=input_filename,
67 shell = command.format(from_filename=input_filename,
62 68 to_filename=output_filename)
63 69 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
64 70
65 71 #Read output from drive
66 72 if os.path.isfile(output_filename):
67 73 with open(output_filename, 'rb') as f:
68 74 return f.read().encode("base64") #PDF is a nb supported binary
69 75 #data type, so base64 encode.
70 76 else:
71 77 return TypeError("Inkscape svg to png conversion failed")
72 78 #TODO: ERROR: which notebook crashed exporter
General Comments 0
You need to be logged in to leave comments. Login now