files.py
111 lines
| 4.2 KiB
| text/x-python
|
PythonLexer
|
r16264 | """Contains writer for writing nbconvert output to filesystem.""" | ||
|
r11369 | |||
|
r16264 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
|
r11369 | |||
import io | ||||
import os | ||||
import glob | ||||
from IPython.utils.traitlets import Unicode | ||||
|
r11649 | from IPython.utils.path import link_or_copy | ||
|
r16264 | from IPython.utils.py3compat import unicode_type | ||
|
r11369 | |||
from .base import WriterBase | ||||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
class FilesWriter(WriterBase): | ||||
"""Consumes nbconvert output and produces files.""" | ||||
|
r11982 | build_directory = Unicode("", config=True, | ||
|
r11369 | help="""Directory to write output to. Leave blank | ||
to output to the current directory""") | ||||
|
r11632 | # Make sure that the output directory exists. | ||
|
r11369 | def _build_directory_changed(self, name, old, new): | ||
if new and not os.path.isdir(new): | ||||
os.makedirs(new) | ||||
def __init__(self, **kw): | ||||
super(FilesWriter, self).__init__(**kw) | ||||
self._build_directory_changed('build_directory', self.build_directory, | ||||
self.build_directory) | ||||
|
r11842 | |||
def _makedir(self, path): | ||||
"""Make a directory if it doesn't already exist""" | ||||
|
r12004 | if path and not os.path.isdir(path): | ||
|
r11842 | self.log.info("Making directory %s", path) | ||
os.makedirs(path) | ||||
|
r11369 | |||
def write(self, output, resources, notebook_name=None, **kw): | ||||
""" | ||||
Consume and write Jinja output to the file system. Output directory | ||||
is set via the 'build_directory' variable of this instance (a | ||||
configurable). | ||||
See base for more... | ||||
""" | ||||
|
r12005 | # Verify that a notebook name is provided. | ||
if notebook_name is None: | ||||
|
r12013 | raise TypeError('notebook_name') | ||
|
r12005 | |||
|
r11632 | # Pull the extension and subdir from the resources dict. | ||
|
r12006 | output_extension = resources.get('output_extension', None) | ||
|
r11369 | |||
|
r11632 | # Write all of the extracted resources to the destination directory. | ||
# NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | ||||
|
r12219 | # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | ||
|
r11635 | for filename, data in resources.get('outputs', {}).items(): | ||
|
r11369 | |||
|
r11632 | # Determine where to write the file to | ||
|
r11369 | dest = os.path.join(self.build_directory, filename) | ||
|
r11629 | path = os.path.dirname(dest) | ||
|
r11842 | self._makedir(path) | ||
|
r11369 | |||
|
r11632 | # Write file | ||
|
r11847 | self.log.debug("Writing %i bytes to support file %s", len(data), dest) | ||
|
r11369 | with io.open(dest, 'wb') as f: | ||
f.write(data) | ||||
|
r11632 | # Copy referenced files to output directory | ||
|
r11369 | if self.build_directory: | ||
for filename in self.files: | ||||
|
r11632 | # Copy files that match search pattern | ||
|
r11369 | for matching_filename in glob.glob(filename): | ||
|
r11632 | # Make sure folder exists. | ||
|
r11369 | dest = os.path.join(self.build_directory, filename) | ||
path = os.path.dirname(dest) | ||||
|
r11842 | self._makedir(path) | ||
|
r11369 | |||
|
r11632 | # Copy if destination is different. | ||
|
r11630 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): | ||
|
r11842 | self.log.info("Linking %s -> %s", matching_filename, dest) | ||
|
r11649 | link_or_copy(matching_filename, dest) | ||
|
r11369 | |||
|
r11632 | # Determine where to write conversion results. | ||
|
r12006 | if output_extension is not None: | ||
dest = notebook_name + '.' + output_extension | ||||
else: | ||||
dest = notebook_name | ||||
|
r11369 | if self.build_directory: | ||
dest = os.path.join(self.build_directory, dest) | ||||
|
r11632 | # Write conversion results. | ||
|
r11853 | self.log.info("Writing %i bytes to %s", len(output), dest) | ||
|
r16264 | if isinstance(output, unicode_type): | ||
with io.open(dest, 'w', encoding='utf-8') as f: | ||||
f.write(output) | ||||
else: | ||||
with io.open(dest, 'wb') as f: | ||||
f.write(output) | ||||
|
r12219 | return dest | ||