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