From f44055b0f27932a9f67b99a4f5aacc1b2e06e198 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 4 Jun 2019 09:52:54 +0200 Subject: [PATCH] Create function get_user_mail_files Sometimes do not need to parse the mail, just need to know how many/which files there are. --- src/mail_utils.py | 36 +++++++++++++++++++++++++++--------- 1 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/mail_utils.py b/src/mail_utils.py index 0288c04..c2cfb60 100644 --- a/src/mail_utils.py +++ b/src/mail_utils.py @@ -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', -- 1.7.1