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