##// END OF EJS Templates
DebugWriter now reads key instead of
DebugWriter now reads key instead of

File last commit:

r11635:a5796944
r11636:262c9947
Show More
files.py
102 lines | 3.7 KiB | text/x-python | PythonLexer
Jonathan Frederic
Added writer classes
r11369 #!/usr/bin/env python
"""
Contains writer for writing nbconvert output to filesystem.
"""
#-----------------------------------------------------------------------------
#Copyright (c) 2013, the IPython Development Team.
#
#Distributed under the terms of the Modified BSD License.
#
#The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import io
import os
import shutil
import glob
from IPython.utils.traitlets import Unicode
from .base import WriterBase
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class FilesWriter(WriterBase):
"""Consumes nbconvert output and produces files."""
Jonathan Frederic
Change default build directory
r11628 build_directory = Unicode(".", config=True,
Jonathan Frederic
Added writer classes
r11369 help="""Directory to write output to. Leave blank
to output to the current directory""")
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Make sure that the output directory exists.
Jonathan Frederic
Added writer classes
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)
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...
"""
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Pull the extension and subdir from the resources dict.
Jonathan Frederic
Added writer classes
r11369 output_extension = resources['output_extension']
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Write all of the extracted resources to the destination directory.
# NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
# TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
Jonathan Frederic
FilesWriter now reads key instead of
r11635 for filename, data in resources.get('outputs', {}).items():
Jonathan Frederic
Added writer classes
r11369
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Determine where to write the file to
Jonathan Frederic
Added writer classes
r11369 dest = os.path.join(self.build_directory, filename)
Jonathan Frederic
Make sure the path exists that the figures are to be written to.
r11629 path = os.path.dirname(dest)
if not os.path.isdir(path):
os.makedirs(path)
Jonathan Frederic
Added writer classes
r11369
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Write file
Jonathan Frederic
Added writer classes
r11369 with io.open(dest, 'wb') as f:
f.write(data)
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Copy referenced files to output directory
Jonathan Frederic
Added writer classes
r11369 if self.build_directory:
for filename in self.files:
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Copy files that match search pattern
Jonathan Frederic
Added writer classes
r11369 for matching_filename in glob.glob(filename):
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Make sure folder exists.
Jonathan Frederic
Added writer classes
r11369 dest = os.path.join(self.build_directory, filename)
path = os.path.dirname(dest)
if not os.path.isdir(path):
os.makedirs(path)
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Copy if destination is different.
Jonathan Frederic
Don't move the file if it doesn't need to be moved.
r11630 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
shutil.copyfile(matching_filename, dest)
Jonathan Frederic
Added writer classes
r11369
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Determine where to write conversion results.
Jonathan Frederic
Added writer classes
r11369 dest = notebook_name + '.' + output_extension
if self.build_directory:
dest = os.path.join(self.build_directory, dest)
Jonathan Frederic
Added a space between pound and comments (@minrk request)
r11632 # Write conversion results.
Jonathan Frederic
Added writer classes
r11369 with io.open(dest, 'w') as f:
f.write(output)