Show More
@@ -1,78 +1,81 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 os |
|
16 | import os | |
|
17 | import sys | |||
|
18 | import subprocess | |||
|
19 | ||||
17 | from IPython.utils.tempdir import TemporaryDirectory |
|
20 | from IPython.utils.tempdir import TemporaryDirectory | |
18 |
|
21 | |||
19 | from .convertfigures import ConvertFiguresTransformer |
|
22 | from .convertfigures import ConvertFiguresTransformer | |
20 |
|
23 | |||
21 |
|
24 | |||
22 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
23 | # Constants |
|
26 | # Constants | |
24 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
25 |
|
28 | |||
26 | INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"' |
|
29 | 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}"' |
|
30 | INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"' | |
28 |
|
31 | |||
29 |
|
32 | |||
30 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
31 | # Classes |
|
34 | # Classes | |
32 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
33 |
|
36 | |||
34 | class ConvertSvgTransformer(ConvertFiguresTransformer): |
|
37 | class ConvertSvgTransformer(ConvertFiguresTransformer): | |
35 | """ |
|
38 | """ | |
36 | Converts all of the outputs in a notebook from one format to another. |
|
39 | Converts all of the outputs in a notebook from one format to another. | |
37 | """ |
|
40 | """ | |
38 |
|
41 | |||
39 |
|
42 | |||
40 | def __init__(self, **kw): |
|
43 | def __init__(self, **kw): | |
41 | """ |
|
44 | """ | |
42 | Constructor |
|
45 | Constructor | |
43 | """ |
|
46 | """ | |
44 | super(ConvertSvgTransformer, self).__init__(['svg'], 'pdf', **kw) |
|
47 | super(ConvertSvgTransformer, self).__init__(['svg'], 'pdf', **kw) | |
45 |
|
48 | |||
46 |
|
49 | |||
47 | def convert_figure(self, data_format, data): |
|
50 | def convert_figure(self, data_format, data): | |
48 | """ |
|
51 | """ | |
49 | Convert a single Svg figure. Returns converted data. |
|
52 | Convert a single Svg figure. Returns converted data. | |
50 | """ |
|
53 | """ | |
51 |
|
54 | |||
52 | #Work in a temporary directory |
|
55 | #Work in a temporary directory | |
53 | with TemporaryDirectory() as tmpdir: |
|
56 | with TemporaryDirectory() as tmpdir: | |
54 |
|
57 | |||
55 | #Write fig to temp file |
|
58 | #Write fig to temp file | |
56 | input_filename = os.path.join(tmpdir, 'figure.' + data_format) |
|
59 | input_filename = os.path.join(tmpdir, 'figure.' + data_format) | |
57 | with open(input_filename, 'w') as f: |
|
60 | with open(input_filename, 'w') as f: | |
58 | f.write(data) |
|
61 | f.write(data) | |
59 |
|
62 | |||
60 | #Determine command (different on Mac OSX) |
|
63 | #Determine command (different on Mac OSX) | |
61 | command = INKSCAPE_COMMAND |
|
64 | command = INKSCAPE_COMMAND | |
62 | if sys.platform == 'darwin': |
|
65 | if sys.platform == 'darwin': | |
63 | command = INKSCAPE_OSX_COMMAND |
|
66 | command = INKSCAPE_OSX_COMMAND | |
64 |
|
67 | |||
65 | #Call conversion application |
|
68 | #Call conversion application | |
66 | output_filename = os.path.join(tmpdir, 'figure.pdf') |
|
69 | output_filename = os.path.join(tmpdir, 'figure.pdf') | |
67 | shell = command.format(from_filename=input_filename, |
|
70 | shell = command.format(from_filename=input_filename, | |
68 | to_filename=output_filename) |
|
71 | to_filename=output_filename) | |
69 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. |
|
72 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. | |
70 |
|
73 | |||
71 | #Read output from drive |
|
74 | #Read output from drive | |
72 | if os.path.isfile(output_filename): |
|
75 | if os.path.isfile(output_filename): | |
73 | with open(output_filename, 'rb') as f: |
|
76 | with open(output_filename, 'rb') as f: | |
74 | return f.read().encode("base64") #PDF is a nb supported binary |
|
77 | return f.read().encode("base64") #PDF is a nb supported binary | |
75 | #data type, so base64 encode. |
|
78 | #data type, so base64 encode. | |
76 | else: |
|
79 | else: | |
77 | return TypeError("Inkscape svg to png conversion failed") |
|
80 | return TypeError("Inkscape svg to png conversion failed") | |
78 | #TODO: ERROR: which notebook crashed exporter |
|
81 | #TODO: ERROR: which notebook crashed exporter |
General Comments 0
You need to be logged in to leave comments.
Login now