Skip to content
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
51 changes: 51 additions & 0 deletions test_unstructured/partition/test_email_metadata_ownership.py
Original file line number Diff line number Diff line change
@@ -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 <sender@example.com>"
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="<p>first</p><p>second</p>"),
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"]
2 changes: 1 addition & 1 deletion unstructured/partition/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion unstructured/partition/msg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import os
import re
import tempfile
Expand Down Expand Up @@ -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


Expand Down