8
8
import sys
9
9
from tempfile import NamedTemporaryFile
10
10
import time
11
- from typing import Any , Optional
11
+ from typing import Any , Generator , Optional
12
12
import zipfile
13
13
from base64 import b64decode
14
14
from collections import OrderedDict
@@ -49,6 +49,7 @@ class Mail:
49
49
to : str
50
50
path : str
51
51
count : int
52
+ template_data : dict [str ,Any ]
52
53
53
54
54
55
class SMTPBatchOutputBot (Bot ):
@@ -164,7 +165,7 @@ def cli_run(self):
164
165
165
166
print ("Preparing mail queue..." )
166
167
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 ]
168
169
169
170
print ("" )
170
171
if self .limit_results :
@@ -205,9 +206,7 @@ def cli_run(self):
205
206
count = 0
206
207
exit_code = 0
207
208
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 )
211
210
if not succ :
212
211
exit_code = 1
213
212
else :
@@ -228,7 +227,6 @@ def cli_run(self):
228
227
sys .exit (exit_code )
229
228
elif i == "clear" :
230
229
for mail in mails :
231
- mail = mail [0 ]
232
230
self .cache .redis .delete (mail .key )
233
231
print ("Queue cleared." )
234
232
sys .exit (0 )
@@ -240,7 +238,6 @@ def cli_run(self):
240
238
self .send_mails_to_tester (mails )
241
239
else :
242
240
for mail in mails :
243
- mail = mail [0 ]
244
241
if mail .to == i :
245
242
self .send_mails_to_tester ([mail ])
246
243
break
@@ -259,10 +256,10 @@ def send_mails_to_tester(self, mails):
259
256
:param mails: list
260
257
"""
261
258
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 )])
263
260
print (f"{ count } × mail sent to: { self .testing_to } \n " )
264
261
265
- def prepare_mails (self ):
262
+ def prepare_mails (self ) -> Generator [ Mail ] :
266
263
""" Generates Mail objects """
267
264
268
265
for mail_record in self .cache .redis .keys (f"{ self .key } *" )[slice (self .limit_results )]:
@@ -334,11 +331,14 @@ def prepare_mails(self):
334
331
335
332
# collect all data which must be the same for all events of the
336
333
# 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
+ }
342
342
343
343
email_to = template_data ["source_abuse_contact" ]
344
344
filename = f'{ time .strftime ("%y%m%d" )} _{ count } _events'
@@ -355,12 +355,12 @@ def prepare_mails(self):
355
355
print (f"Alternative: instead of { email_to } we use { self .alternative_mail [email_to ]} " )
356
356
email_to = self .alternative_mail [email_to ]
357
357
358
- mail = Mail (mail_record , email_to , path , count )
358
+ mail = Mail (mail_record , email_to , path , count , template_data )
359
359
# 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
362
362
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 ):
364
364
""" creates a MIME message
365
365
:param mail: Mail object
366
366
:param send: True to send through SMTP, False for just printing the information
0 commit comments