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