diff --git a/activefires_pp/fire_notifications.py b/activefires_pp/fire_notifications.py index 426d665..1db2a20 100644 --- a/activefires_pp/fire_notifications.py +++ b/activefires_pp/fire_notifications.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2021, 2022 Adam Dybbroe +# Copyright (c) 2021 - 2023 Adam Dybbroe # Author(s): @@ -20,8 +20,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Creating and sending notifications for detected forest fires. -""" +"""Creating and sending notifications for detected forest fires.""" import socket from netrc import netrc @@ -56,8 +55,10 @@ class RecipientDataStruct(object): - def __init__(self): + """A data structure to control the list of configured recipients.""" + def __init__(self): + """Initialize the recipient data structure.""" self.recipients_with_attachment = [] self.recipients_without_attachment = [] self.recipients_all = [] @@ -140,7 +141,6 @@ def _setup_and_start_communication(self): def _set_options_from_config(self, config): """From the configuration on disk set the option dictionary, holding all metadata for processing.""" - for item in config: self.options[item] = config[item] @@ -184,6 +184,12 @@ def run(self): LOG.debug("Message type not supported: %s", str(msg.type)) continue + product_name = msg.data.get('product') + product_list = self.options.get('products') + if product_list and product_name and product_name not in product_list: + LOG.info('Product %s will not generate a notification!', product_name) + continue + output_msg = self.notify_end_users(msg) if output_msg: LOG.debug("Sending message: %s", str(output_msg)) @@ -218,7 +224,6 @@ def notify_end_users(self, msg): def _send_notifications_with_attachments(self, server, recipients, full_message, filename, platform_name): """Send notifications with attachments.""" - notification = MIMEMultipart() notification['From'] = self.sender if platform_name: @@ -252,7 +257,6 @@ def _send_notifications_with_attachments(self, server, recipients, full_message, def _send_notifications_without_attachments(self, server, recipients, sub_messages, platform_name): """Send notifications without attachments.""" - for submsg in sub_messages: notification = MIMEMultipart() notification['From'] = self.sender diff --git a/activefires_pp/tests/test_fire_notifications.py b/activefires_pp/tests/test_fire_notifications.py index f8f32f0..91306ac 100644 --- a/activefires_pp/tests/test_fire_notifications.py +++ b/activefires_pp/tests/test_fire_notifications.py @@ -40,6 +40,10 @@ publish_topic: VIIRS/L2/MSB/National subscribe_topics: VIIRS/L2/Fires/PP/National +products: + - afimg + - afimg_some_other_geoid + smtp_server: smtp.mydomain.se domain: mydomain.se @@ -137,7 +141,9 @@ def test_get_options_national_filtering(self, setup_comm, read_config, gethostna expected = {'publish_topic': 'VIIRS/L2/MSB/National', 'subscribe_topics': ['VIIRS/L2/Fires/PP/National'], - 'smtp_server': 'smtp.mydomain.se', 'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se', + 'products': ['afimg', 'afimg_some_other_geoid'], + 'smtp_server': 'smtp.mydomain.se', + 'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se', 'recipients': ['recipient1@recipients.se', 'recipient2@recipients.se', 'recipient3@recipients.se'], 'recipients_attachment': ['recipient1@recipients.se', 'recipient2@recipients.se'], 'subject': 'My subject', 'max_number_of_fires_in_sms': 3, diff --git a/examples/fire_notifier.yaml b/examples/fire_notifier.yaml index eddb68c..ca33244 100644 --- a/examples/fire_notifier.yaml +++ b/examples/fire_notifier.yaml @@ -2,6 +2,10 @@ publish_topic: VIIRS/L2/MSB/National subscribe_topics: VIIRS/L2/Fires/PP/National +products: + - afimg + - afimg_some_other_geoid + smtp_server: smtp.mydomain.se domain: mydomain.se