Show More
@@ -1,102 +1,102 | |||||
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(".", config=True, |
|
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 and subdir from the resources dict. |
|
60 | # Pull the extension and subdir 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(' |
|
66 | for filename, data in resources.get('outputs', {}).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 | path = os.path.dirname(dest) |
|
70 | path = os.path.dirname(dest) | |
71 | if not os.path.isdir(path): |
|
71 | if not os.path.isdir(path): | |
72 | os.makedirs(path) |
|
72 | os.makedirs(path) | |
73 |
|
73 | |||
74 | # Write file |
|
74 | # Write file | |
75 | with io.open(dest, 'wb') as f: |
|
75 | with io.open(dest, 'wb') as f: | |
76 | f.write(data) |
|
76 | f.write(data) | |
77 |
|
77 | |||
78 | # Copy referenced files to output directory |
|
78 | # Copy referenced files to output directory | |
79 | if self.build_directory: |
|
79 | if self.build_directory: | |
80 | for filename in self.files: |
|
80 | for filename in self.files: | |
81 |
|
81 | |||
82 | # Copy files that match search pattern |
|
82 | # Copy files that match search pattern | |
83 | for matching_filename in glob.glob(filename): |
|
83 | for matching_filename in glob.glob(filename): | |
84 |
|
84 | |||
85 | # Make sure folder exists. |
|
85 | # Make sure folder exists. | |
86 | dest = os.path.join(self.build_directory, filename) |
|
86 | dest = os.path.join(self.build_directory, filename) | |
87 | path = os.path.dirname(dest) |
|
87 | path = os.path.dirname(dest) | |
88 | if not os.path.isdir(path): |
|
88 | if not os.path.isdir(path): | |
89 | os.makedirs(path) |
|
89 | os.makedirs(path) | |
90 |
|
90 | |||
91 | # Copy if destination is different. |
|
91 | # Copy if destination is different. | |
92 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): |
|
92 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): | |
93 | shutil.copyfile(matching_filename, dest) |
|
93 | shutil.copyfile(matching_filename, dest) | |
94 |
|
94 | |||
95 | # Determine where to write conversion results. |
|
95 | # Determine where to write conversion results. | |
96 | dest = notebook_name + '.' + output_extension |
|
96 | dest = notebook_name + '.' + output_extension | |
97 | if self.build_directory: |
|
97 | if self.build_directory: | |
98 | dest = os.path.join(self.build_directory, dest) |
|
98 | dest = os.path.join(self.build_directory, dest) | |
99 |
|
99 | |||
100 | # Write conversion results. |
|
100 | # Write conversion results. | |
101 | with io.open(dest, 'w') as f: |
|
101 | with io.open(dest, 'w') as f: | |
102 | f.write(output) |
|
102 | f.write(output) |
General Comments 0
You need to be logged in to leave comments.
Login now