Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and tuncbkose committed Apr 19, 2023
1 parent 0c6b1af commit 32be570
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def default_config(self):
{
"RegexRemovePreprocessor": {"enabled": True},
"TagRemovePreprocessor": {"enabled": True},
"ExtractAttachmentsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/preprocessors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions nbconvert/preprocessors/extractattachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,42 @@
"""

# 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):
"""
Check if attachments directory already exists.
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

Expand All @@ -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}")

Expand All @@ -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

0 comments on commit 32be570

Please sign in to comment.