email-forward-parser 0.2.1__tar.gz → 0.2.3__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.
Files changed (25) hide show
  1. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/.githooks/pre-push +7 -1
  2. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/.github/workflows/ci.yml +1 -1
  3. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/.github/workflows/workflow.yml +3 -3
  4. email_forward_parser-0.2.3/.python-version +1 -0
  5. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/PKG-INFO +15 -2
  6. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/README.md +13 -1
  7. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/forward_parser.py +4 -1
  8. email_forward_parser-0.2.3/emailforwardparser/literals.py +274 -0
  9. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/loop.py +16 -0
  10. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/regexs.py +4 -0
  11. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/utils.py +11 -1
  12. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/pyproject.toml +5 -2
  13. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/tests/coverage_edges_test.py +10 -0
  14. email_forward_parser-0.2.3/tests/literals_test.py +178 -0
  15. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/uv.lock +1 -1
  16. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/.github/dependabot.yml +0 -0
  17. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/.gitignore +0 -0
  18. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/LICENSE +0 -0
  19. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/NOTICE +0 -0
  20. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/__init__.py +0 -0
  21. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/emailforwardparser/client.py +0 -0
  22. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/tests/__init__.py +0 -0
  23. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/tests/client_test.py +0 -0
  24. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/tests/forward_parser_test.py +0 -0
  25. {email_forward_parser-0.2.1 → email_forward_parser-0.2.3}/tests/regressions_test.py +0 -0
@@ -1,5 +1,6 @@
1
1
  #!/bin/sh
2
- # Style and type checks on the commits being pushed; GitHub Actions runs the tests.
2
+ # Style, type and test checks on the commits being pushed, on the newest Python (.python-version); GitHub Actions
3
+ # runs the tests on the oldest one the package supports.
3
4
  # Turn it on once per clone: git config core.hooksPath .githooks
4
5
 
5
6
  fix='uv run black . && uv run isort . && uv run ruff check --fix .'
@@ -14,6 +15,11 @@ checks() {
14
15
  check uv run isort --check-only .
15
16
  check uv run ruff check .
16
17
  check uv run mypy
18
+ # The coverage gate runs with the JIT off: under it, CPython 3.15 reports a loop's exit branch from the wrong
19
+ # instruction (coveragepy/coveragepy#2249). Callers may run Python with the JIT on, so the suite runs again
20
+ # under it, without coverage.
21
+ check env PYTHON_JIT=0 uv run pytest
22
+ check env PYTHON_JIT=1 uv run pytest --no-cov
17
23
  }
18
24
 
19
25
  # --- the same in every repo from here ---
@@ -21,7 +21,7 @@ jobs:
21
21
  steps:
22
22
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
23
23
  - name: Install uv and Python
24
- uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
24
+ uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
25
25
  with:
26
26
  python-version: "3.14"
27
27
  enable-cache: true
@@ -16,7 +16,7 @@ jobs:
16
16
  steps:
17
17
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
18
18
  - name: Install uv and Python
19
- uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
19
+ uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
20
20
  with:
21
21
  python-version: "3.14"
22
22
  enable-cache: true
@@ -40,7 +40,7 @@ jobs:
40
40
  uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
41
41
 
42
42
  - name: Set up Python
43
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
43
+ uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
44
44
  with:
45
45
  python-version: "3.14"
46
46
 
@@ -78,7 +78,7 @@ jobs:
78
78
  path: dist/
79
79
 
80
80
  - name: Publish package
81
- uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
81
+ uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
82
82
 
83
83
  release:
84
84
  name: Publish GitHub release
@@ -0,0 +1 @@
1
+ 3.15
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: email-forward-parser
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: Python Library to parse forwarded email messages
5
5
  Project-URL: Homepage, https://github.com/felipehertzer/email-forward-parser-py
6
6
  Project-URL: Repository, https://github.com/felipehertzer/email-forward-parser-py
@@ -213,6 +213,7 @@ Classifier: License :: OSI Approved :: Apache Software License
213
213
  Classifier: Operating System :: OS Independent
214
214
  Classifier: Programming Language :: Python :: 3
215
215
  Classifier: Programming Language :: Python :: 3.14
216
+ Classifier: Programming Language :: Python :: 3.15
216
217
  Classifier: Topic :: Communications :: Email
217
218
  Classifier: Topic :: Text Processing
218
219
  Requires-Python: >=3.14
@@ -524,7 +525,19 @@ python -m mypy
524
525
  python -m pytest
525
526
  ```
526
527
 
527
- `pytest` is configured to require 100% statement and branch coverage.
528
+ `pytest` is configured to require 100% statement and branch coverage. On Python 3.15,
529
+ measure it with the JIT off (`PYTHON_JIT=0 python -m pytest`): with the JIT on, CPython
530
+ 3.15 reports some branches from the wrong instruction (coveragepy/coveragepy#2249). The
531
+ pre-push hook runs the gate that way, then the suite again with `PYTHON_JIT=1` and
532
+ `--no-cov`.
533
+
534
+ `emailforwardparser/literals.py` lists the text each pattern in `regexs.py` needs
535
+ before it can match, so a pattern is not run on a string without it. After changing
536
+ a pattern, write the table again (the suite fails until it matches):
537
+
538
+ ```bash
539
+ python -m tests.literals_test && python -m black emailforwardparser/literals.py
540
+ ```
528
541
 
529
542
  ## Release
530
543
 
@@ -304,7 +304,19 @@ python -m mypy
304
304
  python -m pytest
305
305
  ```
306
306
 
