Skip to content

Commit 3a0b916

Browse files
committed
move template_data from tuple to Mail
1 parent c0aac85 commit 3a0b916

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

intelmq/bots/outputs/smtp_batch/output.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
from tempfile import NamedTemporaryFile
1010
import time
11-
from typing import Any, Optional
11+
from typing import Any, Generator, Optional
1212
import zipfile
1313
from base64 import b64decode
1414
from collections import OrderedDict
@@ -49,6 +49,7 @@ class Mail:
4949
to: str
5050
path: str
5151
count: int
52+
template_data: dict[str,Any]
5253

5354

5455
class SMTPBatchOutputBot(Bot):
@@ -164,7 +165,7 @@ def cli_run(self):
164165

165166
print("Preparing mail queue...")
166167
self.timeout = []
167-
mails = [m for m in self.prepare_mails() if m[0]]
168+
mails = [m for m in self.prepare_mails() if m]
168169

169170
print("")
170171
if self.limit_results:
@@ -205,9 +206,7 @@ def cli_run(self):
205206
count = 0
206207
exit_code = 0
207208
for mail in mails:
208-
template_data = mail[1]
209-
mail = mail[0]
210-
succ = self.build_mail(mail, send=True, template_data=template_data)
209+
succ = self.build_mail(mail, send=True)
211210
if not succ:
212211
exit_code = 1
213212
else:
@@ -228,7 +227,6 @@ def cli_run(self):
228227
sys.exit(exit_code)
229228
elif i == "clear":
230229
for mail in mails:
231-
mail = mail[0]
232230
self.cache.redis.delete(mail.key)
233231
print("Queue cleared.")
234232
sys.exit(0)
@@ -240,7 +238,6 @@ def cli_run(self):
240238
self.send_mails_to_tester(mails)
241239
else:
242240
for mail in mails:
243-
mail = mail[0]
244241
if mail.to == i:
245242
self.send_mails_to_tester([mail])
246243
break
@@ -259,10 +256,10 @@ def send_mails_to_tester(self, mails):
259256
:param mails: list
260257
"""
261258
self.set_tester(False)
262-
count = sum([1 for mail in mails if self.build_mail(mail[0], send=True, override_to=self.testing_to, template_data=mail[1])])
259+
count = sum([1 for mail in mails if self.build_mail(mail, send=True, override_to=self.testing_to)])
263260
print(f"{count}× mail sent to: {self.testing_to}\n")
264261

265-
def prepare_mails(self):
262+
def prepare_mails(self) -> Generator[Mail]:
266263
""" Generates Mail objects """
267264

268265
for mail_record in self.cache.redis.keys(f"{self.key}*")[slice(self.limit_results)]:
@@ -334,11 +331,14 @@ def prepare_mails(self):
334331

335332
# collect all data which must be the same for all events of the
336333
# bucket and thus can be used for templating
337-
template_data = {
338-
k.replace(".", "_"): lines[0][k]
339-
for k in ["source.abuse_contact"] + self.additional_grouping_keys
340-
if k in rows_output[0]
341-
}
334+
template_data = {}
335+
# only collect if templating is enabled (save the memory otherwise)
336+
if jinja2 and self.templating and any(self.templating.values()):
337+
template_data = {
338+
k.replace(".", "_"): lines[0][k]
339+
for k in ["source.abuse_contact"] + self.additional_grouping_keys
340+
if k in rows_output[0]
341+
}
342342

343343
email_to = template_data["source_abuse_contact"]
344344
filename = f'{time.strftime("%y%m%d")}_{count}_events'
@@ -355,12 +355,12 @@ def prepare_mails(self):
355355
print(f"Alternative: instead of {email_to} we use {self.alternative_mail[email_to]}")
356356
email_to = self.alternative_mail[email_to]
357357

358-
mail = Mail(mail_record, email_to, path, count)
358+
mail = Mail(mail_record, email_to, path, count, template_data)
359359
# build_mail only used to output metadata of the mail -> send=False -> return None
360-
self.build_mail(mail, send=False, template_data=template_data)
361-
yield (mail, template_data if jinja2 and self.templating and any(self.templating.values()) else {})
360+
self.build_mail(mail, send=False)
361+
yield mail
362362

363-
def build_mail(self, mail, send=False, override_to=None, template_data:dict[str, Any]={}):
363+
def build_mail(self, mail, send=False, override_to=None):
364364
""" creates a MIME message
365365
:param mail: Mail object
366366
:param send: True to send through SMTP, False for just printing the information

0 commit comments

Comments
 (0)