Skip to content

Commit 8589ff0

Browse files
committed
fix implement for ses.py
1 parent 0e567cc commit 8589ff0

File tree

1 file changed

+132
-56
lines changed
  • aws_lambda_powertools/event_handler/async_execution/routes

1 file changed

+132
-56
lines changed

aws_lambda_powertools/event_handler/async_execution/routes/ses.py

Lines changed: 132 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,56 @@
77
from .base import BaseRoute
88

99

10+
def match_prefix(all_address: list[str], all_prefix: list[str]) -> bool:
11+
for address in all_address:
12+
for prefix in all_prefix:
13+
if address.find(prefix) == 0:
14+
return True
15+
return False
16+
17+
18+
def match_suffix(all_address: list[str], all_suffix: list[str]) -> bool:
19+
for address in all_address:
20+
for suffix in all_suffix:
21+
length_suffix = len(suffix)
22+
address_suffix = address[-length_suffix:]
23+
if address_suffix == suffix:
24+
return True
25+
return False
26+
27+
28+
def match_prefix_and_suffix(all_address: list[str], all_prefix: list[str], all_suffix: list[str]) -> bool:
29+
for address in all_address:
30+
for prefix in all_prefix:
31+
for suffix in all_suffix:
32+
length_suffix = len(suffix)
33+
address_suffix = address[-length_suffix:]
34+
if address.find(prefix) == 0 and address_suffix == suffix:
35+
return True
36+
return False
37+
38+
1039
class SESRoute(BaseRoute):
1140
mail_to: list[str] | None
41+
mail_to_prefix: list[str] | None
42+
mail_to_suffix: list[str] | None
1243
mail_from: list[str] | None
44+
mail_from_prefix: list[str] | None
45+
mail_from_suffix: list[str] | None
1346
mail_subject: str | None
47+
mail_subject_prefix: str | None
1448

1549
def __init__(
1650
self,
1751
func: Callable,
1852
mail_to: str | list[str] | None = None,
53+
mail_to_prefix: str | list[str] | None = None,
54+
mail_to_suffix: str | list[str] | None = None,
1955
mail_from: str | list[str] | None = None,
56+
mail_from_prefix: str | list[str] | None = None,
57+
mail_from_suffix: str | list[str] | None = None,
2058
mail_subject: str | None = None,
59+
mail_subject_prefix: str | None = None,
2160
):
2261
self.func = func
2362

