Show More
@@ -1,98 +1,98 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """ |
|
2 | """ | |
3 | Contains writer for writing nbconvert output to filesystem. |
|
3 | Contains writer for writing nbconvert output to filesystem. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | #Copyright (c) 2013, the IPython Development Team. |
|
6 | #Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | #Distributed under the terms of the Modified BSD License. |
|
8 | #Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | #The full license is in the file COPYING.txt, distributed with this software. |
|
10 | #The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import io |
|
17 | import io | |
18 | import os |
|
18 | import os | |
19 | import shutil |
|
19 | import shutil | |
20 | import glob |
|
20 | import glob | |
21 |
|
21 | |||
22 | from IPython.utils.traitlets import Unicode |
|
22 | from IPython.utils.traitlets import Unicode | |
23 |
|
23 | |||
24 | from .base import WriterBase |
|
24 | from .base import WriterBase | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Classes |
|
27 | # Classes | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | class FilesWriter(WriterBase): |
|
30 | class FilesWriter(WriterBase): | |
31 | """Consumes nbconvert output and produces files.""" |
|
31 | """Consumes nbconvert output and produces files.""" | |
32 |
|
32 | |||
33 |
|
33 | |||
34 |
build_directory = Unicode(" |
|
34 | build_directory = Unicode(".", config=True, | |
35 | help="""Directory to write output to. Leave blank |
|
35 | help="""Directory to write output to. Leave blank | |
36 | to output to the current directory""") |
|
36 | to output to the current directory""") | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | #Make sure that the output directory exists. |
|
39 | #Make sure that the output directory exists. | |
40 | def _build_directory_changed(self, name, old, new): |
|
40 | def _build_directory_changed(self, name, old, new): | |
41 | if new and not os.path.isdir(new): |
|
41 | if new and not os.path.isdir(new): | |
42 | os.makedirs(new) |
|
42 | os.makedirs(new) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def __init__(self, **kw): |
|
45 | def __init__(self, **kw): | |
46 | super(FilesWriter, self).__init__(**kw) |
|
46 | super(FilesWriter, self).__init__(**kw) | |
47 | self._build_directory_changed('build_directory', self.build_directory, |
|
47 | self._build_directory_changed('build_directory', self.build_directory, | |
48 | self.build_directory) |
|
48 | self.build_directory) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def write(self, output, resources, notebook_name=None, **kw): |
|
51 | def write(self, output, resources, notebook_name=None, **kw): | |
52 | """ |
|
52 | """ | |
53 | Consume and write Jinja output to the file system. Output directory |
|
53 | Consume and write Jinja output to the file system. Output directory | |
54 | is set via the 'build_directory' variable of this instance (a |
|
54 | is set via the 'build_directory' variable of this instance (a | |
55 | configurable). |
|
55 | configurable). | |
56 |
|
56 | |||
57 | See base for more... |
|
57 | See base for more... | |
58 | """ |
|
58 | """ | |
59 |
|
59 | |||
60 | #Pull the extension from the resources dict. |
|
60 | #Pull the extension from the resources dict. | |
61 | output_extension = resources['output_extension'] |
|
61 | output_extension = resources['output_extension'] | |
62 |
|
62 | |||
63 | #Write all of the extracted resources to the destination directory. |
|
63 | #Write all of the extracted resources to the destination directory. | |
64 | #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG |
|
64 | #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | |
65 | #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... |
|
65 | #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |
66 | for filename, data in resources.get('figures', {}).items(): |
|
66 | for filename, data in resources.get('figures', {}).items(): | |
67 |
|
67 | |||
68 | #Determine where to write the file to |
|
68 | #Determine where to write the file to | |
69 | dest = os.path.join(self.build_directory, filename) |
|
69 | dest = os.path.join(self.build_directory, filename) | |
70 |
|
70 | |||
71 | #Write file |
|
71 | #Write file | |
72 | with io.open(dest, 'wb') as f: |
|
72 | with io.open(dest, 'wb') as f: | |
73 | f.write(data) |
|
73 | f.write(data) | |
74 |
|
74 | |||
75 | #Copy referenced files to output directory |
|
75 | #Copy referenced files to output directory | |
76 | if self.build_directory: |
|
76 | if self.build_directory: | |
77 | for filename in self.files: |
|
77 | for filename in self.files: | |
78 |
|
78 | |||
79 | #Copy files that match search pattern |
|
79 | #Copy files that match search pattern | |
80 | for matching_filename in glob.glob(filename): |
|
80 | for matching_filename in glob.glob(filename): | |
81 |
|
81 | |||
82 | #Make sure folder exists. |
|
82 | #Make sure folder exists. | |
83 | dest = os.path.join(self.build_directory, filename) |
|
83 | dest = os.path.join(self.build_directory, filename) | |
84 | path = os.path.dirname(dest) |
|
84 | path = os.path.dirname(dest) | |
85 | if not os.path.isdir(path): |
|
85 | if not os.path.isdir(path): | |
86 | os.makedirs(path) |
|
86 | os.makedirs(path) | |
87 |
|
87 | |||
88 | #Copy |
|
88 | #Copy | |
89 | shutil.copyfile(matching_filename, dest) |
|
89 | shutil.copyfile(matching_filename, dest) | |
90 |
|
90 | |||
91 | #Determine where to write conversion results. |
|
91 | #Determine where to write conversion results. | |
92 | dest = notebook_name + '.' + output_extension |
|
92 | dest = notebook_name + '.' + output_extension | |
93 | if self.build_directory: |
|
93 | if self.build_directory: | |
94 | dest = os.path.join(self.build_directory, dest) |
|
94 | dest = os.path.join(self.build_directory, dest) | |
95 |
|
95 | |||
96 | #Write conversion results. |
|
96 | #Write conversion results. | |
97 | with io.open(dest, 'w') as f: |
|
97 | with io.open(dest, 'w') as f: | |
98 | f.write(output) |
|
98 | f.write(output) |
General Comments 0
You need to be logged in to leave comments.
Login now