Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: process attchments in partitioning nested emails (#3604) #3605

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions unstructured/partition/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ def extract_attachment_info(
list_attachments: list[Any] = []

for part in message.walk():
if part.is_multipart():
continue
if "content-disposition" in part:
cdisp = part["content-disposition"].split(";")
cdisp = [clean_extra_whitespace(item) for item in cdisp]
Expand Down
14 changes: 11 additions & 3 deletions unstructured/partition/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import re
import tempfile
from typing import IO, Any, Iterator, Optional
from typing import IO, Any, Iterator, Optional, Callable

from oxmsg import Message
from oxmsg.attachment import Attachment
Expand Down Expand Up @@ -35,6 +35,7 @@ def partition_msg(
metadata_filename: Optional[str] = None,
metadata_last_modified: Optional[str] = None,
process_attachments: bool = False,
attachment_partitioner: Optional[Callable[..., list[Element]]] = None,
**kwargs: Any,
) -> list[Element]:
"""Partitions a MSFT Outlook .msg file
Expand Down Expand Up @@ -64,6 +65,7 @@ def partition_msg(
metadata_file_path=metadata_filename,
metadata_last_modified=metadata_last_modified,
partition_attachments=process_attachments,
attachment_partitioner=attachment_partitioner,
)

return list(
Expand All @@ -87,13 +89,15 @@ def __init__(
metadata_file_path: str | None,
metadata_last_modified: str | None,
partition_attachments: bool,
attachment_partitioner: Optional[Callable[..., list[Element]]],
):
self._date_from_file_object = date_from_file_object
self._file = file
self._file_path = file_path
self._metadata_file_path = metadata_file_path
self._metadata_last_modified = metadata_last_modified
self._partition_attachments = partition_attachments
self._attachment_partitioner = attachment_partitioner

@lazyproperty
def is_encrypted(self) -> bool:
Expand Down Expand Up @@ -140,6 +144,11 @@ def partition_attachments(self) -> bool:
"""True when message attachments should also be partitioned."""
return self._partition_attachments

@lazyproperty
def attachment_partitioner(self) -> Optional[Callable[..., list[Element]]]:
"""The function to use to partition attachments"""
return self._attachment_partitioner

@lazyproperty
def partitioning_kwargs(self) -> dict[str, Any]:
"""Partitioning keyword-arguments to be passed along to attachment partitioner."""
Expand Down Expand Up @@ -276,7 +285,6 @@ def iter_elements(

def _iter_elements(self) -> Iterator[Element]:
"""Partition the file in an `oxmsg.attachment.Attachment` into elements."""
from unstructured.partition.auto import partition

with tempfile.TemporaryDirectory() as tmp_dir_path:
# -- save attachment as file in this temporary directory --
Expand All @@ -285,7 +293,7 @@ def _iter_elements(self) -> Iterator[Element]:
f.write(self._file_bytes)

# -- partition the attachment --
for element in partition(
for element in self._opts.attachment_partitioner(
detached_file_path,
metadata_filename=self._attachment_file_name,
metadata_last_modified=self._attachment_last_modified,
Expand Down