You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/01/22 15:16:11 UTC

[GitHub] ashb commented on a change in pull request #4475: [AIRFLOW-3602] Changes ImapHook handling of retrieving no attachments

ashb commented on a change in pull request #4475: [AIRFLOW-3602] Changes ImapHook handling of retrieving no attachments
URL: https://github.com/apache/airflow/pull/4475#discussion_r249823008
 
 

 ##########
 File path: airflow/contrib/hooks/imap_hook.py
 ##########
 @@ -55,72 +53,98 @@ def has_mail_attachment(self, name, mail_folder='INBOX', check_regex=False):
         :param name: The name of the attachment that will be searched for.
         :type name: str
         :param mail_folder: The mail folder where to look at.
-                            The default value is 'INBOX'.
         :type mail_folder: str
         :param check_regex: Checks the name for a regular expression.
-                            The default value is False.
         :type check_regex: bool
         :returns: True if there is an attachment with the given name and False if not.
         :rtype: bool
         """
-        mail_attachments = self._retrieve_mails_attachments_by_name(name, mail_folder,
+        mail_attachments = self._retrieve_mails_attachments_by_name(name,
+                                                                    mail_folder,
                                                                     check_regex,
                                                                     latest_only=True)
         return len(mail_attachments) > 0
 
-    def retrieve_mail_attachments(self, name, mail_folder='INBOX', check_regex=False,
-                                  latest_only=False):
+    def retrieve_mail_attachments(self,
+                                  name,
+                                  mail_folder='INBOX',
+                                  check_regex=False,
+                                  latest_only=False,
+                                  not_found_mode='raise'):
         """
         Retrieves mail's attachments in the mail folder by its name.
 
         :param name: The name of the attachment that will be downloaded.
         :type name: str
         :param mail_folder: The mail folder where to look at.
-                            The default value is 'INBOX'.
         :type mail_folder: str
         :param check_regex: Checks the name for a regular expression.
-                            The default value is False.
         :type check_regex: bool
         :param latest_only: If set to True it will only retrieve
                             the first matched attachment.
-                            The default value is False.
         :type latest_only: bool
+        :param not_found_mode: Specify what should happen if no attachment has been found.
+                               Supported values are 'raise' and 'warn'.
+                               If it is set to 'raise' it will raise an exception and
+                               if set to 'warn' it will only print a warning.
+        :type not_found_mode: str
         :returns: a list of tuple each containing the attachment filename and its payload.
         :rtype: a list of tuple
         """
-        mail_attachments = self._retrieve_mails_attachments_by_name(name, mail_folder,
+        mail_attachments = self._retrieve_mails_attachments_by_name(name,
+                                                                    mail_folder,
                                                                     check_regex,
                                                                     latest_only)
+        if not mail_attachments:
+            self._handle_not_found_mode(not_found_mode)
+
         return mail_attachments
 
-    def download_mail_attachments(self, name, local_output_directory, mail_folder='INBOX',
-                                  check_regex=False, latest_only=False):
+    def download_mail_attachments(self,
+                                  name,
+                                  local_output_directory,
+                                  mail_folder='INBOX',
+                                  check_regex=False,
+                                  latest_only=False,
+                                  not_found_mode='raise'):
         """
-        Downloads mail's attachments in the mail folder by its name
-        to the local directory.
+        Downloads mail's attachments in the mail folder by its name to the local directory.
 
         :param name: The name of the attachment that will be downloaded.
         :type name: str
         :param local_output_directory: The output directory on the local machine
                                        where the files will be downloaded to.
         :type local_output_directory: str
         :param mail_folder: The mail folder where to look at.
-                            The default value is 'INBOX'.
         :type mail_folder: str
         :param check_regex: Checks the name for a regular expression.
-                            The default value is False.
         :type check_regex: bool
         :param latest_only: If set to True it will only download
                             the first matched attachment.
-                            The default value is False.
         :type latest_only: bool
+        :param not_found_mode: Specify what should happen if no attachment has been found.
+                               Supported values are 'raise' and 'warn'.
+                               If it is set to 'raise' it will raise an exception and
+                               if set to 'warn' it will only print a warning.
+        :type not_found_mode: str
         """
-        mail_attachments = self._retrieve_mails_attachments_by_name(name, mail_folder,
-                                                                    check_regex, latest_only)
+        mail_attachments = self._retrieve_mails_attachments_by_name(name,
+                                                                    mail_folder,
+                                                                    check_regex,
+                                                                    latest_only)
+
+        if not mail_attachments:
+            self._handle_not_found_mode(not_found_mode)
+
         self._create_files(mail_attachments, local_output_directory)
 
-    def _retrieve_mails_attachments_by_name(self, name, mail_folder, check_regex,
-                                            latest_only):
+    def _handle_not_found_mode(self, not_found_mode):
+        if not_found_mode is 'raise':
+            raise AirflowException('No mail attachments found!')
+        if not_found_mode is 'warn':
+            self.log.warning('No mail attachments found!')
 
 Review comment:
   I'd like a 'silent' option - do nothing at all. If I'm conditionally handling this in my custom operator I don't want a log message.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services