mailreader 2.7.1__tar.gz → 2.7.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mailreader-2.7.1 → mailreader-2.7.2}/PKG-INFO +7 -2
- {mailreader-2.7.1 → mailreader-2.7.2}/README.md +6 -1
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/__init__.py +2 -0
- mailreader-2.7.2/mailreader/default.config +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/htmlmail.py +44 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader.egg-info/PKG-INFO +7 -2
- {mailreader-2.7.1 → mailreader-2.7.2}/pyproject.toml +1 -1
- mailreader-2.7.1/mailreader/default.config +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/LICENSE +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/engine.py +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/exceptions.py +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/extract.py +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/sanitize.py +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader/selectors.py +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader.egg-info/SOURCES.txt +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader.egg-info/dependency_links.txt +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader.egg-info/requires.txt +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/mailreader.egg-info/top_level.txt +0 -0
- {mailreader-2.7.1 → mailreader-2.7.2}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mailreader
|
|
3
|
-
Version: 2.7.
|
|
3
|
+
Version: 2.7.2
|
|
4
4
|
Summary: Sanitized HTML mail parsing built on the confext HTML parser
|
|
5
5
|
Author-email: nigarash <nigarash@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -82,6 +82,9 @@ mailreader.mail_to_html(raw)
|
|
|
82
82
|
# src survives for inline images
|
|
83
83
|
mailreader.select_mail(raw, 'a') # [{'tag': 'a', 'attrs': ..., 'text': ..., 'html': ...}]
|
|
84
84
|
mailreader.readable_text(raw) # same as doc.text
|
|
85
|
+
|
|
86
|
+
mailreader.save_attachments(raw_with_attachments, 'out/')
|
|
87
|
+
# writes each attachment payload to disk, e.g. 'out/report.pdf'
|
|
85
88
|
```
|
|
86
89
|
|
|
87
90
|
## API
|
|
@@ -94,6 +97,8 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
94
97
|
| `readable_text(raw)` | Readable plain text of a mail: quoted reply lines (`> …`) and everything after a `-- `/`--` signature marker stripped, blank runs collapsed. |
|
|
95
98
|
| `select_mail(raw, selector)` | CSS selector over the sanitized mail HTML; returns dicts per match. |
|
|
96
99
|
| `mail_to_html(raw)` | The sanitized mail HTML as a string. |
|
|
100
|
+
| `save_attachments(source, directory)` | Write attachment payloads to `directory` and return the written paths (`str` each). `source` is a raw message (`str`/`bytes`, parsed with `include_payload=True`) or an already-parsed `MailDocument`. Filenames are used verbatim as paths relative to `directory` — `..`, subdirectories, and absolute paths are honored — with `-1`, `-2`, … inserted before the extension on collisions
|
|
101
|
+
(`report-1.txt`); parts without a filename become `attachment-<n>`. Raises `MailReaderError` when a parsed document carries no payloads. |
|
|
97
102
|
|
|
98
103
|
### MailDocument
|
|
99
104
|
|
|
@@ -106,7 +111,7 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
106
111
|
| `html` | The sanitized body as a confext `ParsedDocument` (or `None`). |
|
|
107
112
|
| `text` | Readable plain text (from the `text/plain` part when present, else extracted from the sanitized HTML). |
|
|
108
113
|
| `parts` | Every leaf part as `{'content_type', 'charset', 'disposition', 'filename', 'content_id', 'size'}`. |
|
|
109
|
-
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. |
|
|
114
|
+
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. Use `save_attachments` to write payloads to disk. |
|
|
110
115
|
| `inline_images` | Remaining image parts carrying a `Content-ID`. |
|
|
111
116
|
| `config` | Snapshot of the engine config used. |
|
|
112
117
|
| `to_dict()` | All of the above as a plain dict (additionally reports document `stats`). |
|
|
@@ -56,6 +56,9 @@ mailreader.mail_to_html(raw)
|
|
|
56
56
|
# src survives for inline images
|
|
57
57
|
mailreader.select_mail(raw, 'a') # [{'tag': 'a', 'attrs': ..., 'text': ..., 'html': ...}]
|
|
58
58
|
mailreader.readable_text(raw) # same as doc.text
|
|
59
|
+
|
|
60
|
+
mailreader.save_attachments(raw_with_attachments, 'out/')
|
|
61
|
+
# writes each attachment payload to disk, e.g. 'out/report.pdf'
|
|
59
62
|
```
|
|
60
63
|
|
|
61
64
|
## API
|
|
@@ -68,6 +71,8 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
68
71
|
| `readable_text(raw)` | Readable plain text of a mail: quoted reply lines (`> …`) and everything after a `-- `/`--` signature marker stripped, blank runs collapsed. |
|
|
69
72
|
| `select_mail(raw, selector)` | CSS selector over the sanitized mail HTML; returns dicts per match. |
|
|
70
73
|
| `mail_to_html(raw)` | The sanitized mail HTML as a string. |
|
|
74
|
+
| `save_attachments(source, directory)` | Write attachment payloads to `directory` and return the written paths (`str` each). `source` is a raw message (`str`/`bytes`, parsed with `include_payload=True`) or an already-parsed `MailDocument`. Filenames are used verbatim as paths relative to `directory` — `..`, subdirectories, and absolute paths are honored — with `-1`, `-2`, … inserted before the extension on collisions
|
|
75
|
+
(`report-1.txt`); parts without a filename become `attachment-<n>`. Raises `MailReaderError` when a parsed document carries no payloads. |
|
|
71
76
|
|
|
72
77
|
### MailDocument
|
|
73
78
|
|
|
@@ -80,7 +85,7 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
80
85
|
| `html` | The sanitized body as a confext `ParsedDocument` (or `None`). |
|
|
81
86
|
| `text` | Readable plain text (from the `text/plain` part when present, else extracted from the sanitized HTML). |
|
|
82
87
|
| `parts` | Every leaf part as `{'content_type', 'charset', 'disposition', 'filename', 'content_id', 'size'}`. |
|
|
83
|
-
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. |
|
|
88
|
+
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. Use `save_attachments` to write payloads to disk. |
|
|
84
89
|
| `inline_images` | Remaining image parts carrying a `Content-ID`. |
|
|
85
90
|
| `config` | Snapshot of the engine config used. |
|
|
86
91
|
| `to_dict()` | All of the above as a plain dict (additionally reports document `stats`). |
|
|
@@ -18,6 +18,7 @@ from .htmlmail import (
|
|
|
18
18
|
mail_to_html,
|
|
19
19
|
parse_mail,
|
|
20
20
|
readable_text,
|
|
21
|
+
save_attachments,
|
|
21
22
|
select_mail,
|
|
22
23
|
)
|
|
23
24
|
|
|
@@ -43,5 +44,6 @@ __all__ = [
|
|
|
43
44
|
'readable_text',
|
|
44
45
|
'select_mail',
|
|
45
46
|
'mail_to_html',
|
|
47
|
+
'save_attachments',
|
|
46
48
|
'HTMLParserError',
|
|
47
49
|
]
|
|
Binary file
|
|
@@ -5,11 +5,14 @@ import re
|
|
|
5
5
|
from email import policy as _email_policy
|
|
6
6
|
from email.parser import BytesParser as _BytesParser
|
|
7
7
|
from html import escape as _escape
|
|
8
|
+
from pathlib import Path
|
|
8
9
|
from typing import Any, Dict, List, Optional, Union
|
|
9
10
|
|
|
10
11
|
from confext import ParsedDocument
|
|
12
|
+
from confext.config import PathLike
|
|
11
13
|
|
|
12
14
|
from .engine import get_engine
|
|
15
|
+
from .exceptions import MailReaderError
|
|
13
16
|
from .extract import find_all as _find_all, text as _extract_text
|
|
14
17
|
|
|
15
18
|
_QUOTED_LINE = re.compile(r'^\s*>')
|
|
@@ -154,6 +157,47 @@ def mail_to_html(raw: Union[str, bytes]) -> str:
|
|
|
154
157
|
return doc.to_html() if doc is not None else ''
|
|
155
158
|
|
|
156
159
|
|
|
160
|
+
def save_attachments(source: Union[str, bytes, MailDocument],
|
|
161
|
+
directory: PathLike) -> List[str]:
|
|
162
|
+
"""Write a mail's attachment payloads to a directory; return saved paths.
|
|
163
|
+
|
|
164
|
+
Accepts a raw message or an already-parsed MailDocument (a raw message
|
|
165
|
+
is parsed with include_payload=True). Attachment filenames are used
|
|
166
|
+
verbatim as paths relative to directory, so '..', subdirectories, and
|
|
167
|
+
absolute paths are all honored; collisions get -1, -2, ... suffixes and
|
|
168
|
+
unnamed attachments become attachment-<n>. Raises MailReaderError when
|
|
169
|
+
a parsed document carries no payloads.
|
|
170
|
+
"""
|
|
171
|
+
if isinstance(source, MailDocument):
|
|
172
|
+
doc = source
|
|
173
|
+
else:
|
|
174
|
+
doc = parse_mail(source, include_payload=True)
|
|
175
|
+
target = Path(directory)
|
|
176
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
177
|
+
saved: List[str] = []
|
|
178
|
+
used: set = set()
|
|
179
|
+
for index, att in enumerate(doc.attachments, 1):
|
|
180
|
+
payload = att.get('payload')
|
|
181
|
+
if payload is None:
|
|
182
|
+
raise MailReaderError(
|
|
183
|
+
'attachment payload missing; pass the raw message or '
|
|
184
|
+
'parse with include_payload=True'
|
|
185
|
+
)
|
|
186
|
+
name = att.get('filename') or f'attachment-{index}'
|
|
187
|
+
stem, ext = Path(name).stem, Path(name).suffix
|
|
188
|
+
path = target / name
|
|
189
|
+
suffix = 1
|
|
190
|
+
while path.exists() or str(path) in used:
|
|
191
|
+
path = (target / f'{stem}-{suffix}{ext}' if ext
|
|
192
|
+
else target / f'{name}-{suffix}')
|
|
193
|
+
suffix += 1
|
|
194
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
195
|
+
path.write_bytes(payload)
|
|
196
|
+
used.add(str(path))
|
|
197
|
+
saved.append(str(path))
|
|
198
|
+
return saved
|
|
199
|
+
|
|
200
|
+
|
|
157
201
|
def _charset_for(part, config: Dict[str, Any]) -> str:
|
|
158
202
|
charset = part.get_content_charset()
|
|
159
203
|
if charset:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mailreader
|
|
3
|
-
Version: 2.7.
|
|
3
|
+
Version: 2.7.2
|
|
4
4
|
Summary: Sanitized HTML mail parsing built on the confext HTML parser
|
|
5
5
|
Author-email: nigarash <nigarash@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -82,6 +82,9 @@ mailreader.mail_to_html(raw)
|
|
|
82
82
|
# src survives for inline images
|
|
83
83
|
mailreader.select_mail(raw, 'a') # [{'tag': 'a', 'attrs': ..., 'text': ..., 'html': ...}]
|
|
84
84
|
mailreader.readable_text(raw) # same as doc.text
|
|
85
|
+
|
|
86
|
+
mailreader.save_attachments(raw_with_attachments, 'out/')
|
|
87
|
+
# writes each attachment payload to disk, e.g. 'out/report.pdf'
|
|
85
88
|
```
|
|
86
89
|
|
|
87
90
|
## API
|
|
@@ -94,6 +97,8 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
94
97
|
| `readable_text(raw)` | Readable plain text of a mail: quoted reply lines (`> …`) and everything after a `-- `/`--` signature marker stripped, blank runs collapsed. |
|
|
95
98
|
| `select_mail(raw, selector)` | CSS selector over the sanitized mail HTML; returns dicts per match. |
|
|
96
99
|
| `mail_to_html(raw)` | The sanitized mail HTML as a string. |
|
|
100
|
+
| `save_attachments(source, directory)` | Write attachment payloads to `directory` and return the written paths (`str` each). `source` is a raw message (`str`/`bytes`, parsed with `include_payload=True`) or an already-parsed `MailDocument`. Filenames are used verbatim as paths relative to `directory` — `..`, subdirectories, and absolute paths are honored — with `-1`, `-2`, … inserted before the extension on collisions
|
|
101
|
+
(`report-1.txt`); parts without a filename become `attachment-<n>`. Raises `MailReaderError` when a parsed document carries no payloads. |
|
|
97
102
|
|
|
98
103
|
### MailDocument
|
|
99
104
|
|
|
@@ -106,7 +111,7 @@ mailreader.readable_text(raw) # same as doc.text
|
|
|
106
111
|
| `html` | The sanitized body as a confext `ParsedDocument` (or `None`). |
|
|
107
112
|
| `text` | Readable plain text (from the `text/plain` part when present, else extracted from the sanitized HTML). |
|
|
108
113
|
| `parts` | Every leaf part as `{'content_type', 'charset', 'disposition', 'filename', 'content_id', 'size'}`. |
|
|
109
|
-
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. |
|
|
114
|
+
| `attachments` | Same shape, restricted to attachments (a filename or an attachment disposition); plus `payload` bytes when `include_payload=True`. Use `save_attachments` to write payloads to disk. |
|
|
110
115
|
| `inline_images` | Remaining image parts carrying a `Content-ID`. |
|
|
111
116
|
| `config` | Snapshot of the engine config used. |
|
|
112
117
|
| `to_dict()` | All of the above as a plain dict (additionally reports document `stats`). |
|
|
Binary file
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|