Create function get_user_mail_files
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 4 Jun 2019 07:52:54 +0000 (09:52 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 8 Aug 2019 09:54:43 +0000 (11:54 +0200)
Sometimes do not need to parse the mail, just need to know how many/which
files there are.

src/mail_utils.py

index 0288c04..c2cfb60 100644 (file)
@@ -745,6 +745,31 @@ def parse_mail_file(file_name, headers_only=True, attachment_filenames=False):
                       if filename is not None)
 
 
+def get_user_mail_files(user, mailbox='INBOX'):
+    """
+    Iterate over mails in given folder of given user; yields file names
+
+    :param str mailbox: name of mailbox to use, INBOX (default) for base folder;
+                        name is modified using :py:func:`cyrus_escape`
+    :returns: nothing; but yields full path to messages on disc
+    """
+    # base folder of user mail
+    folder = os.path.join('/datastore', 'imap-mails', 'user', user)
+
+    # adapt paths like "INBOX/sub/dir" need to "sub/dir"
+    subdirs = mailbox.split('/')
+    if subdirs[0].upper() == 'INBOX':
+        subdirs = subdirs[1:]
+    folder = os.path.join(folder,
+                          *(cyrus_escape(subdir) for subdir in subdirs))
+
+    for filename in os.listdir(folder):
+        if not re.match(r'\d+\.', filename):
+            continue
+        full_path = os.path.join(folder, filename)
+        yield full_path
+
+
 def get_user_mail(user, mailbox='INBOX', **kwargs):
     """
     Iterate over mails in given folder of given user; yields parsed mails.
@@ -757,15 +782,8 @@ def get_user_mail(user, mailbox='INBOX', **kwargs):
               full path to the message on disc, and the latter is the outcome
               of :py:func:`parse_mail_file` for that file
     """
-    folder = os.path.join('/datastore', 'imap-mails', 'user', user)
-    if mailbox != 'INBOX':
-        folder = os.path.join(folder, cyrus_escape(mailbox))
-    for filename in os.listdir(folder):
-        if not re.match(r'\d+\.', filename):
-            continue
-        full_path = os.path.join(folder, filename)
-        yield full_path, parse_mail_file(os.path.join(folder, filename),
-                                         **kwargs)
+    for full_path in get_user_mail_files(user, mailbox):
+        yield full_path, parse_mail_file(full_path, **kwargs)
 
 
 def get_message_text(filename, fallback_encoding='iso8859-1',