diff --git a/test_unstructured/partition/test_email_metadata_ownership.py b/test_unstructured/partition/test_email_metadata_ownership.py new file mode 100644 index 0000000000..c7b7eb1b40 --- /dev/null +++ b/test_unstructured/partition/test_email_metadata_ownership.py @@ -0,0 +1,51 @@ +import io +from email.message import EmailMessage +from types import SimpleNamespace + +import pytest + +from unstructured.documents.elements import ElementMetadata, Text +from unstructured.partition.email import partition_email +from unstructured.partition.msg import _MsgPartitioner + + +@pytest.mark.parametrize("field", ["sent_from", "sent_to", "cc_recipient", "bcc_recipient"]) +def test_eml_elements_own_their_address_lists(field): + message = EmailMessage() + message["From"] = "Sender " + message["To"] = "recipient@example.com" + message["Cc"] = "copy@example.com" + message["Bcc"] = "private@example.com" + message.set_content("First paragraph.\n\nSecond paragraph.") + + elements = partition_email( + file=io.BytesIO(message.as_bytes()), process_attachments=False, languages=["eng"] + ) + + assert len(elements) == 2 + original = getattr(elements[1].metadata, field).copy() + getattr(elements[0].metadata, field).clear() + assert getattr(elements[1].metadata, field) == original + + +def test_msg_elements_and_context_own_their_address_lists(monkeypatch): + from unstructured.partition import msg + + shared = ElementMetadata(sent_to=["recipient@example.com"], cc_recipient=["copy@example.com"]) + options = SimpleNamespace( + msg=SimpleNamespace(html_body="

first

second

"), + metadata_file_path=None, + metadata_last_modified=None, + partitioning_kwargs={}, + extra_msg_metadata=shared, + ) + monkeypatch.setattr(msg, "partition_html", lambda **kwargs: [Text("first"), Text("second")]) + + elements = list(_MsgPartitioner(options)._iter_message_body_elements()) + elements[0].metadata.sent_to.clear() + elements[1].metadata.cc_recipient.append("other@example.com") + + assert elements[1].metadata.sent_to == ["recipient@example.com"] + assert elements[0].metadata.cc_recipient == ["copy@example.com"] + assert shared.sent_to == ["recipient@example.com"] + assert shared.cc_recipient == ["copy@example.com"] diff --git a/unstructured/partition/email.py b/unstructured/partition/email.py index db6ed5bf8f..5a859d77b6 100644 --- a/unstructured/partition/email.py +++ b/unstructured/partition/email.py @@ -365,7 +365,7 @@ def _iter_elements(self) -> Iterator[Element]: in the message as well. """ for e in self._iter_email_body_elements(): - e.metadata.update(self._ctx.email_metadata) + e.metadata.update(copy.deepcopy(self._ctx.email_metadata)) yield e if not self._ctx.process_attachments: diff --git a/unstructured/partition/msg.py b/unstructured/partition/msg.py index b2bb9d77a6..35665aa5ad 100644 --- a/unstructured/partition/msg.py +++ b/unstructured/partition/msg.py @@ -1,5 +1,6 @@ from __future__ import annotations +import copy import os import re import tempfile @@ -273,7 +274,7 @@ def _iter_message_body_elements(self) -> Iterator[Element]: # -- augment the element metadata with email-specific values -- email_specific_metadata = self._opts.extra_msg_metadata for e in elements: - e.metadata.update(email_specific_metadata) + e.metadata.update(copy.deepcopy(email_specific_metadata)) yield e