307
- `pytest` is configured to require 100% statement and branch coverage.
307
+ `pytest` is configured to require 100% statement and branch coverage. On Python 3.15,
308
+ measure it with the JIT off (`PYTHON_JIT=0 python -m pytest`): with the JIT on, CPython
309
+ 3.15 reports some branches from the wrong instruction (coveragepy/coveragepy#2249). The
310
+ pre-push hook runs the gate that way, then the suite again with `PYTHON_JIT=1` and
311
+ `--no-cov`.
312
+
313
+ `emailforwardparser/literals.py` lists the text each pattern in `regexs.py` needs
314
+ before it can match, so a pattern is not run on a string without it. After changing
315
+ a pattern, write the table again (the suite fails until it matches):
316
+
317
+ ```bash
318
+ python -m tests.literals_test && python -m black emailforwardparser/literals.py
319
+ ```
308
320
 
309
321
  ## Release
310
322
 
@@ -225,8 +225,11 @@ def parse_mailbox(regexes: list[Pattern[str]], text: str) -> list[MailboxResult]
225
225
  mailboxes_line = match[-1].strip()
226
226
  if mailboxes_line:
227
227
  mailboxes = []
228
+ mailbox_regexes = (
229
+ regexs.MAILBOX if "mailto:" in mailboxes_line else regexs.MAILBOX_WITHOUT_MAILTO
230
+ )
228
231
  while mailboxes_line:
229
- mailbox_match, _ = loop.loop_regexes_match(regexs.MAILBOX, mailboxes_line)
232
+ mailbox_match, _ = loop.loop_regexes_match(mailbox_regexes, mailboxes_line)
230
233
  if mailbox_match:
231
234
  if len(mailbox_match) == 3:
232
235
  name = mailbox_match[1]
@@ -0,0 +1,274 @@
1
+ """Generated by tests/literals_test.py from regexs.py: do not edit by hand.
2
+
3
+ For each pattern in regexs.py's lists, text that every match of it contains. A string
4
+ without that text cannot match, so the loop helpers do not run the pattern on it.
5
+ """
6
+
7
+ # Keyed by pattern source: every pattern in regexs.py carries its flags inline.
8
+ REQUIRED_LITERALS: dict[str, str] = {
9
+ r"(?m)^Fw:(.*)": "Fw:",
10
+ r"(?m)^VS:(.*)": "VS:",
11
+ r"(?m)^WG:(.*)": "WG:",
12
+ r"(?m)^RV:(.*)": "RV:",
13
+ r"(?m)^TR:(.*)": "TR:",
14
+ r"(?m)^I:(.*)": "I:",
15
+ r"(?m)^FW:(.*)": "FW:",
16
+ r"(?m)^Vs:(.*)": "Vs:",
17
+ r"(?m)^PD:(.*)": "PD:",
18
+ r"(?m)^ENC:(.*)": "ENC:",
19
+ r"(?m)^Redir.:(.*)": "Redir",
20
+ r"(?m)^VB:(.*)": "VB:",
21
+ r"(?m)^VL:(.*)": "VL:",
22
+ r"(?m)^Videresend:(.*)": "Videresend:",
23
+ r"(?m)^İLT:(.*)": "İLT:",
24
+ r"(?m)^Fwd:(.*)": "Fwd:",
25
+ r"(?m)^(?:>\s*|[^\S\n]*)Begin forwarded message\s?:": "Begin forwarded message",
26
+ r"(?m)^(?:>\s*|[^\S\n]*)Začátek přeposílané zprávy\s?:": "Začátek přeposílané zprávy",
27
+ r"(?m)^(?:>\s*|[^\S\n]*)Start på videresendt besked\s?:": "Start på videresendt besked",
28
+ r"(?m)^(?:>\s*|[^\S\n]*)Anfang der weitergeleiteten Nachricht\s?:": "Anfang der weitergeleiteten Nachricht",
29
+ r"(?m)^(?:>\s*|[^\S\n]*)Inicio del mensaje reenviado\s?:": "Inicio del mensaje reenviado",
30
+ r"(?m)^(?:>\s*|[^\S\n]*)Välitetty viesti alkaa\s?:": "Välitetty viesti alkaa",
31
+ r"(?m)^(?:>\s*|[^\S\n]*)Début du message réexpédié\s?:": "Début du message réexpédié",
32
+ r"(?m)^(?:>\s*|[^\S\n]*)Début du message transféré\s?:": "Début du message transféré",
33
+ r"(?m)^(?:>\s*|[^\S\n]*)Započni proslijeđenu poruku\s?:": "Započni proslijeđenu poruku",
34
+ r"(?m)^(?:>\s*|[^\S\n]*)Továbbított levél kezdete\s?:": "Továbbított levél kezdete",
35
+ r"(?m)^(?:>\s*|[^\S\n]*)Inizio messaggio inoltrato\s?:": "Inizio messaggio inoltrato",
36
+ r"(?m)^(?:>\s*|[^\S\n]*)Begin doorgestuurd bericht\s?:": "Begin doorgestuurd bericht",
37
+ r"(?m)^(?:>\s*|[^\S\n]*)Videresendt melding\s?:": "Videresendt melding",
38
+ r"(?m)^(?:>\s*|[^\S\n]*)Początek przekazywanej wiadomości\s?:": "Początek przekazywanej wiadomości",
39
+ r"(?m)^(?:>\s*|[^\S\n]*)Início da mensagem reencaminhada\s?:": "Início da mensagem reencaminhada",
40
+ r"(?m)^(?:>\s*|[^\S\n]*)Início da mensagem encaminhada\s?:": "Início da mensagem encaminhada",
41
+ r"(?m)^(?:>\s*|[^\S\n]*)Începe mesajul redirecționat\s?:": "Începe mesajul redirecționat",
42
+ r"(?m)^(?:>\s*|[^\S\n]*)Начало переадресованного сообщения\s?:": "Начало переадресованного сообщения",
43
+ r"(?m)^(?:>\s*|[^\S\n]*)Začiatok preposlanej správy\s?:": "Začiatok preposlanej správy",
44
+ r"(?m)^(?:>\s*|[^\S\n]*)Vidarebefordrat mejl\s?:": "Vidarebefordrat mejl",
45
+ r"(?m)^(?:>\s*|[^\S\n]*)İleti başlangıcı\s?:": "İleti başlangıcı",
46
+ r"(?m)^(?:>\s*|[^\S\n]*)Початок листа, що пересилається\s?:": "Початок листа, що пересилається",
47
+ r"(?m)^[^\S\n]*-{8,10}\s*Forwarded message\s*-{8,10}\s*": "Forwarded message",
48
+ r"(?m)^[^\S\n]*_{32}\s*$": "_",
49
+ r"(?m)^\s?Dne\s?.+\,\s?.+(?:\n\s*)?[\[|<].+[\]|>]\s?napsal\(a\)\s?:": "napsal(a)",
50
+ r"(?m)^\s?D.\s?.+\s?skrev\s?\".+\"\s*[\[|<].+[\]|>]\s?:": "skrev",
51
+ r"(?m)^\s?Am\s?.+\s?schrieb\s?\".+\"\s*[\[|<].+[\]|>]\s?:": "schrieb",
52
+ r"(?m)^\s?On\s?.+\,\s?\".+\"\s*[\[|<].+[\]|>]\s?wrote\s?:": "wrote",
53
+ r"(?m)^\s?El\s?.+\,\s?\".+\"\s*[\[|<].+[\]|>]\s?escribió\s?:": "escribió",
54
+ r"(?m)^\s?Le\s?.+\,\s?«.+»\s*[\[|<].+[\]|>]\s?a écrit\s?:": "a écrit",
55
+ r"(?m)^\s?.+(?:\n\s*)?[\[|<].+[\]|>]\s?kirjoitti\s?.+\s?:": "kirjoitti",
56
+ r"(?m)^\s?.+\s?időpontban\s?.+(?:\n\s*)?[\[|<|(].+[\]|>|)]\s?ezt írta\s?:": "időpontban",
57
+ r"(?m)^\s?Il giorno\s?.+\s?\".+\"\s*[\[|<].+[\]|>]\s?ha scritto\s?:": "ha scritto",
58
+ r"(?m)^\s?Op\s?.+\s?heeft\s?.+(?:\n\s*)?[\[|<].+[\]|>]\s?geschreven\s?:": "geschreven",
59
+ r"(?m)^\s?.+(?:\n\s*)?[\[|<].+[\]|>]\s?skrev følgende den\s?.+\s?:": "skrev følgende den",
60
+ r"(?m)^\s?Dnia\s?.+\s?„.+”\s*[\[|<].+[\]|>]\s?napisał\s?:": "napisał",
61
+ r"(?m)^\s?Em\s?.+\,\s?\".+\"\s*[\[|<].+[\]|>]\s?escreveu\s?:": "escreveu",
62
+ r"(?m)^\s?.+\s?пользователь\s?\".+\"\s*[\[|<].+[\]|>]\s?написал\s?:": "пользователь",
63
+ r"(?m)^\s?.+\s?používateľ\s?.+(?:\n\s*)?\([\[|<].+[\]|>]\)\s?napísal\s?:": "používateľ",
64
+ r"(?m)^\s?Den\s?.+\s?skrev\s?\".+\"\s*[\[|<].+[\]|>]\s?följande\s?:": "följande",
65
+ r"(?m)^\s?\".+\"\s*[\[|<].+[\]|>]\,\s?.+\s?tarihinde şunu yazdı\s?:": "tarihinde şunu yazdı",
66
+ r"(?m)^[^\S\n]*-{5,8} Přeposlaná zpráva -{5,8}\s*": " Přeposlaná zpráva ",
67
+ r"(?m)^[^\S\n]*-{5,8} Videresendt meddelelse -{5,8}\s*": " Videresendt meddelelse ",
68
+ r"(?m)^[^\S\n]*-{5,10} Weitergeleitete Nachricht -{5,10}\s*": " Weitergeleitete Nachricht ",
69
+ r"(?m)^[^\S\n]*-{5,8} Forwarded Message -{5,8}\s*": " Forwarded Message ",
70
+ r"(?m)^[^\S\n]*-{5,10} Mensaje reenviado -{5,10}\s*": " Mensaje reenviado ",
71
+ r"(?m)^[^\S\n]*-{5,10} Edelleenlähetetty viesti -{5,10}\s*": " Edelleenlähetetty viesti ",
72
+ r"(?m)^[^\S\n]*-{5} Message transmis -{5}\s*": " Message transmis ",
73
+ r"(?m)^[^\S\n]*-{5,8} Továbbított üzenet -{5,8}\s*": " Továbbított üzenet ",
74
+ r"(?m)^[^\S\n]*-{5,10} Messaggio inoltrato -{5,10}\s*": " Messaggio inoltrato ",
75
+ r"(?m)^[^\S\n]*-{5,10} Doorgestuurd bericht -{5,10}\s*": " Doorgestuurd bericht ",
76
+ r"(?m)^[^\S\n]*-{5,8} Videresendt melding -{5,8}\s*": " Videresendt melding ",
77
+ r"(?m)^[^\S\n]*-{5} Przekazana wiadomość -{5}\s*": " Przekazana wiadomość ",
78
+ r"(?m)^[^\S\n]*-{5,8} Mensagem reencaminhada -{5,8}\s*": " Mensagem reencaminhada ",
79
+ r"(?m)^[^\S\n]*-{5,10} Mensagem encaminhada -{5,10}\s*": " Mensagem encaminhada ",
80
+ r"(?m)^[^\S\n]*-{5,8} Mesaj redirecționat -{5,8}\s*": " Mesaj redirecționat ",
81
+ r"(?m)^[^\S\n]*-{5} Пересылаемое сообщение -{5}\s*": " Пересылаемое сообщение ",
82
+ r"(?m)^[^\S\n]*-{5} Preposlaná správa -{5}\s*": " Preposlaná správa ",
83
+ r"(?m)^[^\S\n]*-{5,10} Vidarebefordrat meddelande -{5,10}\s*": " Vidarebefordrat meddelande ",
84
+ r"(?m)^[^\S\n]*-{5} İletilmiş Mesaj -{5}\s*": " İletilmiş Mesaj ",
85
+ r"(?m)^[^\S\n]*-{5} Перенаправлене повідомлення -{5}\s*": " Перенаправлене повідомлення ",
86
+ r"(?m)^[^\S\n]*-{8} Välitetty viesti \/ Fwd.Msg -{8}\s*": " Välitetty viesti / Fwd",
87
+ r"(?m)^[^\S\n]*-{8,10} Message transféré -{8,10}\s*": " Message transféré ",
88
+ r"(?m)^[^\S\n]*-{8} Proslijeđena poruka -{8}\s*": " Proslijeđena poruka ",
89
+ r"(?m)^[^\S\n]*-{8} Messaggio Inoltrato -{8}\s*": " Messaggio Inoltrato ",
90
+ r"(?m)^[^\S\n]*-{3} Treść przekazanej wiadomości -{3}\s*": " Treść przekazanej wiadomości ",
91
+ r"(?m)^[^\S\n]*-{8} Перенаправленное сообщение -{8}\s*": " Перенаправленное сообщение ",
92
+ r"(?m)^[^\S\n]*-{8} Preposlaná správa --- Forwarded Message -{8}\s*": " Preposlaná správa --- Forwarded Message ",
93
+ r"(?m)^[^\S\n]*-{8} İletilen İleti -{8}\s*": " İletilen İleti ",
94
+ r"(?m)^[^\S\n]*-{8} Переслане повідомлення -{8}\s*": " Переслане повідомлення ",
95
+ r"(?m)^[^\S\n]*-{9,10} メッセージを転送 -{9,10}\s*": " メッセージを転送 ",
96
+ r"(?m)^[^\S\n]*-{9,10} Wiadomość przesłana dalej -{9,10}\s*": " Wiadomość przesłana dalej ",
97
+ r"(?m)^(?:>\s*|[^\S\n]*)-{10} Original Message -{10}\s*": " Original Message ",
98
+ r"(?m)^\s?Dne\s?(?P<date>.+)\,\s?(?P<from_name>.+)(?:\n\s*)?[\[|<](?P<from_address>.+)[\]|>]\s?napsal\(a\)\s?:": "napsal(a)",
99
+ r"(?m)^\s?D.\s?(?P<date>.+)\s?skrev\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?:": "skrev",
100
+ r"(?m)^\s?Am\s?(?P<date>.+)\s?schrieb\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?:": "schrieb",
101
+ r"(?m)^\s?On\s?(?P<date>.+)\,\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?wrote\s?:": "wrote",
102
+ r"(?m)^\s?El\s?(?P<date>.+)\,\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?escribió\s?:": "escribió",
103
+ r"(?m)^\s?Le\s?(?P<date>.+)\,\s?«(?P<from_name>.+)»\s*[\[|<](?P<from_address>.+)[\]|>]\s?a écrit\s?:": "a écrit",
104
+ r"(?m)^\s?(?P<from_name>.+)(?:\n\s*)?[\[|<](?P<from_address>.+)[\]|>]\s?kirjoitti\s?(?P<date>.+)\s?:": "kirjoitti",
105
+ r"(?m)^\s?(?P<date>.+)\s?időpontban\s?(?P<from_name>.+)(?:\n\s*)?[\[|<|(](?P<from_address>.+)[\]|>|)]\s?ezt írta\s?:": "időpontban",
106
+ r"(?m)^\s?Il giorno\s?(?P<date>.+)\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?ha scritto\s?:": "ha scritto",
107
+ r"(?m)^\s?Op\s?(?P<date>.+)\s?heeft\s?(?P<from_name>.+)(?:\n\s*)?[\[|<](?P<from_address>.+)[\]|>]\s?geschreven\s?:": "geschreven",
108
+ r"(?m)^\s?(?P<from_name>.+)(?:\n\s*)?[\[|<](?P<from_address>.+)[\]|>]\s?skrev følgende den\s?(?P<date>.+)\s?:": "skrev følgende den",
109
+ r"(?m)^\s?Dnia\s?(?P<date>.+)\s?„(?P<from_name>.+)”\s*[\[|<](?P<from_address>.+)[\]|>]\s?napisał\s?:": "napisał",
110
+ r"(?m)^\s?Em\s?(?P<date>.+)\,\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?escreveu\s?:": "escreveu",
111
+ r"(?m)^\s?(?P<date>.+)\s?пользователь\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?написал\s?:": "пользователь",
112
+ r"(?m)^\s?(?P<date>.+)\s?používateľ\s?(?P<from_name>.+)(?:\n\s*)?\([\[|<](?P<from_address>.+)[\]|>]\)\s?napísal\s?:": "používateľ",
113
+ r"(?m)^\s?Den\s?(?P<date>.+)\s?skrev\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\s?följande\s?:": "följande",
114
+ r"(?m)^\s?\"(?P<from_name>.+)\"\s*[\[|<](?P<from_address>.+)[\]|>]\,\s?(?P<date>.+)\s?tarihinde şunu yazdı\s?:": "tarihinde şunu yazdı",
115
+ r"(?m)^((?:\*\s*|[^\S\n]*)From\s?:\*?(.+))$": "From",
116
+ r"(?m)^([^\S\n]*Od\s?:(.+))$": "Od",
117
+ r"(?m)^([^\S\n]*Fra\s?:(.+))$": "Fra",
118
+ r"(?m)^([^\S\n]*Von\s?:(.+))$": "Von",
119
+ r"(?m)^([^\S\n]*De\s?:(.+))$": "De",
120
+ r"(?m)^([^\S\n]*Lähettäjä\s?:(.+))$": "Lähettäjä",
121
+ r"(?m)^([^\S\n]*Šalje\s?:(.+))$": "Šalje",
122
+ r"(?m)^([^\S\n]*Feladó\s?:(.+))$": "Feladó",
123
+ r"(?m)^([^\S\n]*Da\s?:(.+))$": "Da",
124
+ r"(?m)^([^\S\n]*Van\s?:(.+))$": "Van",
125
+ r"(?m)^([^\S\n]*Expeditorul\s?:(.+))$": "Expeditorul",
126
+ r"(?m)^([^\S\n]*Отправитель\s?:(.+))$": "Отправитель",
127
+ r"(?m)^([^\S\n]*Från\s?:(.+))$": "Från",
128
+ r"(?m)^([^\S\n]*Kimden\s?:(.+))$": "Kimden",
129
+ r"(?m)^([^\S\n]*Від кого\s?:(.+))$": "Від кого",
130
+ r"(?m)^([^\S\n]*Saatja\s?:(.+))$": "Saatja",
131
+ r"(?m)^([^\S\n]*De la\s?:(.+))$": "De la",
132
+ r"(?m)^([^\S\n]*Gönderen\s?:(.+))$": "Gönderen",
133
+ r"(?m)^([^\S\n]*От\s?:(.+))$": "От",
134
+ r"(?m)^([^\S\n]*Від\s?:(.+))$": "Від",
135
+ r"(?m)^([^\S\n]*Mittente\s?:(.+))$": "Mittente",
136
+ r"(?m)^([^\S\n]*Nadawca\s?:(.+))$": "Nadawca",
137
+ r"(?m)^([^\S\n]*de la\s?:(.+))$": "de la",
138
+ r"(?m)^([^\S\n]*送信元:(.+))$": "送信元:",
139
+ r"((?:(?<!\s)\s*)?From\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "From",
140
+ r"((?:(?<!\s)\s*)?Od\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Od",
141
+ r"((?:(?<!\s)\s*)?Fra\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Fra",
142
+ r"((?:(?<!\s)\s*)?Von\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Von",
143
+ r"((?:(?<!\s)\s*)?De\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "De",
144
+ r"((?:(?<!\s)\s*)?Lähettäjä\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Lähettäjä",
145
+ r"((?:(?<!\s)\s*)?Feladó\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Feladó",
146
+ r"((?:(?<!\s)\s*)?Da\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Da",
147
+ r"((?:(?<!\s)\s*)?Van\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Van",
148
+ r"((?:(?<!\s)\s*)?De la\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "De la",
149
+ r"((?:(?<!\s)\s*)?От\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "От",
150
+ r"((?:(?<!\s)\s*)?Från\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Från",
151
+ r"((?:(?<!\s)\s*)?Kimden\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Kimden",
152
+ r"((?:(?<!\s)\s*)?Від\s?:([^\S\n]|.+?(?<!\s))\s*[\[|<](.+?)[\]|>])": "Від",
153
+ r"(?m)^(?:\*\s*|[^\S\n]*)To\s?:\*?(.+)$": "To",
154
+ r"(?m)^[^\S\n]*Komu\s?:(.+)$": "Komu",
155
+ r"(?m)^[^\S\n]*Til\s?:(.+)$": "Til",
156
+ r"(?m)^[^\S\n]*An\s?:(.+)$": "An",
157
+ r"(?m)^[^\S\n]*Para\s?:(.+)$": "Para",
158
+ r"(?m)^[^\S\n]*Vastaanottaja\s?:(.+)$": "Vastaanottaja",
159
+ r"(?m)^[^\S\n]*À\s?:(.+)$": "À",
160
+ r"(?m)^[^\S\n]*Prima\s?:(.+)$": "Prima",
161
+ r"(?m)^[^\S\n]*Címzett\s?:(.+)$": "Címzett",
162
+ r"(?m)^[^\S\n]*A\s?:(.+)$": "A",
163
+ r"(?m)^[^\S\n]*Aan\s?:(.+)$": "Aan",
164
+ r"(?m)^[^\S\n]*Do\s?:(.+)$": "Do",
165
+ r"(?m)^[^\S\n]*Destinatarul\s?:(.+)$": "Destinatarul",
166
+ r"(?m)^[^\S\n]*Кому\s?:(.+)$": "Кому",
167
+ r"(?m)^[^\S\n]*Pre\s?:(.+)$": "Pre",
168
+ r"(?m)^[^\S\n]*Till\s?:(.+)$": "Till",
169
+ r"(?m)^[^\S\n]*Kime\s?:(.+)$": "Kime",
170
+ r"(?m)^[^\S\n]*Pour\s?:(.+)$": "Pour",
171
+ r"(?m)^[^\S\n]*Adresat\s?:(.+)$": "Adresat",
172
+ r"(?m)^[^\S\n]*送信先:(.+)$": "送信先:",
173
+ r"(?m)(?:(?<!\s)\s*)?To\s?:(.+)$": "To",
174
+ r"(?m)(?:(?<!\s)\s*)?Komu\s?:(.+)$": "Komu",
175
+ r"(?m)(?:(?<!\s)\s*)?Til\s?:(.+)$": "Til",
176
+ r"(?m)(?:(?<!\s)\s*)?An\s?:(.+)$": "An",
177
+ r"(?m)(?:(?<!\s)\s*)?Para\s?:(.+)$": "Para",
178
+ r"(?m)(?:(?<!\s)\s*)?Vastaanottaja\s?:(.+)$": "Vastaanottaja",
179
+ r"(?m)(?:(?<!\s)\s*)?À\s?:(.+)$": "À",
180
+ r"(?m)(?:(?<!\s)\s*)?Címzett\s?:(.+)$": "Címzett",
181
+ r"(?m)(?:(?<!\s)\s*)?A\s?:(.+)$": "A",
182
+ r"(?m)(?:(?<!\s)\s*)?Aan\s?:(.+)$": "Aan",
183
+ r"(?m)(?:(?<!\s)\s*)?Do\s?:(.+)$": "Do",
184
+ r"(?m)(?:(?<!\s)\s*)?Către\s?:(.+)$": "Către",
185
+ r"(?m)(?:(?<!\s)\s*)?Кому\s?:(.+)$": "Кому",
186
+ r"(?m)(?:(?<!\s)\s*)?Till\s?:(.+)$": "Till",
187
+ r"(?m)(?:(?<!\s)\s*)?Kime\s?:(.+)$": "Kime",
188
+ r"(?m)^[^\S\n]*Reply-To\s?:(.+)$": "Reply-To",
189
+ r"(?m)^[^\S\n]*Odgovori na\s?:(.+)$": "Odgovori na",
190
+ r"(?m)^[^\S\n]*Odpověď na\s?:(.+)$": "Odpověď na",
191
+ r"(?m)^[^\S\n]*Svar til\s?:(.+)$": "Svar til",
192
+ r"(?m)^[^\S\n]*Antwoord aan\s?:(.+)$": "Antwoord aan",
193
+ r"(?m)^[^\S\n]*Vastaus\s?:(.+)$": "Vastaus",
194
+ r"(?m)^[^\S\n]*Répondre à\s?:(.+)$": "Répondre à",
195
+ r"(?m)^[^\S\n]*Antwort an\s?:(.+)$": "Antwort an",
196
+ r"(?m)^[^\S\n]*Válaszcím\s?:(.+)$": "Válaszcím",
197
+ r"(?m)^[^\S\n]*Rispondi a\s?:(.+)$": "Rispondi a",
198
+ r"(?m)^[^\S\n]*Odpowiedź-do\s?:(.+)$": "Odpowiedź-do",
199
+ r"(?m)^[^\S\n]*Responder A\s?:(.+)$": "Responder A",
200
+ r"(?m)^[^\S\n]*Responder a\s?:(.+)$": "Responder a",
201
+ r"(?m)^[^\S\n]*Răspuns către\s?:(.+)$": "Răspuns către",
202
+ r"(?m)^[^\S\n]*Ответ-Кому\s?:(.+)$": "Ответ-Кому",
203
+ r"(?m)^[^\S\n]*Odpovedať-Pre\s?:(.+)$": "Odpovedať-Pre",
204
+ r"(?m)^[^\S\n]*Svara till\s?:(.+)$": "Svara till",
205
+ r"(?m)^[^\S\n]*Yanıt Adresi\s?:(.+)$": "Yanıt Adresi",
206
+ r"(?m)^[^\S\n]*Кому відповісти\s?:(.+)$": "Кому відповісти",
207
+ r"(?m)^(?:\*\s*|[^\S\n]*)Cc\s?:\*?(.+)$": "Cc",
208
+ r"(?m)^[^\S\n]*CC\s?:(.+)$": "CC",
209
+ r"(?m)^[^\S\n]*Kopie\s?:(.+)$": "Kopie",
210
+ r"(?m)^[^\S\n]*Kopio\s?:(.+)$": "Kopio",
211
+ r"(?m)^[^\S\n]*Másolat\s?:(.+)$": "Másolat",
212
+ r"(?m)^[^\S\n]*Kopi\s?:(.+)$": "Kopi",
213
+ r"(?m)^[^\S\n]*Dw\s?:(.+)$": "Dw",
214
+ r"(?m)^[^\S\n]*Копия\s?:(.+)$": "Копия",
215
+ r"(?m)^[^\S\n]*Kopia\s?:(.+)$": "Kopia",
216
+ r"(?m)^[^\S\n]*Bilgi\s?:(.+)$": "Bilgi",
217
+ r"(?m)^[^\S\n]*Копія\s?:(.+)$": "Копія",
218
+ r"(?m)^[^\S\n]*Másolatot kap\s?:(.+)$": "Másolatot kap",
219
+ r"(?m)^[^\S\n]*Kópia\s?:(.+)$": "Kópia",
220
+ r"(?m)^[^\S\n]*DW\s?:(.+)$": "DW",
221
+ r"(?m)^[^\S\n]*Kopie \(CC\)\s?:(.+)$": "Kopie (CC)",
222
+ r"(?m)^[^\S\n]*Copie à\s?:(.+)$": "Copie à",
223
+ r"(?m)^[^\S\n]*CC:(.+)$": "CC:",
224
+ r"(?m)(?:(?<!\s)\s*)?Cc\s?:(.+)$": "Cc",
225
+ r"(?m)(?:(?<!\s)\s*)?CC\s?:(.+)$": "CC",
226
+ r"(?m)(?:(?<!\s)\s*)?Kopie\s?:(.+)$": "Kopie",
227
+ r"(?m)(?:(?<!\s)\s*)?Kopio\s?:(.+)$": "Kopio",
228
+ r"(?m)(?:(?<!\s)\s*)?Másolat\s?:(.+)$": "Másolat",
229
+ r"(?m)(?:(?<!\s)\s*)?Kopi\s?:(.+)$": "Kopi",
230
+ r"(?m)(?:(?<!\s)\s*)?Dw\s?:(.+)$": "Dw",
231
+ r"(?m)(?:(?<!\s)\s*)?Копия\s?:(.+)$": "Копия",
232
+ r"(?m)(?:(?<!\s)\s*)?Kópia\s?:(.+)$": "Kópia",
233
+ r"(?m)(?:(?<!\s)\s*)?Kopia\s?:(.+)$": "Kopia",
234
+ r"(?m)(?:(?<!\s)\s*)?Копія\s?:(.+)$": "Копія",
235
+ r"(?m)^[^\S\n]*Date\s?:(.+)$": "Date",
236
+ r"(?m)^[^\S\n]*Datum\s?:(.+)$": "Datum",
237
+ r"(?m)^[^\S\n]*Dato\s?:(.+)$": "Dato",
238
+ r"(?m)^[^\S\n]*Envoyé\s?:(.+)$": "Envoyé",
239
+ r"(?m)^[^\S\n]*Fecha\s?:(.+)$": "Fecha",
240
+ r"(?m)^[^\S\n]*Päivämäärä\s?:(.+)$": "Päivämäärä",
241
+ r"(?m)^[^\S\n]*Dátum\s?:(.+)$": "Dátum",
242
+ r"(?m)^[^\S\n]*Data\s?:(.+)$": "Data",
243
+ r"(?m)^[^\S\n]*Dată\s?:(.+)$": "Dată",
244
+ r"(?m)^[^\S\n]*Дата\s?:(.+)$": "Дата",
245
+ r"(?m)^[^\S\n]*Tarih\s?:(.+)$": "Tarih",
246
+ r"(?m)^(?:\*\s*|[^\S\n]*)Sent\s?:\*?(.+)$": "Sent",
247
+ r"(?m)^[^\S\n]*Päiväys\s?:(.+)$": "Päiväys",
248
+ r"(?m)^[^\S\n]*日付:(.+)$": "日付:",
249
+ r"(?m)(?:(?<!\s)\s*)?Datum\s?:(.+)$": "Datum",
250
+ r"(?m)(?:(?<!\s)\s*)?Sendt\s?:(.+)$": "Sendt",
251
+ r"(?m)(?:(?<!\s)\s*)?Gesendet\s?:(.+)$": "Gesendet",
252
+ r"(?m)(?:(?<!\s)\s*)?Sent\s?:(.+)$": "Sent",
253
+ r"(?m)(?:(?<!\s)\s*)?Enviado\s?:(.+)$": "Enviado",
254
+ r"(?m)(?:(?<!\s)\s*)?Envoyé\s?:(.+)$": "Envoyé",
255
+ r"(?m)(?:(?<!\s)\s*)?Lähetetty\s?:(.+)$": "Lähetetty",
256
+ r"(?m)(?:(?<!\s)\s*)?Elküldve\s?:(.+)$": "Elküldve",
257
+ r"(?m)(?:(?<!\s)\s*)?Inviato\s?:(.+)$": "Inviato",
258
+ r"(?m)(?:(?<!\s)\s*)?Verzonden\s?:(.+)$": "Verzonden",
259
+ r"(?m)(?:(?<!\s)\s*)?Wysłano\s?:(.+)$": "Wysłano",
260
+ r"(?m)(?:(?<!\s)\s*)?Trimis\s?:(.+)$": "Trimis",
261
+ r"(?m)(?:(?<!\s)\s*)?Отправлено\s?:(.+)$": "Отправлено",
262
+ r"(?m)(?:(?<!\s)\s*)?Odoslané\s?:(.+)$": "Odoslané",
263
+ r"(?m)(?:(?<!\s)\s*)?Skickat\s?:(.+)$": "Skickat",
264
+ r"(?m)(?:(?<!\s)\s*)?Gönderilen\s?:(.+)$": "Gönderilen",
265
+ r"(?m)(?:(?<!\s)\s*)?Відправлено\s?:(.+)$": "Відправлено",
266
+ r"^\s?\n?\s*<[^<>]+?<mailto\:(.+?)>>": "<mailto:",
267
+ r"^([^\S\n]|.+?(?<!\s))\s*<[^<>]+?<mailto\:(.+?)>>": "<mailto:",
268
+ r"^([^\S\n]|.+?(?<!\s))\s*[\[|<]mailto\:(.+?)[\]|>]": "mailto:",
269
+ r"^\'(.+?)\'\s?\n?\s*[\[|<](.+?)[\]|>]": "'",
270
+ r"^\"\'(.+?)\'\"\s?\n?\s*[\[|<](.+?)[\]|>]": "\"'",
271
+ r"^\"(.+?)\"\s?\n?\s*[\[|<](.+?)[\]|>]": '"',
272
+ r"^([^\s@,;]+@[^\s@,;]+\.[^\s@,;]+)": "@",
273
+ r"^(([^\s@,;<>]+)@([^\s@,;<>]{2,}))$": "@",
274
+ }
@@ -1,12 +1,24 @@
1
1
  from collections.abc import Sequence
2
2
  from re import Match, Pattern
3
3
 
4
+ from .literals import REQUIRED_LITERALS
4
5
  from .utils import split_with_regexp
5
6
 
6
7
 
8
+ def may_match(regex: Pattern[str], string: str) -> bool:
9
+ """False when `string` lacks text that every match of `regex` contains.
10
+
11
+ Most patterns cannot match a given string, and each would otherwise scan all of it.
12
+ """
13
+ literal = REQUIRED_LITERALS.get(regex.pattern)
14
+ return literal is None or literal in string
15
+
16
+
7
17
  def loop_regexes_replace(regexes: Sequence[Pattern[str]], string: str) -> str:
8
18
  match = string
9
19
  for regex in regexes:
20
+ if not may_match(regex, string):
21
+ continue
10
22
  current_match = regex.sub("", string)
11
23
  if len(current_match) < len(match):
12
24
  match = current_match
@@ -19,6 +31,8 @@ def loop_regexes_split(
19
31
  ) -> list[str]:
20
32
  match: list[str] = []
21
33
  for regex in regexes:
34
+ if not may_match(regex, string):
35
+ continue
22
36
  current_match = split_with_regexp(regex, string)
23
37
  if len(current_match) > 1:
24
38
  if highest_position:
@@ -39,6 +53,8 @@ def loop_regexes_match(
39
53
  match_index = 0
40
54
 
41
55
  for regex in regexes:
56
+ if not may_match(regex, string):
57
+ continue
42
58
  current_match = regex.search(string)
43
59
  if not current_match: # style note
44
60
  continue
@@ -708,4 +708,8 @@ MAILBOX = [
708
708
  re.compile(r"^([^;](?:[^\S\n]|.+?(?<!\s)))\s*[\[|<](.+?)[\]|>]"),
709
709
  ]
710
710
 
711
+ # The first three MAILBOX patterns need "mailto:". A recipients line only loses text as its
712
+ # mailboxes are read, so one without "mailto:" never gains it: parse_mailbox leaves them out.
713
+ MAILBOX_WITHOUT_MAILTO = MAILBOX[3:]
714
+
711
715
  MAILBOX_ADDRESS = [re.compile(r"^(([^\s@,;<>]+)@([^\s@,;<>]{2,}))$")]
@@ -30,8 +30,18 @@ def decode_encoded_words(match: Match[str]) -> str:
30
30
  return words
31
31
 
32
32
 
33
+ def remove_non_graphic(s: str) -> str:
34
+ """Drop every character is_graphic() rejects.
35
+
36
+ Each distinct character is judged once: a body has thousands of characters but
37
+ rarely more than a few hundred distinct ones, and str.translate() does the rest.
38
+ """
39
+ rejected = {ord(char): None for char in set(s) if not is_graphic(char)}
40
+ return s.translate(rejected) if rejected else s
41
+
42
+
33
43
  def preprocess_string(s: str) -> str:
34
- s = "".join(char for char in s if is_graphic(char))
44
+ s = remove_non_graphic(s)
35
45
  s = s.replace("\ufeff", "")
36
46
  # Decode encoded words in place. email.header.decode_header() on the whole
37
47
  # text would join every line into one (so no line-anchored pattern could
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "email-forward-parser"
7
- version = "0.2.1"
7
+ version = "0.2.3"
8
8
  description = "Python Library to parse forwarded email messages"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.14"
@@ -14,6 +14,7 @@ keywords = ["email", "forwarded-email", "parser", "eml", "mime"]
14
14
  classifiers = [
15
15
  "Programming Language :: Python :: 3",
16
16
  "Programming Language :: Python :: 3.14",
17
+ "Programming Language :: Python :: 3.15",
17
18
  "License :: OSI Approved :: Apache Software License",
18
19
  "Topic :: Communications :: Email",
19
20
  "Topic :: Text Processing",
@@ -51,7 +52,7 @@ show_missing = true
51
52
 
52
53
  [tool.black]
53
54
  line-length = 100
54
- target-version = ["py314"]
55
+ target-version = ["py314", "py315"]
55
56
 
56
57
  [tool.isort]
57
58
  combine_as_imports = true
@@ -69,6 +70,8 @@ select = ["E", "F", "I", "UP", "B", "SIM", "RUF"]
69
70
  [tool.ruff.lint.per-file-ignores]
70
71
  # These are executable multilingual fixtures; rewriting locale punctuation changes matching.
71
72
  "emailforwardparser/regexs.py" = ["E501", "RUF001"]
73
+ # Generated from regexs.py, with its patterns as keys.
74
+ "emailforwardparser/literals.py" = ["E501", "RUF001"]
72
75
 
73
76
  [tool.mypy]
74
77
  python_version = "3.14"
@@ -406,6 +406,16 @@ def test_utils_regex_helpers_cover_empty_limited_and_duplicate_splits() -> None:
406
406
  assert utils.is_graphic("\n") is True
407
407
 
408
408
 
409
+ def test_remove_non_graphic_keeps_text_and_whitespace_and_drops_the_rest() -> None:
410
+ kept = "Café 中文 🙂\n\r\t \u00a0\u2028\u2029¶€\u0301"
411
+ # Controls, soft hyphen, zero-width space, BOM, a lone surrogate, private use, unassigned.
412
+ dropped = "\x00\x07\x1b\x7f\x85\u00ad\u200b\ufeff\ud800\ue000\U000f0000\U0010ffff"
413
+
414
+ assert utils.remove_non_graphic(kept) is kept
415
+ assert utils.remove_non_graphic(dropped + kept[:6] + dropped + kept[6:] + dropped) == kept
416
+ assert utils.remove_non_graphic(dropped) == ""
417
+
418
+
409
419
  def test_parser_unusual_empty_match_paths() -> None:
410
420
  with patch(
411
421
  "emailforwardparser.forward_parser.parse_mailbox",
@@ -0,0 +1,178 @@
1
+ """Checks emailforwardparser/literals.py against the patterns in regexs.py, and writes it:
2
+
3
+ python -m tests.literals_test && black emailforwardparser/literals.py
4
+
5
+ A pattern is skipped when its literal is not in the string, so the table may only name text that
6
+ every match of the pattern contains. That is read here from the standard library's regex parser,
7
+ which is private to `re`: it writes and checks the table, and never runs in the library.
8
+ """
9
+
10
+ import ast
11
+ import importlib
12
+ import re
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ import pytest
17
+
18
+ from emailforwardparser import forward_parser as fp, literals, loop, regexs
19
+ from emailforwardparser.literals import REQUIRED_LITERALS
20
+
21
+ sre = importlib.import_module("re._constants")
22
+ sre_parse = importlib.import_module("re._parser")
23
+
24
+ # Every list of patterns in regexs.py; the loop helpers only ever get these lists.
25
+ PATTERN_LISTS = {
26
+ name: value
27
+ for name, value in vars(regexs).items()
28
+ if isinstance(value, list) and value and all(isinstance(item, re.Pattern) for item in value)
29
+ }
30
+ PATTERNS = [pattern for patterns in PATTERN_LISTS.values() for pattern in patterns]
31
+ REPEATS = (sre.MAX_REPEAT, sre.MIN_REPEAT, sre.POSSESSIVE_REPEAT)
32
+
33
+
34
+ def required_literal(pattern: re.Pattern[str]) -> str:
35
+ """The longest run of literal text every match of `pattern` contains ("" when none is sure).
36
+
37
+ Only items every match goes through count: literals at the top level, in groups and in
38
+ repeats of at least one. A branch, an optional repeat, a lookaround, a character class or a
39
+ case-insensitive pattern or group ends the run and adds nothing.
40
+ """
41
+ if pattern.flags & re.IGNORECASE:
42
+ return ""
43
+ best = ""
44
+
45
+ def walk(items: Any) -> None:
46
+ nonlocal best
47
+ run = ""
48
+ for op, argument in items:
49
+ if op is sre.LITERAL:
50
+ run += chr(argument)
51
+ continue
52
+ best = max(best, run, key=len)
53
+ run = ""
54
+ if op is sre.SUBPATTERN and not argument[1] & re.IGNORECASE:
55
+ walk(argument[3])
56
+ elif op in REPEATS and argument[0] >= 1:
57
+ walk(argument[2])
58
+ elif op is sre.ATOMIC_GROUP:
59
+ walk(argument)
60
+ best = max(best, run, key=len)
61
+
62
+ walk(sre_parse.parse(pattern.pattern, pattern.flags))
63
+ return best
64
+
65
+
66
+ def derived_table() -> dict[str, str]:
67
+ table: dict[str, str] = {}
68
+ for pattern in PATTERNS:
69
+ literal = required_literal(pattern)
70
+ if literal:
71
+ table[pattern.pattern] = literal
72
+ return table
73
+
74
+
75
+ def python_string(text: str) -> str:
76
+ """`text` as a raw string literal, as regexs.py writes its patterns, where one can hold it."""
77
+ raw = f'r"{text}"'
78
+ try:
79
+ if ast.literal_eval(raw) == text:
80
+ return raw
81
+ except SyntaxError:
82
+ pass
83
+ return repr(text)
84
+
85
+
86
+ def render() -> str:
87
+ lines = [
88
+ '"""Generated by tests/literals_test.py from regexs.py: do not edit by hand.',
89
+ "",
90
+ "For each pattern in regexs.py's lists, text that every match of it contains. A string",
91
+ "without that text cannot match, so the loop helpers do not run the pattern on it.",
92
+ '"""',
93
+ "",
94
+ "# Keyed by pattern source: every pattern in regexs.py carries its flags inline.",
95
+ "REQUIRED_LITERALS: dict[str, str] = {",
96
+ ]
97
+ table = derived_table()
98
+ lines += [f" {python_string(source)}: {literal!r}," for source, literal in table.items()]
99
+ lines.append("}")
100
+ return "\n".join(lines) + "\n"
101
+
102
+
103
+ def test_the_table_is_what_the_patterns_require() -> None:
104
+ assert derived_table() == REQUIRED_LITERALS
105
+
106
+
107
+ def test_a_pattern_source_says_everything_the_table_keys_on() -> None:
108
+ for pattern in PATTERNS:
109
+ assert re.compile(pattern.pattern).flags == pattern.flags
110
+
111
+
112
+ @pytest.mark.parametrize(
113
+ ("pattern", "literal"),
114
+ [
115
+ (r"(?m)^Fw:(.*)", "Fw:"),
116
+ (r"(?m)^(?:>\s*|[^\S\n]*)Begin forwarded message\s?:", "Begin forwarded message"),
117
+ (r"^\s?\n?\s*<[^<>]+?<mailto\:(.+?)>>", "<mailto:"),
118
+ (r"(?m)^Redir.:(.*)", "Redir"),
119
+ (r"a(?:bc)+d", "bc"),
120
+ (r"a(?:bcd)*e", "a"),
121
+ (r"(?:ab|cd)e", "e"),
122
+ (r"(?:abc|abd)e", "ab"),
123
+ (r"x(?=abcd)", "x"),
124
+ (r"(?>abcd)e", "abcd"),
125
+ (r"(?i:abcd)e", "e"),
126
+ (r"(?i)abcd", ""),
127
+ (r"[ab]", ""),
128
+ ],
129
+ )
130
+ def test_required_literal_takes_only_text_every_match_has(pattern: str, literal: str) -> None:
131
+ assert required_literal(re.compile(pattern)) == literal
132
+
133
+
134
+ def test_a_string_without_the_literal_skips_the_pattern() -> None:
135
+ pattern = regexs.SUBJECT[0]
136
+ assert REQUIRED_LITERALS[pattern.pattern] == "Fw:"
137
+
138
+ assert not loop.may_match(pattern, "Fwd: news")
139
+ assert loop.may_match(pattern, "Fw: news")
140
+ assert loop.may_match(re.compile(r"(?i)fw:"), "FW: news")
141
+
142
+
143
+ def test_only_the_first_three_mailbox_patterns_need_mailto() -> None:
144
+ assert all("mailto:" in REQUIRED_LITERALS[pattern.pattern] for pattern in regexs.MAILBOX[:3])
145
+ assert all(
146
+ "mailto:" not in REQUIRED_LITERALS.get(pattern.pattern, "")
147
+ for pattern in regexs.MAILBOX_WITHOUT_MAILTO
148
+ )
149
+ assert regexs.MAILBOX[3:] == regexs.MAILBOX_WITHOUT_MAILTO
150
+
151
+
152
+ @pytest.mark.parametrize(
153
+ ("line", "mailboxes"),
154
+ [
155
+ (
156
+ "To: Ann Lee <ann@example.com>, Bob <bob@example.com>",
157
+ [("Ann Lee", "ann@example.com"), ("Bob", "bob@example.com")],
158
+ ),
159
+ (
160
+ "To: Bob <mailto:bob@example.com>; Ann Lee <ann@example.com>",
161
+ [("Bob", "bob@example.com"), ("Ann Lee", "ann@example.com")],
162
+ ),
163
+ (
164
+ "To: <ann@example.com<mailto:ann@example.com>>, Bob [bob@example.com]",
165
+ [("", "ann@example.com"), ("Bob", "bob@example.com")],
166
+ ),
167
+ ],
168
+ )
169
+ def test_recipient_lines_with_and_without_mailto(
170
+ line: str, mailboxes: list[tuple[str, str]]
171
+ ) -> None:
172
+ parsed = fp.parse_mailbox(regexs.ORIGINAL_TO, line)
173
+
174
+ assert [(mailbox.name, mailbox.address) for mailbox in parsed] == mailboxes
175
+
176
+
177
+ if __name__ == "__main__":
178
+ Path(literals.__file__).write_text(render(), encoding="utf-8")
@@ -181,7 +181,7 @@ wheels = [
181
181
 
182
182
  [[package]]
183
183
  name = "email-forward-parser"
184
- version = "0.2.1"
184
+ version = "0.2.3"
185
185
  source = { editable = "." }
186
186
 
187
187
  [package.dev-dependencies]