##// END OF EJS Templates
make inkscape command configurable...
MinRK -
Show More
@@ -1,77 +1,92 b''
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 base64
16 import os
17 import os
17 import sys
18 import sys
18 import subprocess
19 import subprocess
19
20
20 from IPython.utils.tempdir import TemporaryDirectory
21 from IPython.utils.tempdir import TemporaryDirectory
21 from IPython.utils.traitlets import Unicode
22 from IPython.utils.traitlets import Unicode
22
23
23 from .convertfigures import ConvertFiguresTransformer
24 from .convertfigures import ConvertFiguresTransformer
24
25
25
26
26 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
27 # Constants
28 # Constants
28 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
29
30
30 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
31 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
31 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
32 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
32
33
33
34
34 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
35 # Classes
36 # Classes
36 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
37
38
38 class SVG2PDFTransformer(ConvertFiguresTransformer):
39 class SVG2PDFTransformer(ConvertFiguresTransformer):
39 """
40 """
40 Converts all of the outputs in a notebook from one format to another.
41 Converts all of the outputs in a notebook from SVG to PDF.
41 """
42 """
42
43
43 from_format = Unicode('svg', config=True, help='Format the converter accepts')
44 from_format = Unicode('svg', config=True, help='Format the converter accepts')
44 to_format = Unicode('pdf', config=False, help='Format the converter writes')
45 to_format = Unicode('pdf', config=False, help='Format the converter writes')
46 command = Unicode(config=True,
47 help="""The command to use for converting SVG to PDF
48
49 This string is a template, which will be formatted with the keys
50 to_filename and from_filename.
51
52 The conversion call must read the SVG from {from_flename},
53 and write a PDF to {to_filename}.
54 """)
55
56 def _command_default(self):
57 if sys.platform == "darwin":
58 return INKSCAPE_OSX_COMMAND
59 elif sys.platform == "win32":
60 # windows not yet supported
61 return ""
62 else:
63 return INKSCAPE_COMMAND
45
64
46
65
47 def convert_figure(self, data_format, data):
66 def convert_figure(self, data_format, data):
48 """
67 """
49 Convert a single Svg figure. Returns converted data.
68 Convert a single SVG figure to PDF. Returns converted data.
50 """
69 """
51
70
52 #Work in a temporary directory
71 #Work in a temporary directory
53 with TemporaryDirectory() as tmpdir:
72 with TemporaryDirectory() as tmpdir:
54
73
55 #Write fig to temp file
74 #Write fig to temp file
56 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
75 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
57 with open(input_filename, 'w') as f:
76 with open(input_filename, 'wb') as f:
58 f.write(data)
77 f.write(data)
59
78
60 #Determine command (different on Mac OSX)
61 command = INKSCAPE_COMMAND
62 if sys.platform == 'darwin':
63 command = INKSCAPE_OSX_COMMAND
64
65 #Call conversion application
79 #Call conversion application
66 output_filename = os.path.join(tmpdir, 'figure.pdf')
80 output_filename = os.path.join(tmpdir, 'figure.pdf')
67 shell = command.format(from_filename=input_filename,
81 shell = self.command.format(from_filename=input_filename,
68 to_filename=output_filename)
82 to_filename=output_filename)
69 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
83 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
70
84
71 #Read output from drive
85 #Read output from drive
86 # return value expects a filename
72 if os.path.isfile(output_filename):
87 if os.path.isfile(output_filename):
73 with open(output_filename, 'rb') as f:
88 with open(output_filename, 'rb') as f:
74 return f.read().encode("base64") #PDF is a nb supported binary
89 # PDF is a nb supported binary, data type, so base64 encode.
75 #data type, so base64 encode.
90 return base64.encodestring(f.read())
76 else:
91 else:
77 return TypeError("Inkscape svg to png conversion failed")
92 return TypeError("Inkscape svg to png conversion failed")
General Comments 0
You need to be logged in to leave comments. Login now