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