##// END OF EJS Templates
Break up convert_notebooks so that it is easier to subclass
Jessica B. Hamrick -
Show More
@@ -277,61 +277,128 b' class NbConvertApp(BaseIPythonApplication):'
277 super(NbConvertApp, self).start()
277 super(NbConvertApp, self).start()
278 self.convert_notebooks()
278 self.convert_notebooks()
279
279
280 def init_single_notebook_resources(self, notebook_filename):
281 """Step 1: Initialize resources
282
283 This intializes the resources dictionary for a single notebook. This
284 method should return the resources dictionary, and MUST include the
285 following keys:
286
287 - profile_dir: the location of the profile directory
288 - unique_key: the notebook name
289 - output_files_dir: a directory where output files (not including
290 the notebook itself) should be saved
291
292 """
293
294 # Get a unique key for the notebook and set it in the resources object.
295 basename = os.path.basename(notebook_filename)
296 notebook_name = basename[:basename.rfind('.')]
297 if self.output_base:
298 # strip duplicate extension from output_base, to avoid Basname.ext.ext
299 if getattr(self.exporter, 'file_extension', False):
300 base, ext = os.path.splitext(self.output_base)
301 if ext == self.exporter.file_extension:
302 self.output_base = base
303 notebook_name = self.output_base
304
305 self.log.debug("Notebook name is '%s'", notebook_name)
306
307 # first initialize the resources we want to use
308 resources = {}
309 resources['profile_dir'] = self.profile_dir.location
310 resources['unique_key'] = notebook_name
311 resources['output_files_dir'] = '%s_files' % notebook_name
312
313 return resources
314
315 def export_single_notebook(self, notebook_filename, resources):
316 """Step 2: Export the notebook
317
318 Exports the notebook to a particular format according to the specified
319 exporter. This function returns the output and (possibly modified)
320 resources from the exporter.
321
322 """
323 try:
324 output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
325 except ConversionException:
326 self.log.error("Error while converting '%s'", notebook_filename, exc_info=True)
327 self.exit(1)
328
329 return output, resources
330
331 def write_single_notebook(self, output, resources):
332 """Step 3: Write the notebook to file
333
334 This writes output from the exporter to file using the specified writer.
335 It returns the results from the writer.
336
337 """
338 if 'unique_key' not in resources:
339 raise KeyError("unique_key MUST be specified in the resources, but it is not")
340
341 notebook_name = resources['unique_key']
342 if self.use_output_suffix and not self.output_base:
343 notebook_name += resources.get('output_suffix', '')
344
345 write_results = self.writer.write(
346 output, resources, notebook_name=notebook_name)
347 return write_results
348
349 def postprocess_single_notebook(self, write_results):
350 """Step 4: Postprocess the notebook
351
352 This postprocesses the notebook after it has been written, taking as an
353 argument the results of writing the notebook to file. This only actually
354 does anything if a postprocessor has actually been specified.
355
356 """
357 # Post-process if post processor has been defined.
358 if hasattr(self, 'postprocessor') and self.postprocessor:
359 self.postprocessor(write_results)
360
361 def convert_single_notebook(self, notebook_filename):
362 """Convert a single notebook. Performs the following steps:
363
364 1. Initialize notebook resources
365 2. Export the notebook to a particular format
366 3. Write the exported notebook to file
367 4. (Maybe) postprocess the written file
368
369 """
370 self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format)
371 resources = self.init_single_notebook_resources(notebook_filename)
372 output, resources = self.export_single_notebook(notebook_filename, resources)
373 write_results = self.write_single_notebook(output, resources)
374 self.postprocess_single_notebook(write_results)
375
280 def convert_notebooks(self):
376 def convert_notebooks(self):
281 """
377 """
282 Convert the notebooks in the self.notebook traitlet
378 Convert the notebooks in the self.notebook traitlet
283 """
379 """
284 # Export each notebook
380 # check that the output base isn't specified if there is more than
285 conversion_success = 0
381 # one notebook to convert
286
287 if self.output_base != '' and len(self.notebooks) > 1:
382 if self.output_base != '' and len(self.notebooks) > 1:
288 self.log.error(
383 self.log.error(
289 """UsageError: --output flag or `NbConvertApp.output_base` config option
384 """
290 cannot be used when converting multiple notebooks.
385 UsageError: --output flag or `NbConvertApp.output_base` config option
291 """)
386 cannot be used when converting multiple notebooks.
387 """
388 )
292 self.exit(1)
389 self.exit(1)
293
390
294 exporter = exporter_map[self.export_format](config=self.config)
391 # initialize the exporter
392 self.exporter = exporter_map[self.export_format](config=self.config)
295
393
296 for notebook_filename in self.notebooks:
394 # no notebooks to convert!
297 self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format)
395 if len(self.notebooks) == 0:
298
299 # Get a unique key for the notebook and set it in the resources object.
300 basename = os.path.basename(notebook_filename)
301 notebook_name = basename[:basename.rfind('.')]
302 if self.output_base:
303 # strip duplicate extension from output_base, to avoid Basname.ext.ext
304 if getattr(exporter, 'file_extension', False):
305 base, ext = os.path.splitext(self.output_base)
306 if ext == exporter.file_extension:
307 self.output_base = base
308 notebook_name = self.output_base
309 resources = {}
310 resources['profile_dir'] = self.profile_dir.location
311 resources['unique_key'] = notebook_name
312 resources['output_files_dir'] = '%s_files' % notebook_name
313
314 # Try to export
315 try:
316 output, resources = exporter.from_filename(notebook_filename, resources=resources)
317 except ConversionException as e:
318 self.log.error("Error while converting '%s'", notebook_filename,
319 exc_info=True)
320 self.exit(1)
321 else:
322 if self.use_output_suffix and 'output_suffix' in resources and not self.output_base:
323 notebook_name += resources['output_suffix']
324 write_results = self.writer.write(output, resources, notebook_name=notebook_name)
325
326 #Post-process if post processor has been defined.
327 if hasattr(self, 'postprocessor') and self.postprocessor:
328 self.postprocessor(write_results)
329 conversion_success += 1
330
331 # If nothing was converted successfully, help the user.
332 if conversion_success == 0:
333 self.print_help()
396 self.print_help()
334 sys.exit(-1)
397 sys.exit(-1)
398
399 # convert each notebook
400 for notebook_filename in self.notebooks:
401 self.convert_single_notebook(notebook_filename)
335
402
336 #-----------------------------------------------------------------------------
403 #-----------------------------------------------------------------------------
337 # Main entry point
404 # Main entry point
General Comments 0
You need to be logged in to leave comments. Login now