From 32be570aae5ebc686df1330317a11cc691f5c156 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Apr 2023 13:25:15 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- nbconvert/exporters/exporter.py | 1 + nbconvert/exporters/templateexporter.py | 1 + nbconvert/preprocessors/__init__.py | 2 +- nbconvert/preprocessors/extractattachments.py | 30 +++++++++++++------ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/nbconvert/exporters/exporter.py b/nbconvert/exporters/exporter.py index 5023d59bf..f833a344e 100644 --- a/nbconvert/exporters/exporter.py +++ b/nbconvert/exporters/exporter.py @@ -95,6 +95,7 @@ class Exporter(LoggingConfigurable): "nbconvert.preprocessors.LatexPreprocessor", "nbconvert.preprocessors.HighlightMagicsPreprocessor", "nbconvert.preprocessors.ExtractOutputPreprocessor", + "nbconvert.preprocessors.ExtractAttachmentsPreprocessor", "nbconvert.preprocessors.ClearMetadataPreprocessor", ], help="""List of preprocessors available by default, by name, namespace, diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index 21603b53b..cdee19bfb 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -181,6 +181,7 @@ def default_config(self): { "RegexRemovePreprocessor": {"enabled": True}, "TagRemovePreprocessor": {"enabled": True}, + "ExtractAttachmentsPreprocessor": {"enabled": True}, } ) c.merge(super().default_config) diff --git a/nbconvert/preprocessors/__init__.py b/nbconvert/preprocessors/__init__.py index 683ee6db7..a13443d92 100644 --- a/nbconvert/preprocessors/__init__.py +++ b/nbconvert/preprocessors/__init__.py @@ -11,7 +11,7 @@ from .convertfigures import ConvertFiguresPreprocessor from .csshtmlheader import CSSHTMLHeaderPreprocessor from .execute import ExecutePreprocessor -from .extractattachments import ExtractAttachmentPreprocessor +from .extractattachments import ExtractAttachmentsPreprocessor from .extractoutput import ExtractOutputPreprocessor from .highlightmagics import HighlightMagicsPreprocessor from .latex import LatexPreprocessor diff --git a/nbconvert/preprocessors/extractattachments.py b/nbconvert/preprocessors/extractattachments.py index 74369e0ea..5d2f9fb19 100644 --- a/nbconvert/preprocessors/extractattachments.py +++ b/nbconvert/preprocessors/extractattachments.py @@ -3,23 +3,30 @@ """ # TODO -# - Tie attachments_directory to specific notebook # - Look at what `resources` contains, and determine if that requires changes -from pathlib import Path +import random +import string from base64 import b64decode +from pathlib import Path + from traitlets import Unicode from .base import Preprocessor -class ExtractAttachmentPreprocessor(Preprocessor): + +class ExtractAttachmentsPreprocessor(Preprocessor): """ Extracts attachments from all (markdown and raw) cells in a notebook. The extracted attachments are stored in a directory ('attachments' by default). https://nbformat.readthedocs.io/en/latest/format_description.html#cell-attachments """ - attachments_directory = Unicode("attachments").tag(config=True) + attachments_directory = Unicode("attachments_{suffix}").tag(config=True) + + def __init__(self, **kw): + super().__init__(**kw) + self.unique_suffix = ''.join(random.choice(string.ascii_letters) for i in range(16)) def preprocess(self, nb, resources): """ @@ -27,9 +34,11 @@ def preprocess(self, nb, resources): Alert and exit if it does. Else continue. """ - dir = Path(self.attachments_directory) - assert not dir.exists(), f"{self.attachments_directory} directory already exists." - dir.mkdir() + path_name = self.attachments_directory.format(suffix=self.unique_suffix) + dir_path = Path(path_name) + if dir_path.exists(): + self.log.error(f"{path_name} directory already exists.") + dir_path.mkdir() nb, resources = super().preprocess(nb, resources) return nb, resources @@ -43,6 +52,7 @@ def preprocess_cell(self, cell, resources, index): '![image.png](021fdd80.png)' """ if "attachments" in cell: + path_name = self.attachments_directory.format(suffix=self.unique_suffix) for fname in cell.attachments: self.log.debug(f"Encountered attachment {fname}") @@ -52,13 +62,15 @@ def preprocess_cell(self, cell, resources, index): # mime types under same filename, and I can't index into it without the mimetype. # So I only read the first one. for mimetype in cell.attachments[fname]: - with open(self.attachments_directory+"/"+fname, "wb") as f: + with open(path_name + "/" + fname, "wb") as f: data = cell.attachments[fname][mimetype].encode("utf-8") decoded = b64decode(data) f.write(decoded) break # Edit the reference to the attachment - cell.source = cell.source.replace("attachment:"+fname, self.attachments_directory+"/"+fname) + cell.source = cell.source.replace( + "attachment:" + fname, path_name + "/" + fname + ) return cell, resources