@@ -26,76 +65,92 @@ def __init__(
2665
else:
2766
self.mail_to = mail_to
2867

68+
if isinstance(mail_to_prefix, str):
69+
self.mail_to_prefix = [mail_to_prefix]
70+
else:
71+
self.mail_to_prefix = mail_to_prefix
72+
73+
if isinstance(mail_to_suffix, str):
74+
self.mail_to_suffix = [mail_to_suffix]
75+
else:
76+
self.mail_to_suffix = mail_to_suffix
77+
2978
if isinstance(mail_from, str):
3079
self.mail_from = [mail_from]
3180
else:
3281
self.mail_from = mail_from
3382

83+
if isinstance(mail_from_prefix, str):
84+
self.mail_from_prefix = [mail_from_prefix]
85+
else:
86+
self.mail_from_prefix = mail_from_prefix
87+
88+
if isinstance(mail_from_suffix, str):
89+
self.mail_from_suffix = [mail_from_suffix]
90+
else:
91+
self.mail_from_suffix = mail_from_suffix
92+
3493
self.mail_subject = mail_subject
94+
self.mail_subject_prefix = mail_subject_prefix
3595

3696
if not self.mail_to and not self.mail_from and not self.mail_subject:
37-
raise ValueError("mail_to, mail_from, or mail_subject must be not null")
97+
raise ValueError(
98+
(
99+
"mail_to, mail_to_prefix, mail_to_suffix, mail_from, mail_from_prefix, mail_from_suffix, "
100+
"mail_subject, or mail_subject_prefix must be not null"
101+
),
102+
)
38103

39104
def is_target_with_mail_to(self, mail_to: list[str] | None) -> bool:
40-
if not mail_to or not self.mail_to:
105+
if not mail_to:
41106
return False
42-
43-
for address in self.mail_to:
44-
if address in mail_to:
45-
return True
107+
elif self.mail_to:
108+
for address in mail_to:
109+
if address in self.mail_to:
110+
return True
111+
elif self.mail_to_prefix and self.mail_to_suffix:
112+
return match_prefix_and_suffix(
113+
all_address=mail_to,
114+
all_prefix=self.mail_to_prefix,
115+
all_suffix=self.mail_to_suffix,
116+
)
117+
elif self.mail_to_prefix:
118+
return match_prefix(all_address=mail_to, all_prefix=self.mail_to_prefix)
119+
elif self.mail_to_suffix:
120+
return match_suffix(all_address=mail_to, all_suffix=self.mail_to_suffix)
46121

47122
return False
48123

49124
def is_target_with_mail_from(self, mail_from: list[str] | None) -> bool:
50-
if not mail_from or not self.mail_from:
125+
if not mail_from:
51126
return False
52-
53-
for address in self.mail_from:
54-
if address in mail_from:
55-
return True
127+
elif self.mail_from:
128+
for address in mail_from:
129+
if address in self.mail_from:
130+
return True
131+
elif self.mail_from_prefix and self.mail_from_suffix:
132+
return match_prefix_and_suffix(
133+
all_address=mail_from,
134+
all_prefix=self.mail_from_prefix,
135+
all_suffix=self.mail_from_suffix,
136+
)
137+
elif self.mail_from_prefix:
138+
return match_prefix(all_address=mail_from, all_prefix=self.mail_from_prefix)
139+
elif self.mail_from_suffix:
140+
return match_suffix(all_address=mail_from, all_suffix=self.mail_from_suffix)
56141

57142
return False
58143

59144
def is_target_with_mail_subject(self, mail_subject: str | None) -> bool:
60-
if mail_subject and self.mail_subject:
145+
if not mail_subject:
146+
return False
147+
elif self.mail_subject:
61148
return self.mail_subject == mail_subject
149+
elif self.mail_subject_prefix:
150+
return mail_subject.find(self.mail_subject_prefix) == 0
62151
else:
63152
return False
64153

65-
def is_target(self, mail_to: list[str] | None, mail_from: list[str] | None, mail_subject: str | None) -> bool:
66-
flag_mail_to = self.is_target_with_mail_to(mail_to=mail_to)
67-
flag_mail_from = self.is_target_with_mail_from(mail_from=mail_from)
68-
flag_mail_subject = self.is_target_with_mail_subject(mail_subject=mail_subject)
69-
70-
if mail_to and mail_from and mail_subject:
71-
text = "mail_to, mail_from, mail_subject"
72-
elif mail_to and mail_from and not mail_subject:
73-
text = "mail_to, mail_from"
74-
elif mail_to and not mail_from and mail_subject:
75-
text = "mail_to, mail_subject"
76-
elif not mail_to and mail_from and mail_subject:
77-
text = "mail_from, mail_subject"
78-
elif mail_to and not mail_from and not mail_subject:
79-
text = "mail_to"
80-
elif not mail_to and mail_from and not mail_subject:
81-
text = "mail_from"
82-
elif not mail_to and not mail_from and mail_subject:
83-
text = "mail_subject"
84-
else: # not mail_to and not mail_from and not mail_subject
85-
text = ""
86-
87-
mapping = {
88-
"mail_to, mail_from, mail_subject": flag_mail_to and flag_mail_from and flag_mail_subject,
89-
"mail_to, mail_from": flag_mail_to and flag_mail_from,
90-
"mail_to, mail_subject": flag_mail_to and flag_mail_subject,
91-
"mail_from, mail_subject": flag_mail_from and flag_mail_subject,
92-
"mail_to": flag_mail_to,
93-
"mail_from": flag_mail_from,
94-
"mail_subject": flag_mail_subject,
95-
"": False,
96-
}
97-
return mapping[text]
98-
99154
def match(self, event: dict[str, Any]) -> tuple[Callable, SESEvent] | None:
100155
if not isinstance(event, dict):
101156
return None
@@ -110,22 +165,43 @@ def match(self, event: dict[str, Any]) -> tuple[Callable, SESEvent] | None:
110165
if common_header is None:
111166
return None
112167

113-
if self.mail_to:
114-
mail_to = common_header.get("to")
115-
else:
168+
mail_to = common_header.get("to")
169+
mail_from = common_header.get("from")
170+
mail_subject = common_header.get("subject")
171+
172+
if not self.mail_to and not self.mail_to_prefix and not self.mail_to_suffix:
116173
mail_to = None
117174

118-
if self.mail_from:
119-
mail_from = common_header.get("from")
120-
else:
175+
if not self.mail_from and not self.mail_from_prefix and not self.mail_from_suffix:
121176
mail_from = None
122177

123-
if self.mail_subject:
124-
mail_subject = common_header.get("subject")
125-
else:
178+
if not self.mail_subject and not self.mail_subject_prefix:
126179
mail_subject = None
127180

128-
if self.is_target(mail_to, mail_from, mail_subject):
181+
flag_mail_to = self.is_target_with_mail_to(mail_to=mail_to)
182+
flag_mail_from = self.is_target_with_mail_from(mail_from=mail_from)
183+
flag_mail_subject = self.is_target_with_mail_subject(mail_subject=mail_subject)
184+
185+
text = ", ".join(
186+
[
187+
"mail_to: x" if mail_to is None else "mail_to: o",
188+
"mail_from: x" if mail_from is None else "mail_from: o",
189+
"mail_subject: x" if mail_subject is None else "mail_subject: o",
190+
],
191+
)
192+
193+
mapping = {
194+
"mail_to: o, mail_from: o, mail_subject: o": flag_mail_to and flag_mail_from and flag_mail_subject,
195+
"mail_to: o, mail_from: o, mail_subject: x": flag_mail_to and flag_mail_from,
196+
"mail_to: o, mail_from: x, mail_subject: o": flag_mail_to and flag_mail_subject,
197+
"mail_to: x, mail_from: o, mail_subject: o": flag_mail_from and flag_mail_subject,
198+
"mail_to: o, mail_from: x, mail_subject: x": flag_mail_to,
199+
"mail_to: x, mail_from: o, mail_subject: x": flag_mail_from,
200+
"mail_to: x, mail_from: x, mail_subject: o": flag_mail_subject,
201+
"mail_to: x, mail_from: x, mail_subject: x": False,
202+
}
203+
204+
if mapping[text]:
129205
return self.func, SESEvent(event)
130206
else:
131207
return None

0 commit comments

Comments
 (0)