Skip to content

Commit 036d991

Browse files
Merge pull request #13 from loadsmart/recursivePartLookup
Support recursive parts for attachment lookup
2 parents 41a7489 + 6a723d1 commit 036d991

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

gmail_wrapper/entities.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,24 @@ def date(self):
106106
date_in_seconds = int(self._raw.get("internalDate")) / ms_in_seconds
107107
return datetime.utcfromtimestamp(date_in_seconds)
108108

109+
def _extract_attachments_from_parts(self, parts):
110+
attachments = []
111+
for part in parts:
112+
if part.get("filename") and part.get("body", {}).get("attachmentId"):
113+
attachments.append(Attachment(self.id, self._client, part))
114+
if part.get("parts"):
115+
attachments += self._extract_attachments_from_parts(part.get("parts"))
116+
117+
return attachments
118+
109119
@property
110120
def attachments(self):
111121
parts = self._payload.get("parts")
112-
return [
113-
Attachment(self.id, self._client, part)
114-
for part in (parts if parts else [])
115-
if part["filename"] and part["body"] and part["body"].get("attachmentId")
116-
]
122+
123+
if not parts:
124+
return []
125+
126+
return self._extract_attachments_from_parts(parts)
117127

118128
def modify(self, add_labels=None, remove_labels=None):
119129
self._raw = self._client.modify_raw_message(

tests/conftest.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def raw_complete_message():
5252
"parts": [
5353
{
5454
"body": {
55-
"data": "I am caught up in a meeting. Can you run an urgent errand for me?",
55+
"data": "I am Dr. Bakare Tunde, the cousin of Nigerian Astronaut, Air Force Major Abacha Tunde. He was the first African in space when he made a secret flight to the Salyut 6 space station in 1979.",
5656
"attachmentId": "CCX456",
57-
"size": 65,
57+
"size": 188,
5858
},
5959
"mimeType": "text/plain",
6060
"partId": "BB789",
@@ -66,10 +66,29 @@ def raw_complete_message():
6666
"partId": "BB790",
6767
"filename": "fox.txt",
6868
},
69+
{
70+
"body": {"size": 0},
71+
"mimeType": "multipart/mixed",
72+
"partId": "BB791",
73+
"parts": [
74+
{
75+
"body": {"data": "someRandomDataHere", "size": 18},
76+
"mimeType": "text/html",
77+
"partId": "BB791.0",
78+
"filename": "",
79+
},
80+
{
81+
"body": {"attachmentId": "CCX458", "size": 95},
82+
"mimeType": "application/pdf",
83+
"partId": "BB791.1",
84+
"filename": "tigers.pdf",
85+
}
86+
],
87+
},
6988
],
7089
},
71-
"snippet": "I am caught up in a meeting. Can you run an urgent errand for me?",
72-
"sizeEstimate": 125,
90+
"snippet": "I am Dr. Bakare Tunde, the cousin of Nigerian Astronaut, Air Force Major Abacha Tunde.",
91+
"sizeEstimate": 361,
7392
"threadId": "AA121212",
7493
"labelIds": ["phishing"],
7594
"id": "123AAB",

tests/test_entities.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ def test_it_does_not_generate_attachment_objects_for_attachments_without_id(
7777
"mimeType": "application/pdf",
7878
}
7979
)
80-
assert len(complete_message.attachments) == 1
80+
assert len(complete_message.attachments) == 2
8181
assert complete_message.attachments[0].id
82-
assert complete_message.attachments[0].filename != "invalid.pdf"
82+
assert complete_message.attachments[0].filename == "fox.txt"
83+
assert complete_message.attachments[1].id
84+
assert complete_message.attachments[1].filename == "tigers.pdf"
8385

8486
def test_it_returns_empty_list_if_no_attachments(
8587
self, client, raw_complete_message

0 commit comments

Comments
 (0)