Show More
@@ -1,108 +1,108 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Contains writer for writing nbconvert output to filesystem. |
|
2 | Contains writer for writing nbconvert output to filesystem. | |
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 io |
|
16 | import io | |
17 | import os |
|
17 | import os | |
18 | import glob |
|
18 | import glob | |
19 |
|
19 | |||
20 | from IPython.utils.traitlets import Unicode |
|
20 | from IPython.utils.traitlets import Unicode | |
21 | from IPython.utils.path import link_or_copy |
|
21 | from IPython.utils.path import link_or_copy | |
22 |
|
22 | |||
23 | from .base import WriterBase |
|
23 | from .base import WriterBase | |
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Classes |
|
26 | # Classes | |
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 |
|
28 | |||
29 | class FilesWriter(WriterBase): |
|
29 | class FilesWriter(WriterBase): | |
30 | """Consumes nbconvert output and produces files.""" |
|
30 | """Consumes nbconvert output and produces files.""" | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | build_directory = Unicode(".", config=True, |
|
33 | build_directory = Unicode(".", config=True, | |
34 | help="""Directory to write output to. Leave blank |
|
34 | help="""Directory to write output to. Leave blank | |
35 | to output to the current directory""") |
|
35 | to output to the current directory""") | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | # Make sure that the output directory exists. |
|
38 | # Make sure that the output directory exists. | |
39 | def _build_directory_changed(self, name, old, new): |
|
39 | def _build_directory_changed(self, name, old, new): | |
40 | if new and not os.path.isdir(new): |
|
40 | if new and not os.path.isdir(new): | |
41 | os.makedirs(new) |
|
41 | os.makedirs(new) | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | def __init__(self, **kw): |
|
44 | def __init__(self, **kw): | |
45 | super(FilesWriter, self).__init__(**kw) |
|
45 | super(FilesWriter, self).__init__(**kw) | |
46 | self._build_directory_changed('build_directory', self.build_directory, |
|
46 | self._build_directory_changed('build_directory', self.build_directory, | |
47 | self.build_directory) |
|
47 | self.build_directory) | |
48 |
|
48 | |||
49 | def _makedir(self, path): |
|
49 | def _makedir(self, path): | |
50 | """Make a directory if it doesn't already exist""" |
|
50 | """Make a directory if it doesn't already exist""" | |
51 | if not os.path.isdir(path): |
|
51 | if not os.path.isdir(path): | |
52 | self.log.info("Making directory %s", path) |
|
52 | self.log.info("Making directory %s", path) | |
53 | os.makedirs(path) |
|
53 | os.makedirs(path) | |
54 |
|
54 | |||
55 | def write(self, output, resources, notebook_name=None, **kw): |
|
55 | def write(self, output, resources, notebook_name=None, **kw): | |
56 | """ |
|
56 | """ | |
57 | Consume and write Jinja output to the file system. Output directory |
|
57 | Consume and write Jinja output to the file system. Output directory | |
58 | is set via the 'build_directory' variable of this instance (a |
|
58 | is set via the 'build_directory' variable of this instance (a | |
59 | configurable). |
|
59 | configurable). | |
60 |
|
60 | |||
61 | See base for more... |
|
61 | See base for more... | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | # Pull the extension and subdir from the resources dict. |
|
64 | # Pull the extension and subdir from the resources dict. | |
65 | output_extension = resources['output_extension'] |
|
65 | output_extension = resources['output_extension'] | |
66 |
|
66 | |||
67 | # Write all of the extracted resources to the destination directory. |
|
67 | # Write all of the extracted resources to the destination directory. | |
68 | # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG |
|
68 | # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | |
69 | # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... |
|
69 | # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |
70 | for filename, data in resources.get('outputs', {}).items(): |
|
70 | for filename, data in resources.get('outputs', {}).items(): | |
71 |
|
71 | |||
72 | # Determine where to write the file to |
|
72 | # Determine where to write the file to | |
73 | dest = os.path.join(self.build_directory, filename) |
|
73 | dest = os.path.join(self.build_directory, filename) | |
74 | path = os.path.dirname(dest) |
|
74 | path = os.path.dirname(dest) | |
75 | self._makedir(path) |
|
75 | self._makedir(path) | |
76 |
|
76 | |||
77 | # Write file |
|
77 | # Write file | |
78 | self.log.debug("Writing %i bytes to support file %s", len(data), dest) |
|
78 | self.log.debug("Writing %i bytes to support file %s", len(data), dest) | |
79 | with io.open(dest, 'wb') as f: |
|
79 | with io.open(dest, 'wb') as f: | |
80 | f.write(data) |
|
80 | f.write(data) | |
81 |
|
81 | |||
82 | # Copy referenced files to output directory |
|
82 | # Copy referenced files to output directory | |
83 | if self.build_directory: |
|
83 | if self.build_directory: | |
84 | for filename in self.files: |
|
84 | for filename in self.files: | |
85 |
|
85 | |||
86 | # Copy files that match search pattern |
|
86 | # Copy files that match search pattern | |
87 | for matching_filename in glob.glob(filename): |
|
87 | for matching_filename in glob.glob(filename): | |
88 |
|
88 | |||
89 | # Make sure folder exists. |
|
89 | # Make sure folder exists. | |
90 | dest = os.path.join(self.build_directory, filename) |
|
90 | dest = os.path.join(self.build_directory, filename) | |
91 | path = os.path.dirname(dest) |
|
91 | path = os.path.dirname(dest) | |
92 | self._makedir(path) |
|
92 | self._makedir(path) | |
93 |
|
93 | |||
94 | # Copy if destination is different. |
|
94 | # Copy if destination is different. | |
95 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): |
|
95 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): | |
96 | self.log.info("Linking %s -> %s", matching_filename, dest) |
|
96 | self.log.info("Linking %s -> %s", matching_filename, dest) | |
97 | link_or_copy(matching_filename, dest) |
|
97 | link_or_copy(matching_filename, dest) | |
98 |
|
98 | |||
99 | # Determine where to write conversion results. |
|
99 | # Determine where to write conversion results. | |
100 | dest = notebook_name + '.' + output_extension |
|
100 | dest = notebook_name + '.' + output_extension | |
101 | if self.build_directory: |
|
101 | if self.build_directory: | |
102 | dest = os.path.join(self.build_directory, dest) |
|
102 | dest = os.path.join(self.build_directory, dest) | |
103 |
|
103 | |||
104 | # Write conversion results. |
|
104 | # Write conversion results. | |
105 | self.log.info("Writing %i bytes to %s", len(output), dest) |
|
105 | self.log.info("Writing %i bytes to %s", len(output), dest) | |
106 | with io.open(dest, 'w') as f: |
|
106 | with io.open(dest, 'wb') as f: | |
107 | f.write(output) |
|
107 | f.write(output.encode('utf-8')) | |
108 | return dest No newline at end of file |
|
108 | return dest |
General Comments 0
You need to be logged in to leave comments.
Login now