|
2 | 2 |
|
3 | 3 | import os
|
4 | 4 | from pathlib import Path
|
| 5 | +from logging import Logger |
5 | 6 |
|
6 | 7 | from sphinx.transforms import SphinxTransform
|
7 | 8 | from sphinx.errors import ExtensionError
|
|
40 | 41 | )
|
41 | 42 |
|
42 | 43 |
|
| 44 | +class LoggerAdapterWrapper(Logger): |
| 45 | + """Wrap a logger adapter, while pretending to be a logger. |
| 46 | +
|
| 47 | + Workaround of https://github.com/ipython/traitlets/issues/606 |
| 48 | + """ |
| 49 | + def __init__(self, wrapped): |
| 50 | + self._wrapped = wrapped |
| 51 | + |
| 52 | + def __getattribute__(self, attr): |
| 53 | + if attr == "_wrapped": |
| 54 | + return object.__getattribute__(self, attr) |
| 55 | + return self._wrapped.__getattribute__(attr) |
| 56 | + |
| 57 | + |
43 | 58 | class JupyterKernel(Directive):
|
44 | 59 | """Specify a new Jupyter Kernel.
|
45 | 60 |
|
@@ -262,20 +277,16 @@ def write_notebook_output(notebook, output_dir, notebook_name, location=None):
|
262 | 277 | resources,
|
263 | 278 | os.path.join(output_dir, notebook_name + ".ipynb"),
|
264 | 279 | )
|
265 |
| - # Write a script too. Note that utf-8 is the de facto |
266 |
| - # standard encoding for notebooks. |
267 |
| - ext = notebook.metadata.get("language_info", {}).get("file_extension", None) |
268 |
| - if ext is None: |
269 |
| - ext = ".txt" |
270 |
| - js.logger.warning( |
271 |
| - "Notebook code has no file extension metadata, " "defaulting to `.txt`", |
272 |
| - location=location, |
273 |
| - ) |
274 |
| - contents = "\n\n".join(cell.source for cell in notebook.cells) |
275 | 280 |
|
276 |
| - notebook_file = notebook_name + ext |
| 281 | + # Write a script too. Due to https://github.com/ipython/traitlets/issues/606 |
| 282 | + # we're using a wrapper to make a LoggerAdapter look like a wrapper. |
| 283 | + exporter = nbconvert.exporters.ScriptExporter(log=LoggerAdapterWrapper(js.logger)) |
| 284 | + contents, resources = exporter.from_notebook_node(notebook) |
| 285 | + |
| 286 | + notebook_file = notebook_name + resources['output_extension'] |
277 | 287 | output_dir = Path(output_dir)
|
278 |
| - (output_dir / notebook_file).write_text(contents, encoding = "utf8") |
| 288 | + # utf-8 is the de-facto standard encoding for notebooks. |
| 289 | + (output_dir / notebook_file).write_text(contents, encoding="utf8") |
279 | 290 |
|
280 | 291 |
|
281 | 292 | def contains_widgets(notebook):
|
|
0 commit comments